Sha256: ee80a144771b674de227b2592a928fc25e072f558799b597ba8d37e833cc6b65

Contents?: true

Size: 1.23 KB

Versions: 6

Compression:

Stored size: 1.23 KB

Contents

module Arrest
  class HasManyCollection #< BasicObject
    def initialize parent, clazz_name
      @parent = parent
      @clazz_name = clazz_name
      @children = nil
      @foreign_key_name = (StringUtils.underscore(@parent.class.name).gsub(/^.*\//, '') + '_id').to_sym
      define_filters
    end

    def build attributes = {}
      extended_attrs = attributes.merge({@foreign_key_name => @parent.id})
      resolved_class.new extended_attrs
    end

    def method_missing(*args, &block)
       children.send(*args, &block)
    end

    def inspect
      children.inspect
    end

    private


    def children
      if @children == nil
        url = @parent.resource_location + '/' + resolved_class.resource_name.to_s
        @children = resolved_class.by_url(url)
      end
      @children
    end

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


    def define_filters
      resolved_class.all_filters.each do |filter|
        self.instance_eval <<-"end_eval"
          def #{filter.name} *args
            real_args = [children] + args
            #{resolved_class.name}.FILTER_#{filter.name}(real_args)
          end
        end_eval
      end
    end

  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
arrest-0.0.16 lib/arrest/helper/has_many_collection.rb
arrest-0.0.15 lib/arrest/helper/has_many_collection.rb
arrest-0.0.14 lib/arrest/helper/has_many_collection.rb
arrest-0.0.13 lib/arrest/helper/has_many_collection.rb
arrest-0.0.12 lib/arrest/helper/has_many_collection.rb
arrest-0.0.11 lib/arrest/helper/has_many_collection.rb