Class: ActiveRecord::AttributeView

Inherits:
Object
  • Object
show all
Defined in:
lib/active_record/attribute_views.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*attributes) ⇒ AttributeView

Returns a new instance of AttributeView.



5
6
7
# File 'lib/active_record/attribute_views.rb', line 5

def initialize(*attributes)
  self.attributes = attributes
end

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes.



3
4
5
# File 'lib/active_record/attribute_views.rb', line 3

def attributes
  @attributes
end

Instance Method Details

#get(record) ⇒ Object



9
10
11
# File 'lib/active_record/attribute_views.rb', line 9

def get(record)
  load(*attributes.map { |attribute| record.send(attribute) })
end

#load(*values) ⇒ Object

Raises:

  • (NoMethodError)


13
14
15
# File 'lib/active_record/attribute_views.rb', line 13

def load(*values)
  raise NoMethodError, "Please implement the `load' method on your view class"
end

#parse(input) ⇒ Object

Raises:

  • (NoMethodError)


22
23
24
# File 'lib/active_record/attribute_views.rb', line 22

def parse(input)
  raise NoMethodError, "Please implement the `parse' method on your view class"
end

#set(record, input) ⇒ Object



17
18
19
20
# File 'lib/active_record/attribute_views.rb', line 17

def set(record, input)
  parsed = Array(parse(input))
  record.attributes = Hash[*attributes.zip(parsed).flatten]
end