модуль 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"
Публичные методы экземпляров
# File activerecord/lib/active_record/attribute_methods/before_type_cast.rb, line 60 def attributes_before_type_cast @attributes.values_before_type_cast end
Возвращает хеш атрибутов до приведения типов и десериализации.
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 46 def read_attribute_before_type_cast(attr_name) @attributes[attr_name.to_s].value_before_type_cast 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"
© 2004–2018 David Heinemeier Hansson
Licensed under the MIT License.