Module: Gorillib::Builder

Extended by:
Concern
Includes:
Model
Included in:
Configurable, FancyBuilder
Defined in:
lib/gorillib/builder.rb,
lib/gorillib/builder.rb

Defined Under Namespace

Modules: ClassMethods Classes: GetsetCollectionField, GetsetField, MemberField

Constant Summary collapse

CollectionField =
GetsetCollectionField

Instance Method Summary collapse

Methods included from Concern

append_features, extended, included

Methods included from Model

#==, #as_json, #attribute_set?, #attribute_values, #attributes, #compact_attributes, #handle_extra_attributes, #initialize, #inspect, #inspect_compact, #read_attribute, #read_unset_attribute, #to_json, #to_s, #to_tsv, #to_wire, #unset_attribute, #update_attributes, #write_attribute

Instance Method Details

#collection_of(plural_name) ⇒ Object



96
97
98
# 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) ⇒ Object



70
71
72
# 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) ⇒ Object



18
19
20
21
22
23
24
25
# 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) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# 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) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# 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) ⇒ Boolean

Returns:

  • (Boolean)


80
81
82
# File 'lib/gorillib/builder.rb', line 80

def has_collection_item?(plural_name, item_key)
  collection_of(plural_name).include?(item_key)
end

#key_methodObject



84
85
86
# File 'lib/gorillib/builder.rb', line 84

def key_method
  :name
end

#receive!(*args, &block) ⇒ Object?

Returns the return value of the block, or nil if no block given.

Returns:

  • (Object, nil)

    the return value of the block, or nil if no block given



11
12
13
14
15
16
# 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) ⇒ Object



74
75
76
77
78
# 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_inspectableObject



92
93
94
# File 'lib/gorillib/builder.rb', line 92

def to_inspectable
  super.tap{|attrs| attrs.delete(:owner) }
end

#to_keyObject



88
89
90
# File 'lib/gorillib/builder.rb', line 88

def to_key
  self.send(key_method)
end