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

Returns a new instance of PropertyContainer.



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

#flatten(depth = nil) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/wombat/property_container.rb', line 47

def flatten(depth = nil)
  properties = Hash.new.tap do |h|
    keys.map do |k|
      val = self[k]
      if val.is_a?(PropertyContainer) || val.is_a?(Property)
        h[k] = val.flatten depth
      end
    end
  end

  iters = iterators.reduce({}) do |memo, i| 
    memo.merge("iterator#{iterators.index(i)}" => i.flatten)
  end

  properties.merge iters
end

#for_each(selector) ⇒ Object



64
65
66
67
68
# File 'lib/wombat/property_container.rb', line 64

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

#parseObject



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

def parse
  all_properties.each do |p|
    result = yield p if block_given?
    p.result = p.callback ? p.callback.call(result) : result 
  end
end

#to_aryObject



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

def to_ary
end