Skip to content

Commit

Permalink
Refactor each_with_object, qualify constants
Browse files Browse the repository at this point in the history
  • Loading branch information
flash-gordon committed Jan 19, 2025
1 parent 7020ece commit 334a73f
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 22 deletions.
25 changes: 13 additions & 12 deletions lib/rom/yesql/gateway.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ module Yesql
# expose access to gateway's queries.
#
# @api public
class Gateway < ROM::Gateway
class Gateway < ::ROM::Gateway
extend Initializer

adapter :yesql
Expand Down Expand Up @@ -73,7 +73,7 @@ class Gateway < ROM::Gateway
# @api public
def initialize(*, **)
super
@connection = Sequel.connect(uri, options)
@connection = ::Sequel.connect(uri, options)
@queries = @queries.merge(load_queries(path)).freeze
Relation.query_proc(query_proc)
Relation.load_queries(queries)
Expand Down Expand Up @@ -110,20 +110,21 @@ def load_queries(path)
if path.nil?
{}
else
Dir["#{path}/*"].each_with_object({}) do |dir, fs_queries|
dataset = File.basename(dir).to_sym

fs_queries[dataset] = Dir["#{dir}/**/*.sql"].each_with_object({}) do |file, ds_queries|
query_name = File.basename(file, ".*").to_sym
sql = File.read(file).strip

ds_queries[query_name] = sql
end
::Dir["#{path}/*"].to_h do |dir|
[
::File.basename(dir).to_sym,
::Dir["#{dir}/**/*.sql"].to_h do |file|
query_name = ::File.basename(file, ".*").to_sym
sql = ::File.read(file).strip

[query_name, sql]
end
]
end
end
end
end
end

register_adapter(:yesql, ROM::Yesql)
register_adapter(:yesql, Yesql)
end
14 changes: 5 additions & 9 deletions lib/rom/yesql/relation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ module Yesql
# rom.relations[:reports] # use like a normal rom relation
#
# @api public
class Relation < ROM::Relation
class Relation < ::ROM::Relation
adapter :yesql

extend Dry::Core::ClassAttributes
extend ::Dry::Core::ClassAttributes

defines :query_proc

Materialized = Class.new(ROM::Relation)
Materialized = ::Class.new(::ROM::Relation)

# Extends a relation with query methods
#
Expand Down Expand Up @@ -84,13 +84,9 @@ def self.queries
#
# @api private
def self.load_queries(queries)
@queries = {}
queries.each do |ds, ds_queries|
@queries[ds] = ds_queries.each_with_object({}) do |(name, query), h|
h[name] = query
end
@queries = queries.to_h do |ds, ds_queries|
[ds, ds_queries.to_h { |name, query| [name, query] }]
end
@queries
end

# Returns query proc set on a relation class
Expand Down
2 changes: 1 addition & 1 deletion lib/rom/yesql/relation/class_interface.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

module ROM
module Yesql
class Relation < ROM::Relation
class Relation < ::ROM::Relation
module ClassInterface
# Sets dataset name for the relation class
#
Expand Down

0 comments on commit 334a73f

Please sign in to comment.