Skip to content

Commit

Permalink
tuck ComposeRefinements under the Bashly namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
DannyBen committed Feb 29, 2024
1 parent eaa13aa commit ef1b8d8
Showing 1 changed file with 33 additions and 31 deletions.
64 changes: 33 additions & 31 deletions lib/bashly/refinements/compose_refinements.rb
Original file line number Diff line number Diff line change
@@ -1,43 +1,45 @@
module ComposeRefinements
refine Hash do
def compose(keyword = 'import')
result = {}
each do |k, v|
if k.to_s == keyword
sub = safe_load_yaml(v).compose keyword
if sub.is_a? Array
result = sub
module Bashly
module ComposeRefinements
refine Hash do
def compose(keyword = 'import')
result = {}
each do |k, v|
if k.to_s == keyword
sub = safe_load_yaml(v).compose keyword
if sub.is_a? Array
result = sub
else
result.merge! sub
end
elsif v.respond_to? :compose
result[k] = v.compose keyword
else
result.merge! sub
result[k] = v
end
elsif v.respond_to? :compose
result[k] = v.compose keyword
else
result[k] = v
end
result
end
result
end

def safe_load_yaml(path)
loaded = YAML.load_erb_file path
return loaded if loaded.is_a?(Array) || loaded.is_a?(Hash)
def safe_load_yaml(path)
loaded = YAML.load_erb_file path
return loaded if loaded.is_a?(Array) || loaded.is_a?(Hash)

raise Bashly::ConfigurationError, "Cannot find a valid YAML in g`#{path}`"
rescue Errno::ENOENT
raise Bashly::ConfigurationError, "Cannot find import file g`#{path}`"
raise Bashly::ConfigurationError, "Cannot find a valid YAML in g`#{path}`"
rescue Errno::ENOENT
raise Bashly::ConfigurationError, "Cannot find import file g`#{path}`"
end
end
end

refine Array do
def compose(keyword = 'import')
map do |x|
if x.respond_to? :compose
x.compose keyword
else
x
refine Array do
def compose(keyword = 'import')
map do |x|
if x.respond_to? :compose
x.compose keyword
else
x
end
end
end
end
end
end
end

0 comments on commit ef1b8d8

Please sign in to comment.