Class: AttributeStruct

Inherits:
Object
  • Object
show all
Defined in:
lib/attribute_struct/version.rb,
lib/attribute_struct/attribute_hash.rb,
lib/attribute_struct/attribute_struct.rb

Defined Under Namespace

Classes: AttributeHash, Version

Constant Summary collapse

VERSION =
Version.new('0.1.0')

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args, &block) ⇒ AttributeStruct

Returns a new instance of AttributeStruct.



36
37
38
39
40
41
42
43
44
45
# File 'lib/attribute_struct/attribute_struct.rb', line 36

def initialize(*args, &block)
  self.class.load_the_hash
  @_camel_keys = self.class.camel_keys
  @table = __hashish.new
  unless(args.empty?)
    if(args.size == 1 && args.first.is_a?(Hash))
      _load(args.first)
    end
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args, &block) ⇒ Object



64
65
66
67
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
101
102
103
# File 'lib/attribute_struct/attribute_struct.rb', line 64

def method_missing(sym, *args, &block)
  if((s = sym.to_s).end_with?('='))
    s.slice!(-1, s.length)
    sym = s
  end
  sym = _process_key(sym)
  @table[sym] ||= AttributeStruct.new
  if(!args.empty? || block)
    if(args.empty? && block)
      base = @table[sym]
      if(block.arity == 0)
        base.instance_exec(&block)
      else
        base.instance_exec(base, &block)
      end
      @table[sym] = base
    elsif(!args.empty? && block)
      base = @table[sym]
      base = self.class.new unless base.is_a?(self.class)
      @table[sym] = base
      leaf = base
      args.each do |arg|
        leaf = base[arg]
        unless(leaf.is_a?(self.class))
          leaf = self.class.new
          base._set(arg, leaf)
          base = leaf
        end
      end
      if(block.arity == 0)
        leaf.instance_exec(&block)
      else
        leaf.instance_exec(leaf, &block)
      end
    else
      @table[sym] = args.first
    end
  end
  @table[sym]
end

Class Attribute Details

.camel_keysObject

Returns the value of attribute camel_keys.



5
6
7
# File 'lib/attribute_struct/attribute_struct.rb', line 5

def camel_keys
  @camel_keys
end

.force_chefObject

Returns the value of attribute force_chef.



6
7
8
# File 'lib/attribute_struct/attribute_struct.rb', line 6

def force_chef
  @force_chef
end

Instance Attribute Details

#_camel_keysObject

Returns the value of attribute _camel_keys.



34
35
36
# File 'lib/attribute_struct/attribute_struct.rb', line 34

def _camel_keys
  @_camel_keys
end

Class Method Details

.load_the_camelsObject



13
14
15
16
17
18
# File 'lib/attribute_struct/attribute_struct.rb', line 13

def load_the_camels
  unless(@camels_loaded)
    require 'attrubute_struct/monkey_camels'
    @camels_loaded = true
  end
end

.load_the_hashObject



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/attribute_struct/attribute_struct.rb', line 20

def load_the_hash
  unless(@hash_loaded)
    if(defined?(Chef) || force_chef)
      require 'chef/mash'
      require 'chef/mixin/deep_merge'
    else
      require 'attribute_struct/attribute_hash'
    end
    @hash_loaded
  end
end

Instance Method Details

#[](key) ⇒ Object



52
53
54
# File 'lib/attribute_struct/attribute_struct.rb', line 52

def [](key)
  _data[_process_key(key)]
end

#__hashishObject



154
155
156
# File 'lib/attribute_struct/attribute_struct.rb', line 154

def __hashish
  defined?(Mash) ? Mash : AttributeHash
end

#_dataObject



113
114
115
# File 'lib/attribute_struct/attribute_struct.rb', line 113

def _data
  @table
end

#_dumpObject



117
118
119
120
121
122
123
# File 'lib/attribute_struct/attribute_struct.rb', line 117

def _dump
  __hashish[
    *(@table.map{|key, value|
        [key, value.is_a?(self.class) ? value._dump : value]
      }.flatten(1))
  ]
end

#_keysObject



109
110
111
# File 'lib/attribute_struct/attribute_struct.rb', line 109

def _keys
  _data.keys
end

#_load(hashish) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
# File 'lib/attribute_struct/attribute_struct.rb', line 125

def _load(hashish)
  @table.clear
  hashish.each do |key, value|
    if(value.is_a?(Hash))
      self._set(key)._load(value)
    else
      self._set(key, value)
    end
  end
  self
end

#_merge(target) ⇒ Object



137
138
139
140
141
142
143
144
145
146
# File 'lib/attribute_struct/attribute_struct.rb', line 137

def _merge(target)
  source = deep_copy
  dest = target.deep_copy
  if(defined?(Mash))
    result = Chef::Mixin::DeepMerge.merge(source, dest)
  else
    result = source.deep_merge(dest)
  end
  AttributeStruct.new(result)
end

#_merge!(target) ⇒ Object



148
149
150
151
152
# File 'lib/attribute_struct/attribute_struct.rb', line 148

def _merge!(target)
  result = _merge(target)._dump
  _load(result)
  self
end

#_process_key(key) ⇒ Object



179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/attribute_struct/attribute_struct.rb', line 179

def _process_key(key)
  key = key.to_s
  if(_camel_keys && key._camel?)
    key.to_s.split('_').map do |part|
      "#{part[0,1].upcase}#{part[1,part.size]}"
    end.join.to_sym
  else
    if(_camel_keys)
      # Convert so Hash doesn't make a new one and lose the meta
      key = CamelString.new(key) unless key.is_a?(CamelString)
    end
    key
  end
end

#_set(key, val = nil, &block) ⇒ Object



56
57
58
59
60
61
62
# File 'lib/attribute_struct/attribute_struct.rb', line 56

def _set(key, val=nil, &block)
  if(val)
    self.method_missing(key, val, &block)
  else
    self.method_missing(key, &block)
  end
end

#deep_copy(thing = nil) ⇒ Object



166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/attribute_struct/attribute_struct.rb', line 166

def deep_copy(thing=nil)
  thing ||= _dump
  if(thing.is_a?(Enumerable))
    val = thing.map{|v| v.is_a?(Enumerable) ? deep_copy(v) : do_dup(v) }
  else
    val = do_dup(thing)
  end
  if(thing.is_a?(Hash))
    val = __hashish[*val.flatten(1)]
  end
  val
end

#do_dup(v) ⇒ Object



158
159
160
161
162
163
164
# File 'lib/attribute_struct/attribute_struct.rb', line 158

def do_dup(v)
  begin
    v.dup
  rescue
    v.is_a?(Symbol) ? v.to_s : v
  end
end

#nil?Boolean

Returns:

  • (Boolean)


105
106
107
# File 'lib/attribute_struct/attribute_struct.rb', line 105

def nil?
  _data.empty?
end