модуль ActiveRecord::AttributeMethods::BeforeTypeCast
Методы атрибутов Active Record до приведения к типу
ActiveRecord::AttributeMethods::BeforeTypeCast предоставляет способ чтения значения атрибутов до приведения к типу и десериализации.
class Task < ActiveRecord::Base
end
task = Task.new(id: '1', completed_on: '2012-10-21')
task.id # => 1
task.completed_on # => Sun, 21 Oct 2012
task.attributes_before_type_cast
# => {"id"=>"1", "completed_on"=>"2012-10-21", ... }
task.read_attribute_before_type_cast('id') # => "1"
task.read_attribute_before_type_cast('completed_on') # => "2012-10-21"
В дополнение к read_attribute_before_type_cast и attributes_before_type_cast, он объявляет метод для всех атрибутов с суффиксом *_before_type_cast.
task.id_before_type_cast # => "1" task.completed_on_before_type_cast # => "2012-10-21"
Публичные методы экземпляра
Возвращает хеш атрибутов до приведения к типу и десериализации.
class Task < ActiveRecord::Base
end
task = Task.new(title: nil, is_done: true, completed_on: '2012-10-21')
task.attributes
# => {"id"=>nil, "title"=>nil, "is_done"=>true, "completed_on"=>Sun, 21 Oct 2012, "created_at"=>nil, "updated_at"=>nil}
task.attributes_before_type_cast
# => {"id"=>nil, "title"=>nil, "is_done"=>true, "completed_on"=>"2012-10-21", "created_at"=>nil, "updated_at"=>nil}
# File activerecord/lib/active_record/attribute_methods/before_type_cast.rb, line 59 def attributes_before_type_cast @attributes end
Возвращает значение атрибута, идентифицированного attr_name до приведения к типу и десериализации.
class Task < ActiveRecord::Base
end
task = Task.new(id: '1', completed_on: '2012-10-21')
task.read_attribute('id') # => 1
task.read_attribute_before_type_cast('id') # => '1'
task.read_attribute('completed_on') # => Sun, 21 Oct 2012
task.read_attribute_before_type_cast('completed_on') # => "2012-10-21"
task.read_attribute_before_type_cast(:completed_on) # => "2012-10-21"
# File activerecord/lib/active_record/attribute_methods/before_type_cast.rb, line 45 def read_attribute_before_type_cast(attr_name) @attributes[attr_name.to_s] end
© 2004–2016 David Heinemeier Hansson
Licensed under the MIT License.