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

Returns a new instance of ModelType.



182
183
184
185
# File 'lib/diva/type.rb', line 182

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

Instance Attribute Details

#modelObject (readonly)

Returns the value of attribute model.



180
181
182
# File 'lib/diva/type.rb', line 180

def model
  @model
end

Instance Method Details

#cast(value) ⇒ Object



191
192
193
194
195
196
197
198
199
200
# File 'lib/diva/type.rb', line 191

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

#recommendation_point(value) ⇒ Object



187
188
189
# File 'lib/diva/type.rb', line 187

def recommendation_point(value)
  value.is_a?(model) && 0
end

#schemaObject



202
203
204
# File 'lib/diva/type.rb', line 202

def schema
  @schema ||= { type: uri }.freeze
end

#to_sObject



206
207
208
# File 'lib/diva/type.rb', line 206

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

#uriObject



210
211
212
# File 'lib/diva/type.rb', line 210

def uri
  model.uri
end