Class: SimplyCouch::Model::Persistence::TypeCaster

Inherits:
Object
  • Object
show all
Defined in:
lib/simply_couch/model/persistence.rb

Overview

── Type Caster ──────────────────────────────────────────────────────

Instance Method Summary collapse

Instance Method Details

#cast(value, type = nil) ⇒ Object

Cast a value to the given type. Supports:

type: SomeClass           — direct class reference
type: :boolean, :integer  — symbol (lazy, Rails autoloading safe)
type: 'ClassName'         — string (constantize in Rails)


345
346
347
348
349
350
351
352
353
354
# File 'lib/simply_couch/model/persistence.rb', line 345

def cast(value, type = nil)
  return value unless type
  resolved = resolve_type(type)
  # If resolved is a Module (class), try to coerce
  if resolved.is_a?(Module)
    return value if value.is_a?(resolved)
    return cast_to_builtin(value, resolved)
  end
  value
end

#cast_back(value) ⇒ Object



374
375
376
# File 'lib/simply_couch/model/persistence.rb', line 374

def cast_back(value)
  value.respond_to?(:iso8601) ? value.iso8601 : value
end