requisite 0.2.1 → 0.3.0

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: 0900379bbe4e112f2e983138cba06a3f4c399d8b
4
- data.tar.gz: 30ee7ec731114380f827676e2e881510b860d38e
3
+ metadata.gz: 718f7a0ac2ca20c100cf6e5c119c4a9b4bafd5f4
4
+ data.tar.gz: 0da1427c7209923071d6667610ddc6473a2464fa
5
5
  SHA512:
6
- metadata.gz: 3d979fd487435e6ef9b025db1d4ed6f7d09a6c897a1bf2f440bd19e4328f8b0e5ca663a2582cba8827a4bc5fcc0cfbebb959f18e3a6621ba2339228ac378bcbd
7
- data.tar.gz: b4b83bf22163cc36b6dbd144cafbacf9f803e52076b9a969e001c167f608d387a394e8d124260379de44c5c27b55c5992a0acb0fe73e7d7151170a9f7d7e72e5
6
+ metadata.gz: c5b84bf2d04e682405cb7aac06bb6ef786b12c6976cda1bed69fa7aefbba941b4e97e6bbe8ca0c0bf3e84cab14d3ef637a65cbf5bdade19dfeac39c9e373b761
7
+ data.tar.gz: 5c218d74d921330a280d908bc5cf7683408ad0a32e9685b5f95a99c8961de0fba6f03f8dddbb1e7d7f4411ef5302e203de045bf7361cd5291777c8f1e886adb7
data/CHANGES.md CHANGED
@@ -1,3 +1,6 @@
1
+ ### 0.3.0 (15th April 2015):
2
+ - Optionally allow nils to be returned
3
+
1
4
  ### 0.2.1 (5th March 2015):
2
5
  - Fix false in hash inputs
3
6
 
data/README.md CHANGED
@@ -57,7 +57,7 @@ user.to_json
57
57
  # => "{\"id\":5,\"real_name\":\"Jamie Osler\",\"username\":\"josler\"}"
