module Gorillib::Builder

Constants

CollectionField

Public Instance Methods

collection_of(plural_name) click to toggle source
# File lib/gorillib/builder.rb, line 96
def collection_of(plural_name)
  self.read_attribute(plural_name)
end
get_collection_item(plural_name, item_key) click to toggle source
# File lib/gorillib/builder.rb, line 70
def get_collection_item(plural_name, item_key)
  collection_of(plural_name)[item_key]
end
getset(field, *args, &block) click to toggle source
# 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
getset_collection_item(field, item_key, attrs={}, &block) click to toggle source
# 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
getset_member(field, *args, &block) click to toggle source
# 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
has_collection_item?(plural_name, item_key) click to toggle source
# File lib/gorillib/builder.rb, line 80
def has_collection_item?(plural_name, item_key)
  collection_of(plural_name).include?(item_key)
end
key_method() click to toggle source
# File lib/gorillib/builder.rb, line 84
def key_method
  :name
end
receive!(*args, &block) click to toggle source

@return [Object, nil] the return value of the block, or nil if no block given

Calls superclass method Gorillib::Model#receive!
# 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
set_collection_item(plural_name, item_key, item) click to toggle source
# 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
to_inspectable() click to toggle source
Calls superclass method Gorillib::Model#to_inspectable
# File lib/gorillib/builder.rb, line 92
def to_inspectable
  super.tap{|attrs| attrs.delete(:owner) }
end
to_key() click to toggle source
# File lib/gorillib/builder.rb, line 88
def to_key
  self.send(key_method)
end