Class: Cashboard::Struct

Inherits:
TypecastedOpenStruct show all
Defined in:
lib/cashboard.rb

Direct Known Subclasses

Base

Instance Method Summary collapse

Methods inherited from TypecastedOpenStruct

attr_typecast, element

Constructor Details

#initialize(hash = {}) ⇒ Struct

Since we’re dealing with initializing from hashes with ‘content’ keys we need to set properties based on those keys.

Additionally, we do some magic to ignore attributes we don’t care about.

The basic concept is lifted from ostruct.rb



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/cashboard.rb', line 23

def initialize(hash={})
  @table = {}
  return unless hash
  hash.each do |k,v|
    # Remove keys that aren't useful for our purposes.
    if v.class == Hash
      Cashboard::IGNORED_XML_KEYS.each {|ignored| v.delete(ignored)}
    end
    # Access items based on the 'content' key inside the hash.
    # Allows us to deal with all XML tags equally, even if the tags
    # have attributes or not.
    if v.class == Hash && v['content']
      @table[k.to_sym] = v['content']
    elsif v.class == Hash && v.empty?
      @table[k.to_sym] = nil
    else
      @table[k.to_sym] = v
    end
    new_ostruct_member(k)
  end
end