Class: Pups::MergeCommand
Instance Attribute Summary collapse
-
#filename ⇒ Object
readonly
Returns the value of attribute filename.
-
#merge_hash ⇒ Object
readonly
Returns the value of attribute merge_hash.
Class Method Summary collapse
- .deep_merge(first, second, *args) ⇒ Object
- .from_str(command, params) ⇒ Object
- .parse_command(command) ⇒ Object
Instance Method Summary collapse
-
#initialize(command, params) ⇒ MergeCommand
constructor
A new instance of MergeCommand.
- #run ⇒ Object
Methods inherited from Command
#interpolate_params, interpolate_params, run
Constructor Details
#initialize(command, params) ⇒ MergeCommand
Returns a new instance of MergeCommand.
20 21 22 23 24 25 26 |
# File 'lib/pups/merge_command.rb', line 20 def initialize(command, params) @params = params filename, target_param = Pups::MergeCommand.parse_command(command) @filename = interpolate_params(filename) @merge_hash = params[target_param] end |
Instance Attribute Details
#filename ⇒ Object (readonly)
Returns the value of attribute filename.
5 6 7 |
# File 'lib/pups/merge_command.rb', line 5 def filename @filename end |
#merge_hash ⇒ Object (readonly)
Returns the value of attribute merge_hash.
5 6 7 |
# File 'lib/pups/merge_command.rb', line 5 def merge_hash @merge_hash end |
Class Method Details
.deep_merge(first, second, *args) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/pups/merge_command.rb', line 34 def self.deep_merge(first, second, *args) args ||= [] merge_arrays = args.include? :merge_arrays merger = proc do |_key, v1, v2| if v1.is_a?(Hash) && v2.is_a?(Hash) v1.merge(v2, &merger) elsif v1.is_a?(Array) && v2.is_a?(Array) merge_arrays ? v1 + v2 : v2 elsif v2.is_a?(NilClass) v1 else v2 end end first.merge(second, &merger) end |
.from_str(command, params) ⇒ Object
7 8 9 |
# File 'lib/pups/merge_command.rb', line 7 def self.from_str(command, params) new(command, params) end |
.parse_command(command) ⇒ Object
11 12 13 14 15 16 17 18 |
# File 'lib/pups/merge_command.rb', line 11 def self.parse_command(command) split = command.split(" ") unless split[-1][0] == "$" raise ArgumentError, "Invalid merge command #{command}" end [split[0..-2].join(" "), split[-1][1..-1]] end |
Instance Method Details
#run ⇒ Object
28 29 30 31 32 |
# File 'lib/pups/merge_command.rb', line 28 def run merged = self.class.deep_merge(YAML.load_file(@filename), @merge_hash) File.open(@filename, "w") { |f| f.write(merged.to_yaml) } Pups.log.info("Merge: #{@filename} with: \n#{@merge_hash.inspect}") end |