-
-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tuck ComposeRefinements under the Bashly namespace
- Loading branch information
Showing
1 changed file
with
33 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |