Class: ActiveModel::Type::Model
- Inherits:
-
Value
- Object
- Value
- ActiveModel::Type::Model
- Defined in:
- lib/facio/active_model/type/model.rb
Instance Attribute Summary collapse
-
#class_name ⇒ Object
readonly
Returns the value of attribute class_name.
Instance Method Summary collapse
- #cast(value) ⇒ Object
-
#initialize(**options) ⇒ Model
constructor
A new instance of Model.
Constructor Details
#initialize(**options) ⇒ Model
Returns a new instance of Model.
5 6 7 8 |
# File 'lib/facio/active_model/type/model.rb', line 5 def initialize(**) @class_name = .delete(:class_name) super() end |
Instance Attribute Details
#class_name ⇒ Object (readonly)
Returns the value of attribute class_name.
4 5 6 |
# File 'lib/facio/active_model/type/model.rb', line 4 def class_name @class_name end |
Instance Method Details
#cast(value) ⇒ Object
10 11 12 13 14 15 16 17 18 19 |
# File 'lib/facio/active_model/type/model.rb', line 10 def cast(value) return nil if value.nil? klass = class_name.constantize if value.is_a?(::Integer) || value.is_a?(::String) klass.find(value) elsif value.is_a?(klass) value end end |