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.



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

def fields
  @fields
end

Instance Method Details

#add_attribute(attribute) ⇒ Object



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

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



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

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

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



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

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

#attributes(args) ⇒ Object



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

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

#initializeObject



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

def initialize
  @fields = []
end

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



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

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

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



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

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