Class: ActiveModel::Type::Model

Inherits:
Value
  • Object
show all
Defined in:
lib/facio/active_model/type/model.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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(**options)
  @class_name = options.delete(:class_name)
  super()
end

Instance Attribute Details

#class_nameObject (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