Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow schema types to be pluggable #228

Merged
merged 1 commit into from
Jan 18, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,36 @@ class MyApi::Base < JsonApiClient::Resource
end
```


### Type Casting

You can define your own types and its casting mechanism for schema.

```ruby
require 'money'
class MyMoneyCaster
def self.cast(value, default)
begin
Money.new(1000, "USD")
rescue ArgumentError
default
end
end
end

JsonApiClient::Schema.register money: MyMoneyCaster

```
and finally

```ruby
class Order < JsonApiClient::Resource
property :total_amount, type: :money
end

```


## Changelog

See [changelog](https://github.com/chingor13/json_api_client/blob/master/CHANGELOG.md)
123 changes: 102 additions & 21 deletions lib/json_api_client/schema.rb
Original file line number Diff line number Diff line change
@@ -1,36 +1,108 @@
require 'bigdecimal'
module JsonApiClient
class Schema
Property = Struct.new(:name, :type, :default) do
def cast(value)
return nil if value.nil?
return value if type.nil?
module Types

case type.to_sym
when :int, :integer
class Integer
def self.cast(value, _)
value.to_i
when :string
end
end

class String
def self.cast(value, _)
value.to_s
when :float
end
end

class Float
def self.cast(value, _)
value.to_f
when :time
value.is_a?(Time) ? value : Time.parse(value.to_s)
when :decimal
end
end

class Time
def self.cast(value, _)
value.is_a?(::Time) ? value : ::Time.parse(value)
end
end

class Decimal
def self.cast(value, _)
BigDecimal.new(value)
when :boolean
end
end

class Boolean
def self.cast(value, default)
case value
when "false", "0", 0, false
false
when "true", "1", 1, true
true
else
# if it's unknown, use the default value
default
when "false", "0", 0, false
false
when "true", "1", 1, true
true
else
# if it's unknown, use the default value
default
end
else
value
end
end

end

class TypeFactory
@@types = {}
# Register a new type key or keys with appropriate classes
#
# eg:
#
# require 'money'
#
# class MyMoneyCaster
# def self.cast(value, default)
# begin
# Money.new(1000, "USD")
# rescue ArgumentError
# default
# end
# end
# end
#
# JsonApiClient::Schema::Types.register money: MyMoneyCaster
#
# You can setup several at once:
#
# JsonApiClient::Schema::Types.register money: MyMoneyCaster,
# date: MyJsonDateTypeCaster
#
#
#
#
def self.register(type_hash)
@@types.merge!(type_hash)
end

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

self.register int: Types::Integer,
integer: Types::Integer,
string: Types::String,
float: Types::Float,
time: Types::Time,
decimal: Types::Decimal,
boolean: Types::Boolean

end

Property = Struct.new(:name, :type, :default) do
def cast(value)
return nil if value.nil?
return value if type.nil?
type_caster = TypeFactory.type_for(type)
return value if type_caster.nil?
type_caster.cast(value, default)
end
end

def initialize
Expand All @@ -54,11 +126,13 @@ def add(name, options)
def size
@properties.size
end

alias_method :length, :size

def each_property(&block)
@properties.values.each(&block)
end

alias_method :each, :each_property

# Look up a property by name
Expand All @@ -68,6 +142,13 @@ def each_property(&block)
def find(property_name)
@properties[property_name.to_sym]
end

alias_method :[], :find

class << self
def register(type_hash)
TypeFactory.register(type_hash)
end
end
end
end
18 changes: 18 additions & 0 deletions test/unit/schemable_test.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
require 'test_helper'

class CustomTypeCaster
def self.cast(*)
:mock
end
end

JsonApiClient::Schema.register custom: CustomTypeCaster

class SchemaResource < TestResource
property :a, type: :string, default: 'foo'
property :b, type: :boolean, default: false
Expand All @@ -11,6 +19,10 @@ class SchemaResource2 < TestResource
property :a, type: :float
end

class SchemaResource3 < TestResource
property :a, type: :custom
end

class MultipleSchema < TestResource
properties :name, :short_name, :long_name, type: :string
end
Expand Down Expand Up @@ -131,4 +143,10 @@ def test_boolean_defaults_to_default
assert_equal false, resource.b
end

def test_custom_types
resource = SchemaResource3.new(a: 'anything')
assert_equal :mock, resource.a
assert_equal :mock, resource['a']
end

end