Class: Diva::Type::ModelType

Inherits:
MetaType show all
Defined in:
lib/diva/type.rb

Instance Attribute Summary collapse

Attributes inherited from MetaType

#name

Instance Method Summary collapse

Methods inherited from MetaType

#dump_for_json, #inspect

Constructor Details

#initialize(model, *rest, &cast) ⇒ ModelType



152
153
154
155
# File 'lib/diva/type.rb', line 152

def initialize(model, *rest, &cast)
  super(:model, *rest)
  @model = model
end

Instance Attribute Details

#modelObject (readonly)

Returns the value of attribute model.



151
152
153
# File 'lib/diva/type.rb', line 151

def model
  @model
end

Instance Method Details

#cast(value) ⇒ Object



157
158
159
160
161
162
163
164
165
166
# File 'lib/diva/type.rb', line 157

def cast(value)
  case value
  when model
    value
  when Hash
    model.new(value)
  else
    raise Diva::InvalidTypeError, "The value #{value.inspect} is not a `#{model}'."
  end
end

#to_sObject



168
169
170
# File 'lib/diva/type.rb', line 168

def to_s
  "#{model} #{name}"
end