Class: Bosh::Cli::HashChangeset
Defined Under Namespace
Classes: FormatError
Instance Attribute Summary collapse
-
#values ⇒ Object
Returns the value of attribute values.
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, value) ⇒ Object
- #add_hash(hash, as) ⇒ Object
- #boolean?(value) ⇒ Boolean
- #diff(old_value, new_value, indent) ⇒ Object
- #each(&block) ⇒ Object
-
#initialize ⇒ HashChangeset
constructor
A new instance of HashChangeset.
- #keys ⇒ Object
- #leaf? ⇒ Boolean
- #new ⇒ Object
- #old ⇒ Object
- #state ⇒ Object
- #summary(level = 0) ⇒ Object
Constructor Details
#initialize ⇒ HashChangeset
Returns a new instance of HashChangeset.
9 10 11 12 |
# File 'lib/cli/changeset_helper.rb', line 9 def initialize @children = {} @values = { :old => nil, :new => nil } end |
Instance Attribute Details
#values ⇒ Object
Returns the value of attribute values.
7 8 9 |
# File 'lib/cli/changeset_helper.rb', line 7 def values @values end |
Instance Method Details
#[](key) ⇒ Object
14 15 16 |
# File 'lib/cli/changeset_helper.rb', line 14 def [](key) @children[key.to_s] end |
#[]=(key, value) ⇒ Object
22 23 24 |
# File 'lib/cli/changeset_helper.rb', line 22 def []=(key, value) @children[key.to_s] = value end |
#add_hash(hash, as) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/cli/changeset_helper.rb', line 26 def add_hash(hash, as) unless hash.is_a?(Hash) raise FormatError, "Trying to add #{hash.class} to a changeset, " + "Hash expected" end self.values[as] = hash hash.each_pair do |k, v| self[k] ||= HashChangeset.new self[k].values[as] = v if v.is_a?(Hash) self[k].add_hash(v, as) end end end |
#boolean?(value) ⇒ Boolean
138 139 140 |
# File 'lib/cli/changeset_helper.rb', line 138 def boolean?(value) value.kind_of?(TrueClass) || value.kind_of?(FalseClass) end |
#diff(old_value, new_value, indent) ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/cli/changeset_helper.rb', line 84 def diff(old_value, new_value, indent) old_value = [old_value] unless old_value.kind_of?(Array) new_value = [new_value] unless new_value.kind_of?(Array) added = new_value - old_value removed = old_value - new_value lines = [] # Ruby 1.8 has ugly Hash#to_s, hence the normalization removed.each do |line| line = line.inspect if line.is_a?(Hash) lines << "#{indent}- #{line}".red end added.each do |line| line = line.inspect if line.is_a?(Hash) lines << "#{indent}+ #{line}".green end lines.join("\n") end |
#each(&block) ⇒ Object
48 49 50 |
# File 'lib/cli/changeset_helper.rb', line 48 def each(&block) @children.each_value { |v| yield v } end |
#keys ⇒ Object
18 19 20 |
# File 'lib/cli/changeset_helper.rb', line 18 def keys @children.keys end |
#leaf? ⇒ Boolean
44 45 46 |
# File 'lib/cli/changeset_helper.rb', line 44 def leaf? @children.empty? end |
#new ⇒ Object
112 113 114 |
# File 'lib/cli/changeset_helper.rb', line 112 def new @values[:new] end |
#old ⇒ Object
108 109 110 |
# File 'lib/cli/changeset_helper.rb', line 108 def old @values[:old] end |
#state ⇒ Object
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/cli/changeset_helper.rb', line 116 def state if old.nil? && new.nil? :none elsif old.nil? && !new.nil? :added elsif !old.nil? && new.nil? :removed elsif old.class != new.class && !(boolean?(old) && boolean?(new)) :mismatch elsif old == new :same else :changed end end |
#summary(level = 0) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/cli/changeset_helper.rb', line 52 def summary(level = 0) indent = " " * level out = [] @children.each_pair do |k, v| if v.state == :mismatch out << indent + "#{k} type changed: ".yellow + "#{v.old.class.to_s} -> #{v.new.class.to_s}" out << diff(v.old, v.new, indent + " ") elsif v.leaf? case v.state when :added out << indent + "added #{k}: ".yellow + v.new.to_s when :removed out << indent + "removed #{k}: ".red + v.old.to_s when :changed out << indent + "changed #{k}: ".yellow out << diff(v.old, v.new, indent + " ") end else # TODO: track renames? child_summary = v.summary(level + 1) unless child_summary.empty? out << indent + k out << child_summary end end end out end |