# File lib/gorillib/builder.rb, line 96 def collection_of(plural_name) self.read_attribute(plural_name) end
# File lib/gorillib/builder.rb, line 70 def get_collection_item(plural_name, item_key) collection_of(plural_name)[item_key] end
# File lib/gorillib/builder.rb, line 18 def getset(field, *args, &block) ArgumentError.check_arity!(args, 0..1) if args.empty? read_attribute(field.name) else write_attribute(field.name, args.first) end end
# File lib/gorillib/builder.rb, line 52 def getset_collection_item(field, item_key, attrs={}, &block) plural_name = field.plural_name if attrs.is_a?(field.item_type) # actual object: assign it into collection val = attrs set_collection_item(plural_name, item_key, val) elsif has_collection_item?(plural_name, item_key) # existing item: retrieve it, updating as directed val = get_collection_item(plural_name, item_key) val.receive!(attrs, &block) else # missing item: autovivify item and add to collection val = field.item_type.receive({ key_method => item_key, :owner => self }.merge(attrs), &block) set_collection_item(plural_name, item_key, val) end val end
# File lib/gorillib/builder.rb, line 27 def getset_member(field, *args, &block) ArgumentError.check_arity!(args, 0..1) attrs = args.first if attrs.is_a?(field.type) # actual object: assign it into field val = attrs write_attribute(field.name, val) else val = read_attribute(field.name) if val.present? # existing item: update it with args and block val.receive!(*args, &block) if args.present? or block_given? elsif attrs.blank? and not block_given? # missing item (read): return nil return nil else # missing item (write): construct item and add to collection options = args.extract_options!.merge(:owner => self) val = field.type.receive(*args, options, &block) write_attribute(field.name, val) end end val end
# File lib/gorillib/builder.rb, line 80 def has_collection_item?(plural_name, item_key) collection_of(plural_name).include?(item_key) end
# File lib/gorillib/builder.rb, line 84 def key_method :name end
@return [Object, nil] the return value of the block, or nil if no block given
# File lib/gorillib/builder.rb, line 11 def receive!(*args, &block) super(*args) if block_given? (block.arity == 1) ? block.call(self) : self.instance_eval(&block) end end
# File lib/gorillib/builder.rb, line 74 def set_collection_item(plural_name, item_key, item) collection = collection_of(plural_name) collection[item_key] = item collection[item_key] end
Generated with the Darkfish Rdoc Generator 2.