Class: JsonApiClient::Schema::TypeFactory

Inherits:
Object
  • Object
show all
Defined in:
lib/json_api_client/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

JsonApiClient::Schema::TypeFactory.register money: MyMoneyCaster

You can setup several at once:

JsonApiClient::Schema::TypeFactory.register money: MyMoneyCaster,
                                      date: MyJsonDateTypeCaster


80
81
82
# File 'lib/json_api_client/schema.rb', line 80

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

.type_for(type) ⇒ Object



84
85
86
# File 'lib/json_api_client/schema.rb', line 84

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