Module: Arrest::HasAttributes::HasAttributesClassMethods

Defined in:
lib/arrest/attributes/has_attributes.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#fieldsObject

Returns the value of attribute fields.



140
141
142
# File 'lib/arrest/attributes/has_attributes.rb', line 140

def fields
  @fields
end

Instance Method Details

#add_attribute(attribute) ⇒ Object



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/arrest/attributes/has_attributes.rb', line 161

def add_attribute(attribute)
  @fields ||= []
  # define setter for attribute value
  send :define_method, "#{attribute.name}=" do |v|
    converted_v = convert(attribute, v)
    Arrest::debug "setter #{self.class.name} #{attribute.name} = #{converted_v}"

    attribute.dirty = true

    self.attribute_values[attribute.name] = converted_v
  end
  # define getter for attribute value
  send :define_method, "#{attribute.name}" do
    Arrest::debug "getter #{self.class.name} #{attribute.name}"
    self.load_from_stub if @stubbed
    self.attribute_values[attribute.name]
  end
  @fields << attribute
end

#all_fieldsObject



181
182
183
184
# File 'lib/arrest/attributes/has_attributes.rb', line 181

def all_fields
  @all_fields ||=
    self.ancestors.select{|a| a.include?(HasAttributes)}.map(&:fields).flatten.reject{|f| f == nil}
end

#attribute(name, class_name, attribs = {}) ⇒ Object



146
147
148
149
150
151
152
153
# File 'lib/arrest/attributes/has_attributes.rb', line 146

def attribute(name, class_name, attribs = {})
  if !!attribs[:read_only] && !attribs[:actions]
    actions = [:retrieve]
  else
    actions = attribs[:actions]
  end
  add_attribute Attribute.new(name, class_name, actions)
end

#attributes(args) ⇒ Object



155
156
157
158
159
# File 'lib/arrest/attributes/has_attributes.rb', line 155

def attributes(args)
  args.each_pair do |name, clazz|
    self.attribute name, clazz
  end
end

#initializeObject



142
143
144
# File 'lib/arrest/attributes/has_attributes.rb', line 142

def initialize
  @fields = []
end

#nested(name, class_name, options = {}) ⇒ Object



186
187
188
# File 'lib/arrest/attributes/has_attributes.rb', line 186

def nested name, class_name, options = {}
  add_attribute NestedAttribute.new(name, class_name, options[:actions])
end

#nested_array(name, class_name, options = {}) ⇒ Object



190
191
192
# File 'lib/arrest/attributes/has_attributes.rb', line 190

def nested_array name, class_name, options = {}
  add_attribute Arrest::NestedCollection.new(name, class_name, options[:actions])
end