Sha256: ba295f1b801b59e2136c55f7c7a9faf23157aad75d88ecaf323b150fce8d9499

Contents?: true

Size: 1.2 KB

Versions: 5

Compression:

Stored size: 1.2 KB

Contents

#coding: utf-8

module Wombat
  class PropertyContainer < Hash
    attr_accessor :iterators

    def initialize
      @iterators = []
    end

    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

    def to_ary
    end

    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

    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

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

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
wombat-0.2.5 lib/wombat/property_container.rb
wombat-0.2.4 lib/wombat/property_container.rb
wombat-0.2.3 lib/wombat/property_container.rb
wombat-0.2.2 lib/wombat/property_container.rb
wombat-0.2.0 lib/wombat/property_container.rb