Class: JSONAPI::Consumer::Schema::TypeFactory

Inherits:
Object
  • Object
show all
Defined in:
lib/jsonapi/consumer/schema.rb

Constant Summary collapse

@@types =
{}

Class Method Summary collapse

Class Method Details

.register(type_hash) ⇒ Object

Register a new type key or keys with appropriate classes

eg:

require 'money'

class MyMoneyCaster
   def self.cast(value, default)
      begin
        Money.new(value, "USD")
      rescue ArgumentError
        default
      end
   end
end

JSONAPI::Consumer::Schema::Types.register money: MyMoneyCaster

You can setup several at once:

JSONAPI::Consumer::Schema::Types.register money: MyMoneyCaster,
                                      date: MyJsonDateTypeCaster


79
80
81
# File 'lib/jsonapi/consumer/schema.rb', line 79

def self.register(type_hash)
  @@types.merge!(type_hash)
end

.type_for(type) ⇒ Object



83
84
85
# File 'lib/jsonapi/consumer/schema.rb', line 83

def self.type_for(type)
  @@types[type]
end