Sha256: 9ecfac8452e8629e456b6ab6c180890218c9f24c1eeadedc55aee058ba939206

Contents?: true

Size: 899 Bytes

Versions: 22

Compression:

Stored size: 899 Bytes

Contents

require 'xommelier'

module Xommelier
  class Collection < Hash
    def initialize(klass)
      @klass = klass
    end

    def respond_to?(name)
      key?(name) || super(name)
    end

    def find_and_append(name, options = {}, &block)
      item = self[name]
      item.options = options
      if block_given?
        item.scoped(&block)
      end
      item
    end

    def method_missing(name, options = {}, &block)
      if key?(name)
        find_and_append(name, options, &block)
      else
        super
      end
    end

    def find_or_create(name, options = {}, &block)
      if key?(name)
        find_and_append(name, options, &block)
      else
        @klass.new(name, options, &block)
      end
    end

    def inspect
      values.map { |value| value.inspect }.inspect
    end

    def pretty_inspect
      values.map { |value| value.inspect }.pretty_inspect
    end
  end
end

Version data entries

22 entries across 22 versions & 1 rubygems

Version Path
xommelier-0.1.20 lib/xommelier/collection.rb
xommelier-0.1.19 lib/xommelier/collection.rb
xommelier-0.1.18 lib/xommelier/collection.rb
xommelier-0.1.16 lib/xommelier/collection.rb
xommelier-0.1.15 lib/xommelier/collection.rb
xommelier-0.1.14 lib/xommelier/collection.rb
xommelier-0.1.13 lib/xommelier/collection.rb
xommelier-0.1.12 lib/xommelier/collection.rb
xommelier-0.1.11 lib/xommelier/collection.rb
xommelier-0.1.10 lib/xommelier/collection.rb
xommelier-0.1.9 lib/xommelier/collection.rb
xommelier-0.1.8 lib/xommelier/collection.rb
xommelier-0.1.7 lib/xommelier/collection.rb
xommelier-0.1.6 lib/xommelier/collection.rb
xommelier-0.1.5 lib/xommelier/collection.rb
xommelier-0.1.4 lib/xommelier/collection.rb
xommelier-0.1.3 lib/xommelier/collection.rb
xommelier-0.1.2 lib/xommelier/collection.rb
xommelier-0.1.1 lib/xommelier/collection.rb
xommelier-0.1.0 lib/xommelier/collection.rb