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.



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

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



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

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(v) ⇒ Object



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

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

#schemaObject



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

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

#to_sObject



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

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

#uriObject



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

def uri
  model.uri
end