fson 0.0.11 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ae2acd60f2606d5013fc3cbd7ae728a1052aa782
4
- data.tar.gz: a8d364a84ba246aca70399c8a054a7c8bd7f0817
3
+ metadata.gz: 15d93512d2c5ad431642446ccb1a97a58c826a6e
4
+ data.tar.gz: 668890d610dd29e9198abf1a08ad4029c8f4ede9
5
5
  SHA512:
6
- metadata.gz: 90ed4795d896d061d09553e09f13c09ae6dbe259d598a8ef7ed7cea680db38fcafaab0a45c1b36f7a3c39e2ca6b6fd297238323f4cc88e2305ca579b09a90d5b
7
- data.tar.gz: 8eac147e359441f279957754a31cda214574642a47aa4e47a060a4acfa3d376be6317fd13421b560debd80a0321daf07428b0142a89ca8a77a84ef1617f38724
6
+ metadata.gz: e0d1921160f240aabeb0de4384012872bc2d13c413988307723a76d63367f5a59e23870ea9d62b3621497f9c5e18c54cf5eb835b43f2783264fb0eec83dd77bf
7
+ data.tar.gz: de5a76ed2782982be1d7b3795ff0acf8bbebc32c9c5db83a6c4d0481c202358496a198aa5bb6aac02daa0e1678424fdc70e4897b8b894f1650c2c8b3464835a6
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- fson (0.0.11)
4
+ fson (1.0.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -11,7 +11,7 @@ GEM
11
11
  diff-lcs (1.2.5)
12
12
  docile (1.1.5)
13
13
  json (1.8.3)
14
- oj (2.12.10)
14
+ oj (2.12.11)
15
15
  rake (10.4.2)
16
16
  rspec (3.3.0)
17
17
  rspec-core (~> 3.3.0)
data/README.md CHANGED
@@ -39,10 +39,22 @@ Fson::Response.error # {"status": "error"}
39
39
  Fson::Response.fail # {"status": "fail"}
40
40
  ```
41
41
 
42
- then add some data by passing hash
42
+ then add some data explicitly
43
43
 
44
44
  ```ruby
45
- .data([{:id => 12}])
45
+ .data_array([{:id => 12}])
46
+ ```
47
+
48
+ ```json
49
+ {
50
+ "data": [{
51
+ "id": 12
52
+ }]
53
+ }
54
+ ```
55
+
56
+ ```ruby
57
+ .data_hash({:id => 12})
46
58
  ```
47
59
 
48
60
  ```json
@@ -52,15 +64,30 @@ then add some data by passing hash
52
64
  }
53
65
  }
54
66
  ```
67
+
55
68
 
56
- or defining block
69
+ or by defining block
57
70
 
58
71
  ```ruby
59
- .data { |data|
72
+ .data_array { |data|
60
73
  data << {:id => 12}
61
74
  }
62
75
  ```
63
76
 
77
+ ```json
78
+ {
79
+ "data": [{
80
+ "id": 12
81
+ }]
82
+ }
83
+ ```
84
+
85
+ ```ruby
86
+ .data_hash { |data|
87
+ data[:id] => 12
88
+ }
89
+ ```
90
+
64
91
  ```json
65
92
  {
66
93
  "data": {
@@ -102,7 +129,7 @@ and finally get JSON with
102
129
  Builder chain
103
130
 
104
131
  ```ruby
105
- Fson::Response.fail.data {|data| data << {:id => 12}}.add_error('not authorized').as_json
132
+ Fson::Response.fail.data_array {|data| data << {:id => 12}}.add_error('not authorized').as_json
106
133
  ```
107
134
 
108
135
  will return
@@ -142,6 +169,7 @@ _response # returns response hash
142
169
  _errors # returns errors hash
143
170
  _data # returns data hash
144
171
  _initialized_data_array # returns existing data array or initializes it with empty array
172
+ _initialized_data_hash # returns existing data hash or initializes it with empty hash
145
173
  ```
146
174
 
147
175
  For example you can add builder
@@ -163,7 +191,9 @@ by registering it in initializer
163
191
  ```ruby
164
192
  require 'fson/loader'
165
193
 
166
- ::Fson::Loader::configure([MyCustomBuilder])
194
+ ActionDispatch::Callbacks.to_prepare do
195
+ ::Fson::Loader::configure([MyCustomBuilder])
196
+ end
167
197
  ```
168
198
 
169
199
  ## Contributing
data/lib/fson/builder.rb CHANGED
@@ -5,15 +5,12 @@ module Fson
5
5
  # Builder Methods
6
6
  ##
7
7
 
8
- def data(data = nil, &block)
9
- _initialized_data_array
10
-
11
- if data and data.is_a?(Array)
12
- @_data = data
13
- end
8
+ def data_hash(data = nil, &block)
9
+ data(data, Hash, &block)
10
+ end
14
11
 
15
- yield(_initialized_data_array()) if block_given?
16
- self
12
+ def data_array(data = nil, &block)
13
+ data(data, Array, &block)
17
14
  end
18
15
 
19
16
  def status(status)
@@ -52,6 +49,18 @@ module Fson
52
49
 
53
50
  private
54
51
 
52
+ def data(data = nil, type = nil, &block)
53
+ self.send("_initialized_data_#{type.to_s.downcase}")
54
+
55
+ if data
56
+ raise 'Invalid Argument Error' unless data.is_a?(type)
57
+ @_data = data
58
+ end
59
+
60
+ yield(@_data) if block_given?
61
+ self
62
+ end
63
+
55
64
  def _data
56
65
  @_data
57
66
  end
@@ -65,7 +74,19 @@ module Fson
65
74
  end
66
75
 
67
76
  def _initialized_data_array
77
+ if @_data.is_a?(Hash)
78
+ raise 'Invalid State Error: response data already initialized as a Hash'
79
+ end
80
+
68
81
  @_data ||= []
69
82
  end
83
+
84
+ def _initialized_data_hash
85
+ if @_data.is_a?(Array)
86
+ raise 'Invalid State Error: response data already initialized as an Array'
87
+ end
88
+
89
+ @_data ||= {}
90
+ end
70
91
  end
71
92
  end
data/lib/fson/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Fson
2
- VERSION = '0.0.11'
2
+ VERSION = '1.0.0'
3
3
  end
@@ -8,7 +8,7 @@ describe ::Fson::Response do
8
8
  # given
9
9
  response = ::Fson::Response.error
10
10
  .add_error('invalid') { |e| e[:id] = 'text-set-1'}
11
- .data { |data| data << {:author => 'Mateusz Kluczny'} }
11
+ .data_array { |data| data << {:author => 'Mateusz Kluczny'} }
12
12
 
13
13
  # when/then
14
14
  expect(response.as_json).to eq("{\"status\":\"error\",\"data\":[{\"author\":\"Mateusz Kluczny\"}],\"errors\":[{\"message\":\"invalid\",\"id\":\"text-set-1\"}]}")
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fson
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.11
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mateusz Kluczny
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-07-29 00:00:00.000000000 Z
11
+ date: 2015-08-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec