Class: Remocon::Command::Pull
- Inherits:
-
Object
- Object
- Remocon::Command::Pull
- Includes:
- Remocon::ConditionSorter, ParameterSorter
- Defined in:
- lib/remocon/command/pull_command.rb
Defined Under Namespace
Classes: RemoteConfig
Constant Summary
Constants included from ParameterSorter
ParameterSorter::PARAMETER_KEYS
Constants included from Remocon::ConditionSorter
Remocon::ConditionSorter::CONDITION_KEYS
Instance Attribute Summary collapse
-
#cmd_opts ⇒ Object
readonly
Returns the value of attribute cmd_opts.
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#left ⇒ Object
readonly
Returns the value of attribute left.
Instance Method Summary collapse
- #conditions_diff(left, right) ⇒ Object
-
#initialize(opts) ⇒ Pull
constructor
A new instance of Pull.
- #parameters_diff(left, right) ⇒ Object
- #run ⇒ Object
Methods included from ParameterSorter
#comparator_of_parameter_keys, #sort_conditions_of_parameters, #sort_parameters
Methods included from Remocon::ConditionSorter
#comparator_of_condition_keys, #sort_conditions
Constructor Details
Instance Attribute Details
#cmd_opts ⇒ Object (readonly)
Returns the value of attribute cmd_opts.
36 37 38 |
# File 'lib/remocon/command/pull_command.rb', line 36 def cmd_opts @cmd_opts end |
#config ⇒ Object (readonly)
Returns the value of attribute config.
36 37 38 |
# File 'lib/remocon/command/pull_command.rb', line 36 def config @config end |
#left ⇒ Object (readonly)
Returns the value of attribute left.
36 37 38 |
# File 'lib/remocon/command/pull_command.rb', line 36 def left @left end |
Instance Method Details
#conditions_diff(left, right) ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/remocon/command/pull_command.rb', line 68 def conditions_diff(left, right) left_names = left.map { |c| c[:name] } right_names = right.map { |c| c[:name] } added_names = right_names - left_names removed_names = left_names - right_names added = added_names.each_with_object([]) do |k, acc| acc.push(right.find { |c| c[:name] == k }) end changed = [] unchanged = [] (right_names & left_names).each do |k| old = left.find { |c| c[:name] == k } new = right.find { |c| c[:name] == k } if old == new unchanged.push(old) else changed.push(Remocon::ConditionFileDumper.new(new).dump) end end removed = [] removed_names.each do |k| removed.push(left.find { |c| c[:name] == k }) end [unchanged, added, changed, removed] end |
#parameters_diff(left, right) ⇒ Object
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/remocon/command/pull_command.rb', line 102 def parameters_diff(left, right) added_keys = right.keys - left.keys removed_keys = left.keys - right.keys added = added_keys.each_with_object({}) do |k, acc| acc.merge!(Remocon::ParameterFileDumper.new({ k => right[k] }).dump) end changed = {} unchanged = {} (right.keys & left.keys).each do |k| # both of left and right is config json's format # comparison should be done based on the format but dumped format should be returned old = left[k] new = right[k] if old == new unchanged.merge!(Remocon::ParameterFileDumper.new({ k => old }).dump) else changed.merge!(Remocon::ParameterFileDumper.new({ k => new }).dump) end end removed = removed_keys.each_with_object({}) do |k, acc| acc.merge!(Remocon::ParameterFileDumper.new({ k => left[k] }).dump) end [unchanged, added, changed, removed] end |
#run ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/remocon/command/pull_command.rb', line 44 def run raw_json, etag = Remocon::Request.pull(config) raw_hash = JSON.parse(raw_json).with_indifferent_access raise "etag cannot be fetched. please try again" unless etag conditions = raw_hash[:conditions] || [] parameters = raw_hash[:parameters] || {} if config.merge? && File.exist?(config.parameters_file_path) && File.exist?(config.parameters_file_path) unchanged_conditions, added_conditions, changed_conditions, = conditions_diff(left.conditions_to_be_compared, conditions) unchanged_parameters, added_parameters, changed_parameters, = parameters_diff(left.parameters_to_be_compared, parameters) conditions_yaml = JSON.parse(sort_conditions(unchanged_conditions + added_conditions + changed_conditions).to_json).to_yaml parameters_yaml = JSON.parse(sort_parameters(unchanged_parameters.merge(added_parameters).merge(changed_parameters)).to_json).to_yaml else conditions_yaml = JSON.parse(sort_conditions(Remocon::ConditionFileDumper.new(conditions).dump).to_json).to_yaml parameters_yaml = JSON.parse(sort_parameters(Remocon::ParameterFileDumper.new(parameters).dump).to_json).to_yaml end write_to_files(conditions_yaml, parameters_yaml, etag) end |