Class: JSONAPI::Consumer::Schema::TypeFactory
- Inherits:
-
Object
- Object
- JSONAPI::Consumer::Schema::TypeFactory
- Defined in:
- lib/jsonapi/consumer/schema.rb
Constant Summary collapse
- @@types =
{}
Class Method Summary collapse
-
.register(type_hash) ⇒ Object
Register a new type key or keys with appropriate classes.
- .type_for(type) ⇒ Object
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 |