Module: Gorillib::Resolution

Extended by:
Concern
Includes:
FancyBuilder
Included in:
Ironfan::Dsl
Defined in:
lib/gorillib/resolution.rb

Overview

The attribute :underlay provides an object (preferably another

Gorillib::Model or the like) that will resolve stacked
defaults. If fields are declared with a :resolver, it will
apply that call in preference the default rules (self.field
-> underlay.field -> self.field.default )

To provide resolve cleanly without read-write loops destroying

the separation of concerns, the resolve mechanism has been
broken from the regular read-write accessors.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#underlayObject

Returns the value of attribute underlay


59
60
61
# File 'lib/gorillib/resolution.rb', line 59

def underlay
  @underlay
end

Instance Method Details

#deep_resolve(field_name) ⇒ Object


83
84
85
86
87
88
89
90
91
92
93
# File 'lib/gorillib/resolution.rb', line 83

def deep_resolve(field_name)
  temp = read_set_or_underlay_attribute(field_name)
  return if temp.nil?
  if temp.is_a? Gorillib::Collection
    result = temp.class.new
    temp.each_pair {|k,v| result[k] = resolve_value(v) }
  else
    result = resolve_value(v)
  end
  result
end

#merge_resolve(field_name) ⇒ Object


101
102
103
104
105
106
107
# File 'lib/gorillib/resolution.rb', line 101

def merge_resolve(field_name)
  field = self.class.fields[field_name] or return
  result = field.type.new
  merge_values(result,read_underlay_attribute(field_name))
  merge_values(result,read_set_attribute(field_name))
  result
end

#merge_values(target, value = nil) ⇒ Object


109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/gorillib/resolution.rb', line 109

def merge_values(target, value=nil)
  value ||= {}
  if target.is_a? Gorillib::Collection
    value.each_pair do |k,v|
      existing = target[k]
      if existing && existing.respond_to?(:receive!)
        target[k].receive! v
      elsif existing && existing.respond_to?(:merge!)
        target[k].merge! v
      else
        target[k] = v
      end
    end
  else
    target.receive! value
  end
end

#read_resolved_attribute(field_name) ⇒ Object


127
128
129
130
# File 'lib/gorillib/resolution.rb', line 127

def read_resolved_attribute(field_name)
  field = self.class.fields[field_name] or return
  self.send(field.resolver, field_name)
end

#read_set_attribute(field_name) ⇒ Object


132
133
134
135
# File 'lib/gorillib/resolution.rb', line 132

def read_set_attribute(field_name)
  attr_name = "@#{field_name}"
  instance_variable_get(attr_name) if instance_variable_defined?(attr_name)
end

#read_set_or_underlay_attribute(field_name) ⇒ Object


142
143
144
145
146
# File 'lib/gorillib/resolution.rb', line 142

def read_set_or_underlay_attribute(field_name)
  result = read_set_attribute(field_name)
  return result unless result.nil?
  read_underlay_attribute(field_name)
end

#read_underlay_attribute(field_name) ⇒ Object


137
138
139
140
# File 'lib/gorillib/resolution.rb', line 137

def read_underlay_attribute(field_name)
  return if underlay.nil?
  Gorillib.deep_copy(underlay.read_resolved_attribute(field_name))
end

#resolveObject

Return a fully-resolved copy of this object. All objects

referenced will be clean deep_copies, and will lack the
:underlay accessor. This is by design, to prevent self-
referential loops (parent->collection->child->owner)
when deep_coping.

66
67
68
69
70
71
72
73
# File 'lib/gorillib/resolution.rb', line 66

def resolve
  result = self.class.new
  self.class.fields.each do |field_name, field|
    value = read_resolved_attribute(field_name)
    result.write_attribute(field_name, value) unless value.nil?
  end
  result
end

#resolve!Object


75
76
77
78
79
80
81
# File 'lib/gorillib/resolution.rb', line 75

def resolve!
  resolved = resolve
  self.class.fields.each do |field_name, field|
    write_attribute(field_name, resolved.send(field_name.to_sym))
  end
  self
end

#resolve_value(value) ⇒ Object


95
96
97
98
99
# File 'lib/gorillib/resolution.rb', line 95

def resolve_value(value)
  return if value.nil?
  return value.resolve if value.respond_to? :resolve
  deep_copy(value)
end