json-encodable 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +5 -2
- data/README.md +4 -4
- data/lib/json/encodable.rb +4 -6
- data/lib/json/encodable/version.rb +1 -1
- data/spec/json/encodable_spec.rb +25 -3
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 60320d82f60c457b9360b943d1c73cff3fc4b61e
|
4
|
+
data.tar.gz: 32bac5f41d30df13ccd1f24b00f0738a6b2e81d5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d21247643d7c2f7d70ff302b131f626b7061b71644988f440a1704a45942ad2969021a07486be2f432470af884ef97f557aaf293b6f2b8e37a981ce81ad0ec77
|
7
|
+
data.tar.gz: 913b8affcdd2804cba377121934e3731c28106db44206f184af28c3d09e172e35f2e7c50f2d2ef856f9b6f7980f9fc8857171541d4d7f4042af8f3443befb58e
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -4,7 +4,7 @@ Make a class encodable into JSON format.
|
|
4
4
|
## Usage
|
5
5
|
1. Include `JSON::Encodable` module
|
6
6
|
2. Call `.property` method with property name
|
7
|
-
3. Then the instance will be able to respond to
|
7
|
+
3. Then the instance will be able to respond to `to_json` method
|
8
8
|
|
9
9
|
### #to_json
|
10
10
|
```ruby
|
@@ -37,8 +37,8 @@ You can also call `.as_json` method with `:except` and `:only` options.
|
|
37
37
|
|
38
38
|
```ruby
|
39
39
|
Blog.new.as_json(only: [:id, :username])
|
40
|
-
#=> {"id"
|
40
|
+
#=> { "id" => 1, "username" => "alice" }
|
41
41
|
|
42
|
-
Blog.new.as_json(except:
|
43
|
-
#=> {"id"
|
42
|
+
Blog.new.as_json(except: :username)
|
43
|
+
#=> { "id" => 1, "title" => "wonderland" }
|
44
44
|
```
|
data/lib/json/encodable.rb
CHANGED
@@ -1,7 +1,5 @@
|
|
1
1
|
require "active_support/concern"
|
2
|
-
require "active_support/core_ext/hash/reverse_merge"
|
3
2
|
require "active_support/core_ext/object/json"
|
4
|
-
require "active_support/core_ext/string/inflections"
|
5
3
|
require "active_support/json"
|
6
4
|
|
7
5
|
require "json/encodable/version"
|
@@ -60,8 +58,8 @@ module JSON
|
|
60
58
|
|
61
59
|
# @note All Time values will be encoded in ISO 8601 format
|
62
60
|
# @param options [Hash] Options for .property_names method
|
63
|
-
# @option options [Array<Symbol>] :except Property names to exclude properties
|
64
|
-
# @option options [Array<Symbol>] :only Property names to filter properties
|
61
|
+
# @option options [Symbol, Array<Symbol>] :except Property names to exclude properties
|
62
|
+
# @option options [Symbol, Array<Symbol>] :only Property names to filter properties
|
65
63
|
# @return [Hash{Symbol => Object}] Properties as a key-value pairs Hash
|
66
64
|
# @example
|
67
65
|
# {
|
@@ -72,8 +70,8 @@ module JSON
|
|
72
70
|
# }
|
73
71
|
def properties(options = {})
|
74
72
|
names = self.class.property_names
|
75
|
-
names = names - options[:except] if options[:except]
|
76
|
-
names = names & options[:only] if options[:only]
|
73
|
+
names = names - Array(options[:except]) if options[:except]
|
74
|
+
names = names & Array(options[:only]) if options[:only]
|
77
75
|
names.inject({}) do |hash, property_name|
|
78
76
|
key = property_name
|
79
77
|
value = send(property_name)
|
data/spec/json/encodable_spec.rb
CHANGED
@@ -51,13 +51,12 @@ describe JSON::Encodable do
|
|
51
51
|
end
|
52
52
|
end
|
53
53
|
|
54
|
-
context "with :except option" do
|
54
|
+
context "with :except option in Array" do
|
55
55
|
before do
|
56
56
|
options[:except] = [:id]
|
57
57
|
end
|
58
58
|
|
59
59
|
it "excludes those properties" do
|
60
|
-
instance.should_not_receive(:id)
|
61
60
|
should be_json_as(
|
62
61
|
title: "wonderland",
|
63
62
|
username: "alice",
|
@@ -65,7 +64,20 @@ describe JSON::Encodable do
|
|
65
64
|
end
|
66
65
|
end
|
67
66
|
|
68
|
-
context "with :
|
67
|
+
context "with :except option in non-Array" do
|
68
|
+
before do
|
69
|
+
options[:except] = :id
|
70
|
+
end
|
71
|
+
|
72
|
+
it "is treated as an Array" do
|
73
|
+
should be_json_as(
|
74
|
+
title: "wonderland",
|
75
|
+
username: "alice",
|
76
|
+
)
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
context "with :only option in Array" do
|
69
81
|
before do
|
70
82
|
options[:only] = [:id]
|
71
83
|
end
|
@@ -74,5 +86,15 @@ describe JSON::Encodable do
|
|
74
86
|
should be_json_as(id: 1)
|
75
87
|
end
|
76
88
|
end
|
89
|
+
|
90
|
+
context "with :only option in non-Array" do
|
91
|
+
before do
|
92
|
+
options[:only] = :id
|
93
|
+
end
|
94
|
+
|
95
|
+
it "is treated as an Array" do
|
96
|
+
should be_json_as(id: 1)
|
97
|
+
end
|
98
|
+
end
|
77
99
|
end
|
78
100
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: json-encodable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ryo Nakamura
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2015-02-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -119,7 +119,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
119
119
|
version: '0'
|
120
120
|
requirements: []
|
121
121
|
rubyforge_project:
|
122
|
-
rubygems_version: 2.
|
122
|
+
rubygems_version: 2.4.5
|
123
123
|
signing_key:
|
124
124
|
specification_version: 4
|
125
125
|
summary: Make a class encodable into JSON format.
|