Sha256: ae4353b04d23cd263e0af37a686f0840011ced86011962aa58b22de725e9a373
Contents?: true
Size: 1.03 KB
Versions: 1
Compression:
Stored size: 1.03 KB
Contents
module Gorillib class Collection module HasCollection def has_collection(clxn_name, type, key_method=:name) plural_name = clxn_name singular_name = Gorillib::Inflector.singularize(clxn_name.to_s).to_sym instance_variable_set("@#{plural_name}", Gorillib::Collection.new(type, key_method)) define_singleton_method(plural_name) do instance_variable_get("@#{plural_name}") if instance_variable_defined?("@#{plural_name}") end define_singleton_method(singular_name) do |item_key, attrs={}, options={}, &block| collection = instance_variable_get("@#{clxn_name}") val = collection.fetch(item_key) do attrs.merge!(key_method => item_key, :owner => self) if attrs.respond_to?(:merge!) factory = options.fetch(:factory){ type } new_val = factory.receive(attrs) collection << new_val new_val end val.instance_exec(&block) if block val end end end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
gorillib-0.4.0pre | lib/gorillib/collection/has_collection.rb |