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.



119
120
121
# File 'lib/arrest/attributes/has_attributes.rb', line 119

def fields
  @fields
end

Instance Method Details

#add_attribute(attribute) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/arrest/attributes/has_attributes.rb', line 136

def add_attribute(attribute)
  @fields ||= []
  # define setter for attribute value
  if (attribute.is_a?(HasManySubResourceAttribute))
    send :define_method, "#{attribute.name}=" do |v|
      raise ArgumentError, 'Argument is not of Array type' unless v.is_a?(Array)
      Arrest::debug "setter #{self.class.name} #{attribute.name} = #{v}"

      # inform ActiveModel::Dirty about dirtiness of this attribute
      self.send("#{attribute.name}_will_change!") unless v == self.attribute_values[attribute.name]

      self.attribute_values[attribute.name] = v
    end
  else
    send :define_method, "#{attribute.name}=" do |v|
      Arrest::debug "setter #{self.class.name} #{attribute.name} = #{v}"
      self.attribute_values[attribute.name] = v
    end
  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



165
166
167
168
169
170
171
172
173
174
# File 'lib/arrest/attributes/has_attributes.rb', line 165

def all_fields
  self_fields = self.fields
  self_fields ||= []
  if self.superclass.respond_to?('fields') && self.superclass.all_fields != nil
    res = self_fields + self.superclass.all_fields
  else
    res = self_fields
  end
  res
end

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



125
126
127
128
# File 'lib/arrest/attributes/has_attributes.rb', line 125

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

#attributes(args) ⇒ Object



130
131
132
133
134
# File 'lib/arrest/attributes/has_attributes.rb', line 130

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

#initializeObject



121
122
123
# File 'lib/arrest/attributes/has_attributes.rb', line 121

def initialize
  @fields = []
end

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



176
177
178
179
# File 'lib/arrest/attributes/has_attributes.rb', line 176

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

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



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

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