Sha256: d7e833ab22bbc5f18e5639bcd3de7f494c660e8cc5fb6976613312a3b1d9f205

Contents?: true

Size: 843 Bytes

Versions: 15

Compression:

Stored size: 843 Bytes

Contents

module Arrest
  class ChildCollection #< BasicObject
    def initialize parent, clazz_name
      @parent = parent
      @clazz_name = clazz_name
      @children = nil
    end

    def build attributes = {}
      resolved_class.new @parent, attributes
    end

    def method_missing(*args, &block)
     if resolved_class.respond_to?(args[0])
       sub_args = [@parent]
       sub_args += args.drop(1)
       resolved_class.send(args[0], *sub_args)
     else
       children.send(*args, &block)
     end
    end

    def inspect
      children.inspect
    end

    private


    def children
      if @children == nil
        @children = resolved_class.all_for @parent
      end
      @children
    end

    def resolved_class
      if @clazz == nil
        @clazz = Source.mod.const_get(@clazz_name)
      end
      @clazz
    end

  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
arrest-0.0.25 lib/arrest/helper/child_collection.rb
arrest-0.0.24 lib/arrest/helper/child_collection.rb
arrest-0.0.23 lib/arrest/helper/child_collection.rb
arrest-0.0.21 lib/arrest/helper/child_collection.rb
arrest-0.0.20 lib/arrest/helper/child_collection.rb
arrest-0.0.19 lib/arrest/helper/child_collection.rb
arrest-0.0.18 lib/arrest/helper/child_collection.rb
arrest-0.0.17 lib/arrest/helper/child_collection.rb
arrest-0.0.16 lib/arrest/helper/child_collection.rb
arrest-0.0.15 lib/arrest/helper/child_collection.rb
arrest-0.0.14 lib/arrest/helper/child_collection.rb
arrest-0.0.13 lib/arrest/helper/child_collection.rb
arrest-0.0.12 lib/arrest/helper/child_collection.rb
arrest-0.0.11 lib/arrest/helper/child_collection.rb
arrest-0.0.9 lib/arrest/helper/child_collection.rb