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.



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

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

Instance Attribute Details

#modelObject (readonly)

Returns the value of attribute model.



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

def model
  @model
end

Instance Method Details

#cast(value) ⇒ Object



195
196
197
198
199
200
201
202
203
204
# File 'lib/diva/type.rb', line 195

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



191
192
193
# File 'lib/diva/type.rb', line 191

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

#schemaObject



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

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

#to_sObject



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

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

#uriObject



214
215
216
# File 'lib/diva/type.rb', line 214

def uri
  model.uri
end