json-encodable 0.0.3 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +3 -0
- data/README.md +18 -0
- data/json-encodable.gemspec +1 -1
- data/lib/json/encodable.rb +12 -10
- data/lib/json/encodable/property.rb +18 -0
- data/lib/json/encodable/version.rb +1 -1
- data/spec/json/encodable_spec.rb +22 -4
- data/spec/spec_helper.rb +0 -1
- metadata +5 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e414ac50d8cfab870205ef14aa3bb3447f6919dd
|
4
|
+
data.tar.gz: 5b6924e2bb5fb84dde7e40ff53493b5605c6bbbf
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ade9a952eb1a3948f38adc341512917908bbf91ee484a1f90e387c3b9e07e3dc906f50fca4eadf7ee2f707d900dce43cc3fe86db2fe5102b5507eaf65ad0b997
|
7
|
+
data.tar.gz: 59296009778e8aec1c8d7f6f16b33e608c747a1070ba7b74ee32fb519048d2e86c94f7b2647a248f1e285121dd0d01c5b74e7e25f732edf679f2cd662f409c9b
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -42,3 +42,21 @@ Blog.new.as_json(only: [:id, :username])
|
|
42
42
|
Blog.new.as_json(except: :username)
|
43
43
|
#=> { "id" => 1, "title" => "wonderland" }
|
44
44
|
```
|
45
|
+
|
46
|
+
## Advanced
|
47
|
+
You can contain any optional meta-information on each properties,
|
48
|
+
and access to them by `.properties` method.
|
49
|
+
|
50
|
+
```rb
|
51
|
+
class Blog
|
52
|
+
include JSON::Encodable
|
53
|
+
|
54
|
+
property :id, type: Integer
|
55
|
+
property :title, type: String
|
56
|
+
property :username, type: String
|
57
|
+
end
|
58
|
+
|
59
|
+
# Returns an array of `JSON::Encodable::Property` instances.
|
60
|
+
# Each instance has `#name` and `#options` methods.
|
61
|
+
Blog.properties
|
62
|
+
```
|
data/json-encodable.gemspec
CHANGED
@@ -19,6 +19,6 @@ Gem::Specification.new do |spec|
|
|
19
19
|
spec.add_dependency "activesupport"
|
20
20
|
spec.add_development_dependency "bundler", "~> 1.6"
|
21
21
|
spec.add_development_dependency "rake"
|
22
|
-
spec.add_development_dependency "rspec", "2.
|
22
|
+
spec.add_development_dependency "rspec", "3.2.0"
|
23
23
|
spec.add_development_dependency "rspec-json_matcher"
|
24
24
|
end
|
data/lib/json/encodable.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
require "active_support/concern"
|
2
2
|
require "active_support/core_ext/object/json"
|
3
3
|
require "active_support/json"
|
4
|
-
|
4
|
+
require "json/encodable/property"
|
5
5
|
require "json/encodable/version"
|
6
6
|
|
7
7
|
# Makes an included module encodable into JSON format by putting .as_json method.
|
@@ -83,23 +83,25 @@ module JSON
|
|
83
83
|
module ClassMethods
|
84
84
|
attr_writer :property_names
|
85
85
|
|
86
|
-
#
|
87
|
-
# @return [Array]
|
86
|
+
# @return [Array<Symbol>]
|
88
87
|
def property_names
|
89
|
-
|
88
|
+
properties.map(&:name)
|
89
|
+
end
|
90
|
+
|
91
|
+
# @return [Array<JSON::Encodable::Property>]
|
92
|
+
def properties
|
93
|
+
@properies ||= []
|
90
94
|
end
|
91
95
|
|
92
|
-
#
|
93
|
-
|
94
|
-
|
95
|
-
property_names << property_name
|
96
|
+
# @param [Symbol] property_name
|
97
|
+
def property(property_name, options = {})
|
98
|
+
properties << ::JSON::Encodable::Property.new(property_name, options)
|
96
99
|
end
|
97
100
|
|
98
|
-
# Inherits property_names, while they are not shared between a parent and their children
|
99
101
|
# @note Override
|
100
102
|
def inherited(child)
|
101
103
|
super
|
102
|
-
child.
|
104
|
+
child.properties = properties.clone
|
103
105
|
end
|
104
106
|
end
|
105
107
|
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module JSON
|
2
|
+
module Encodable
|
3
|
+
class Property
|
4
|
+
# @return [Symbol]
|
5
|
+
attr_reader :name
|
6
|
+
|
7
|
+
# @return [Hash{Symbol => Object}]
|
8
|
+
attr_reader :options
|
9
|
+
|
10
|
+
# @param [Symbol] name
|
11
|
+
# @param [Hash{Symbol => Object}] options
|
12
|
+
def initialize(name, options = {})
|
13
|
+
@name = name
|
14
|
+
@options = options
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
data/spec/json/encodable_spec.rb
CHANGED
@@ -9,9 +9,9 @@ describe JSON::Encodable do
|
|
9
9
|
Class.new do
|
10
10
|
include JSON::Encodable
|
11
11
|
|
12
|
-
property :id
|
13
|
-
property :title
|
14
|
-
property :username
|
12
|
+
property :id, type: Integer
|
13
|
+
property :title, type: String
|
14
|
+
property :username, type: String
|
15
15
|
|
16
16
|
# Dummy name to prevent defining a class in the global namespace
|
17
17
|
def self.name
|
@@ -40,7 +40,25 @@ describe JSON::Encodable do
|
|
40
40
|
klass.new
|
41
41
|
end
|
42
42
|
|
43
|
-
describe ".
|
43
|
+
describe ".properties" do
|
44
|
+
subject do
|
45
|
+
klass.properties
|
46
|
+
end
|
47
|
+
|
48
|
+
it "returns an Array of JSON::Encodable::Property objects" do
|
49
|
+
should match(
|
50
|
+
array_including(
|
51
|
+
[
|
52
|
+
instance_of(JSON::Encodable::Property),
|
53
|
+
instance_of(JSON::Encodable::Property),
|
54
|
+
instance_of(JSON::Encodable::Property),
|
55
|
+
],
|
56
|
+
),
|
57
|
+
)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
describe "#to_json" do
|
44
62
|
context "without any options" do
|
45
63
|
it "returns an Object in JSON format" do
|
46
64
|
should be_json_as(
|
data/spec/spec_helper.rb
CHANGED
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.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ryo Nakamura
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-05-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -58,14 +58,14 @@ dependencies:
|
|
58
58
|
requirements:
|
59
59
|
- - '='
|
60
60
|
- !ruby/object:Gem::Version
|
61
|
-
version: 2.
|
61
|
+
version: 3.2.0
|
62
62
|
type: :development
|
63
63
|
prerelease: false
|
64
64
|
version_requirements: !ruby/object:Gem::Requirement
|
65
65
|
requirements:
|
66
66
|
- - '='
|
67
67
|
- !ruby/object:Gem::Version
|
68
|
-
version: 2.
|
68
|
+
version: 3.2.0
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
70
|
name: rspec-json_matcher
|
71
71
|
requirement: !ruby/object:Gem::Requirement
|
@@ -96,6 +96,7 @@ files:
|
|
96
96
|
- json-encodable.gemspec
|
97
97
|
- lib/json-encodable.rb
|
98
98
|
- lib/json/encodable.rb
|
99
|
+
- lib/json/encodable/property.rb
|
99
100
|
- lib/json/encodable/version.rb
|
100
101
|
- spec/json/encodable_spec.rb
|
101
102
|
- spec/spec_helper.rb
|
@@ -126,4 +127,3 @@ summary: Make a class encodable into JSON format.
|
|
126
127
|
test_files:
|
127
128
|
- spec/json/encodable_spec.rb
|
128
129
|
- spec/spec_helper.rb
|
129
|
-
has_rdoc:
|