Class: Wombat::PropertyContainer

Inherits:
Hash
  • Object
show all
Defined in:
lib/wombat/property_container.rb

Direct Known Subclasses

Iterator, Metadata

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePropertyContainer



7
8
9
# File 'lib/wombat/property_container.rb', line 7

def initialize
  @iterators = []
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/wombat/property_container.rb', line 11

def method_missing method, *args, &block
  if args.empty? && block
    self["#{method.to_s}"] = PropertyContainer.new unless self["#{method.to_s}"]
    block.call(self["#{method.to_s}"])
  else
    self[method.to_s] = Property.new(
      name: method.to_s,
      selector: args.first,
      format: args[1],
      namespaces: args[2],
      callback: block)
  end      
end

Instance Attribute Details

#iteratorsObject

Returns the value of attribute iterators.



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

def iterators
  @iterators
end

Instance Method Details

#all_propertiesObject



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/wombat/property_container.rb', line 28

def all_properties
  values.flat_map { |v|
    if v.kind_of? PropertyContainer
      v.all_properties
    elsif v.kind_of? Property
      v
    else
      nil
    end
  }.compact
end

#flattenObject



40
41
42
43
44
45
46
47
48
# File 'lib/wombat/property_container.rb', line 40

def flatten
  Hash.new.tap do |h|
    keys.map do |k|
      if self[k].kind_of?(PropertyContainer) || self[k].kind_of?(Property)
        h[k] = self[k].flatten
      end
    end
  end.merge(iterators.inject({}) {|memo, i| memo.merge(i.flatten) })
end

#for_each(selector) ⇒ Object



50
51
52
53
54
# File 'lib/wombat/property_container.rb', line 50

def for_each selector
  Iterator.new(selector).tap do |i|
    iterators << i
  end
end

#to_aryObject



25
26
# File 'lib/wombat/property_container.rb', line 25

def to_ary
end