58
58
  ```
59
59
 
60
- `nil` values are not returned in the response.
60
+ `nil` values are not returned in the response, unless `to_hash(show_nil: true)` or `to_json(show_nil: true)` are requested.
61
61
 
62
62
  Errors are thrown when a required attribute is not present:
63
63
 
@@ -25,32 +25,22 @@ module Requisite
25
25
  end
26
26
  end
27
27
 
28
- def model_responds_to_attribute_query?(name)
29
- if @model.kind_of?(Hash)
30
- @model[name] != nil
31
- else
32
- @model.send(name) if @model.respond_to?(name)
33
- end
34
- end
35
-
36
- def merge_attribute_if_exists!(to_merge, attribute_name)
37
- attribute_from_model(attribute_name) ? to_merge.merge!(attribute_from_model(attribute_name)) : to_merge
38
- end
39
-
40
- def to_hash
28
+ def to_hash(show_nil: false)
41
29
  preprocess_model
42
30
  {}.tap do |result|
43
31
  self.class.attribute_keys_with_inheritance.each do |meth|
44
32
  value = self.send(meth)
45
- result.merge!({meth => value}) unless value.nil?
33
+ result.merge!({meth => value}) if show_nil || !value.nil?
46
34
  end
47
35
  end
48
36
  end
49
37
 
50
- def to_json
51
- to_hash.to_json
38
+ def to_json(show_nil: false)
39
+ to_hash(show_nil: show_nil).to_json
52
40
  end
53
41
 
42
+ private
43
+
54
44
  def parse_typed_hash(name, hash)
55
45
  {}.tap do |result|
56
46
  passed_hash = attribute_from_model(name)
@@ -98,7 +88,18 @@ module Requisite
98
88
  nil
99
89
  end
100
90
 
101
- private
91
+
92
+ def model_responds_to_attribute_query?(name)
93
+ if @model.kind_of?(Hash)
94
+ @model[name] != nil
95
+ else
96
+ @model.send(name) if @model.respond_to?(name)
97
+ end
98
+ end
99
+
100
+ def merge_attribute_if_exists!(to_merge, attribute_name)
101
+ attribute_from_model(attribute_name) ? to_merge.merge!(attribute_from_model(attribute_name)) : to_merge
102
+ end
102
103
 
103
104
  def preprocess_model
104
105
  # noop
@@ -1,3 +1,3 @@
1
1
  module Requisite
2
- VERSION = '0.2.1'
2
+ VERSION = '0.3.0'
3
3
  end
@@ -63,12 +63,12 @@ module Requisite
63
63
 
64
64
  it 'with_type! helper raises on mismatched type' do
65
65
  model = DummyApiModel.new()
66
- proc { model.with_type!(String) { 1 + 2 }}.must_raise(Requisite::BadTypeError)
66
+ proc { model.send(:with_type!, String) { 1 + 2 }}.must_raise(Requisite::BadTypeError)
67
67
  end
68
68
 
69
69
  it 'first_attribute_from_model helper finds first matching attriubute' do
70
70
  model = DummyApiModel.new(:oh => 12, :a => nil, :b => 'B', :c => 'C')
71
- model.first_attribute_from_model(:a, :b, :c).must_equal('B')
71
+ model.send(:first_attribute_from_model, :a, :b, :c).must_equal('B')
72
72
  end
73
73
 
74
74
  it 'attribute can assert type of a boolean field' do
@@ -83,6 +83,12 @@ module Requisite
83
83
  response.to_hash.must_equal({})
84
84
  end
85
85
 
86
+ it 'attribute includes values of nil if permitted' do
87
+ DummyApiModel.serialized_attributes { attribute :num, type: String }
88
+ response = DummyApiModel.new({:num => nil})
89
+ response.to_hash(show_nil: true).must_equal({:num => nil})
90
+ end
91
+
86
92
  it 'attribute can be stringified and renamed with default fields' do
87
93
  DummyApiModel.serialized_attributes { attribute :my_num, rename: :num, stringify: true, default: 22 }
88
94
  response = DummyApiModel.new
@@ -39,6 +39,11 @@ class InheritedApiUser < ApiUser
39
39
  end
40
40
  end
41
41
 
42
+ class UserModel
43
+ attr_accessor :id, :user_id, :email, :name, :created_at, :created, :last_seen_user_agent, :last_request_at, :unsubscribed_from_emails
44
+ attr_accessor :update_last_request_at, :new_session, :custom_attributes, :company, :companies
45
+ end
46
+
42
47
  module Requisite
43
48
  describe ApiUser do
44
49
  it 'accepts a user' do
@@ -111,5 +116,41 @@ module Requisite
111
116
  :new_attribute => 'hi'
112
117
  })
113
118
  end
119
+
120
+ it 'accepts a user model' do
121
+ user_model = UserModel.new
122
+ user_model.user_id = 'abcdef'
123
+ user_model.name = 'Bob'
124
+ user = ApiUser.new(user_model)
125
+ user.to_hash.must_equal({
126
+ :user_id => 'abcdef',
127
+ :name => 'Bob',
128
+ :custom_data => {}
129
+ })
130
+ user.name.must_equal('Bob')
131
+ end
132
+
133
+ it 'accepts a user model and renders nils if asked' do
134
+ user_model = UserModel.new
135
+ user_model.user_id = 'abcdef'
136
+ user_model.name = 'Bob'
137
+ user = ApiUser.new(user_model)
138
+ user.to_hash(show_nil: true).must_equal({
139
+ :id => nil,
140
+ :user_id => 'abcdef',
141
+ :email => nil,
142
+ :name => 'Bob',
143
+ :created_at => nil,
144
+ :last_seen_user_agent => nil,
145
+ :last_request_at => nil,
146
+ :unsubscribed_from_emails => nil,
147
+ :update_last_request_at => nil,
148
+ :new_session => nil,
149
+ :custom_data => {},
150
+ :company => nil,
151
+ :companies => nil
152
+ })
153
+ user.name.must_equal('Bob')
154
+ end
114
155
  end
115
156
  end
metadata CHANGED
@@ -1,38 +1,38 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: requisite
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - James Osler
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-03-05 00:00:00.000000000 Z
11
+ date: 2015-04-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: minitest
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - '>='
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
19
  version: '0'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - '>='
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
- description: ' Requisite is an elegant way of strongly defining request and response
28
- models for serialization '
27
+ description: " Requisite is an elegant way of strongly defining request and response
28
+ models for serialization "
29
29
  email:
30
30
  - jamie@intercom.io
31
31
  executables: []
32
32
  extensions: []
33
33
  extra_rdoc_files: []
34
34
  files:
35
- - .gitignore
35
+ - ".gitignore"
36
36
  - CHANGES.md
37
37
  - Gemfile
38
38
  - LICENCE.txt
@@ -60,17 +60,17 @@ require_paths:
60
60
  - lib
61
61
  required_ruby_version: !ruby/object:Gem::Requirement
62
62
  requirements:
63
- - - '>='
63
+ - - ">="
64
64
  - !ruby/object:Gem::Version
65
65
  version: '0'
66
66
  required_rubygems_version: !ruby/object:Gem::Requirement
67
67
  requirements:
68
- - - '>='
68
+ - - ">="
69
69
  - !ruby/object:Gem::Version
70
70
  version: '0'
71
71
  requirements: []
72
72
  rubyforge_project:
73
- rubygems_version: 2.0.14
73
+ rubygems_version: 2.2.2
74
74
  signing_key:
75
75
  specification_version: 4
76
76
  summary: Strongly defined models for HTTP APIs
@@ -79,3 +79,4 @@ test_files:
79
79
  - test/requisite/api_model_test.rb
80
80
  - test/requisite/api_user_test.rb
81
81
  - test/test_helper.rb
82
+ has_rdoc: