Class: Bosh::Cli::HashChangeset

Inherits:
Object
  • Object
show all
Defined in:
lib/cli/changeset_helper.rb

Defined Under Namespace

Classes: FormatError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeHashChangeset

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

#valuesObject

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

Returns:

  • (Boolean)


137
138
139
# File 'lib/cli/changeset_helper.rb', line 137

def boolean?(value)
  value.kind_of?(TrueClass) || value.kind_of?(FalseClass)
end

#diff(old_value, new_value, indent) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/cli/changeset_helper.rb', line 83

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}".make_red
  end

  added.each do |line|
    line = line.inspect if line.is_a?(Hash)
    lines << "#{indent}+ #{line}".make_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

#keysObject



18
19
20
# File 'lib/cli/changeset_helper.rb', line 18

def keys
  @children.keys
end

#leaf?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/cli/changeset_helper.rb', line 44

def leaf?
  @children.empty?
end

#newObject



111
112
113
# File 'lib/cli/changeset_helper.rb', line 111

def new
  @values[:new]
end

#oldObject



107
108
109
# File 'lib/cli/changeset_helper.rb', line 107

def old
  @values[:old]
end

#stateObject



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/cli/changeset_helper.rb', line 115

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
# 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: ".make_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 + "+ #{k}: ".make_yellow + v.new.to_s
      when :removed
        out << indent + "- #{k}: ".make_red + v.old.to_s
      when :changed
        out << indent + "± #{k}: ".make_yellow
        out << diff(v.old, v.new, indent + "  ")
      end
    else
      child_summary = v.summary(level + 1)

      unless child_summary.empty?
        out << indent + k
        out << child_summary
      end
    end
  end
  out
end