json_api_client 0.9.0 → 0.9.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5ffee5176ad940141eb0e4b9910c8b1224cc00db
4
- data.tar.gz: 6a1d5756ba8fcf6af81b49bc2da42e42f8186bc3
3
+ metadata.gz: bae5dacd58847759a00a4bb8ffd01e34d7752957
4
+ data.tar.gz: 2815de325108b31b33ca49a4d2ff826ad2b7b0c5
5
5
  SHA512:
6
- metadata.gz: d33f9b77fc8f22c002362e5eebc3f329d1c0038a9dce51e6d260555d2936d458f6d04b3e6d56f02f89880eacff61363d1a74f78d419d57861a86f3d142379674
7
- data.tar.gz: 23c4bd14d220bee9d32ced86ebda585d64620cfbe65d55dc9aa1212541e393fb0b3d572c09f86c770ce42f337f735c0dfc935d63dd8e4664134d4205c598c190
6
+ metadata.gz: e60688cf07d575fc8984967c4368f36ee0810519ed7501ffd433d496d27366f5c8d64bfc4c0f8bdf59513ccf42cd3a5f56c801e060f9a4efdb564649fc1a13f4
7
+ data.tar.gz: 953fad49c530d284756742cdeba6128b196e58a5321060d21fd77269d49c8a22bdd7accb3ec6e814c8b56d0388fd35c0eb6f55e91c907bd697fc67086f243bef
@@ -26,7 +26,7 @@ module JsonApiClient
26
26
  end
27
27
 
28
28
  def persisted?
29
- attributes.has_key?(primary_key)
29
+ attributes[primary_key].present?
30
30
  end
31
31
 
32
32
  def query_params
@@ -1,23 +1,40 @@
1
+ require 'date'
2
+
1
3
  module JsonApiClient
2
4
  class Schema
5
+ DEFAULT_PROPERTY_TYPES = {
6
+ int: lambda {|value| value.to_i },
7
+ integer: lambda {|value| value.to_i },
8
+ string: lambda {|value| value.to_s },
9
+ float: lambda {|value| value.to_f },
10
+ boolean: lambda {|value| value.is_a?(String) ? (value != "false") : !!value },
11
+ timestamp: lambda {|value| value.is_a?(DateTime) ? value : Time.at(value.to_f).to_datetime },
12
+ timestamp_ms: lambda {|value| value.is_a?(DateTime) ? value : Time.at(value.to_f/1000).to_datetime },
13
+ datetime: lambda {|value| value.is_a?(DateTime) ? value : DateTime.parse(value.to_s) },
14
+ date: lambda {|value| value.is_a?(Date) ? value : Date.parse(value.to_s) }
15
+ }
16
+
17
+ class << self
18
+ def property_types
19
+ @property_types ||= DEFAULT_PROPERTY_TYPES
20
+ end
21
+
22
+ def register_property_type(name, caster)
23
+ property_types[name.to_sym] = caster
24
+ end
25
+
26
+ def find_property_type(name)
27
+ property_types[name.to_sym]
28
+ end
29
+ end
30
+
3
31
  Property = Struct.new(:name, :type, :default) do
4
32
  def cast(value)
5
33
  return nil if value.nil?
6
34
  return value if type.nil?
7
35
 
8
- case type.to_sym
9
- when :int, :integer
10
- value.to_i
11
- when :string
12
- value.to_s
13
- when :float
14
- value.to_f
15
- when :boolean
16
- if value.is_a?(String)
17
- value == "false" ? false : true
18
- else
19
- !!value
20
- end
36
+ if caster = Schema.find_property_type(type)
37
+ caster.call(value)
21
38
  else
22
39
  value
23
40
  end
@@ -1,3 +1,3 @@
1
1
  module JsonApiClient
2
- VERSION = "0.9.0"
2
+ VERSION = "0.9.2"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: json_api_client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.0
4
+ version: 0.9.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jeff Ching
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-05-06 00:00:00.000000000 Z
11
+ date: 2015-07-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport