json-encodable 0.0.1
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 +7 -0
- data/.gitignore +22 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +44 -0
- data/Rakefile +2 -0
- data/json-encodable.gemspec +24 -0
- data/lib/json-encodable.rb +1 -0
- data/lib/json/encodable.rb +108 -0
- data/lib/json/encodable/version.rb +5 -0
- data/spec/json/encodable_spec.rb +78 -0
- data/spec/spec_helper.rb +9 -0
- metadata +128 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 0bf6e0b66a6b91365aff2c33598588fc9df00621
|
4
|
+
data.tar.gz: 6e03c6710630f90087ce06833076906479f46c0e
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 63b4ac30bc4d8e1c3cb19455606bc7864caac4268e1198c51377f23a652b6f020d20c6efa038fc05fb8d3c9ad6d037b851bb349a369d944ac04af484be7d8eea
|
7
|
+
data.tar.gz: 6e7370ac9f1b573969f0fc6e666d9cd5b1649c7996c05e2ec9cba18d5b47fe428e8433ead737997be79711ecf732732e0e399334795c8f7f18b9eb0fe57b85b9
|
data/.gitignore
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
.bundle
|
4
|
+
.config
|
5
|
+
.yardoc
|
6
|
+
Gemfile.lock
|
7
|
+
InstalledFiles
|
8
|
+
_yardoc
|
9
|
+
coverage
|
10
|
+
doc/
|
11
|
+
lib/bundler/man
|
12
|
+
pkg
|
13
|
+
rdoc
|
14
|
+
spec/reports
|
15
|
+
test/tmp
|
16
|
+
test/version_tmp
|
17
|
+
tmp
|
18
|
+
*.bundle
|
19
|
+
*.so
|
20
|
+
*.o
|
21
|
+
*.a
|
22
|
+
mkmf.log
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Ryo Nakamura
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
# JSON::Encodable
|
2
|
+
Make a class encodable into JSON format.
|
3
|
+
|
4
|
+
## Usage
|
5
|
+
1. Include `JSON::Encodable` module
|
6
|
+
2. Call `.property` method with property name
|
7
|
+
3. Then the instance will be able to respond to `.to_json` method
|
8
|
+
|
9
|
+
### #to_json
|
10
|
+
```ruby
|
11
|
+
class Blog
|
12
|
+
include JSON::Encodable
|
13
|
+
|
14
|
+
property :id
|
15
|
+
property :title
|
16
|
+
property :username
|
17
|
+
|
18
|
+
def id
|
19
|
+
1
|
20
|
+
end
|
21
|
+
|
22
|
+
def title
|
23
|
+
"wonderland"
|
24
|
+
end
|
25
|
+
|
26
|
+
def username
|
27
|
+
"alice"
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
Blog.new.to_json
|
32
|
+
#=> '{"id":1,"title":"wonderland","username":"alice"}'
|
33
|
+
```
|
34
|
+
|
35
|
+
### #as_json(options = {})
|
36
|
+
You can also call `.as_json` method with `:except` and `:only` options.
|
37
|
+
|
38
|
+
```ruby
|
39
|
+
Blog.new.as_json(only: [:id, :username])
|
40
|
+
#=> {"id":1,"username":"alice"}
|
41
|
+
|
42
|
+
Blog.new.as_json(except: [:username])
|
43
|
+
#=> {"id":1,"title":"wonderland"}
|
44
|
+
```
|
data/Rakefile
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
lib = File.expand_path("../lib", __FILE__)
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
require "json/encodable/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "json-encodable"
|
7
|
+
spec.version = JSON::Encodable::VERSION
|
8
|
+
spec.authors = ["Ryo Nakamura"]
|
9
|
+
spec.email = ["r7kamura@gmail.com"]
|
10
|
+
spec.summary = "Make a class encodable into JSON format."
|
11
|
+
spec.homepage = "https://github.com/r7kamura/json-encodable"
|
12
|
+
spec.license = "MIT"
|
13
|
+
|
14
|
+
spec.files = `git ls-files -z`.split("\x0")
|
15
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
16
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
17
|
+
spec.require_paths = ["lib"]
|
18
|
+
|
19
|
+
spec.add_dependency "activesupport"
|
20
|
+
spec.add_development_dependency "bundler", "~> 1.6"
|
21
|
+
spec.add_development_dependency "rake"
|
22
|
+
spec.add_development_dependency "rspec", "2.14.1"
|
23
|
+
spec.add_development_dependency "rspec-json_matcher"
|
24
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
require "json_encoder/encodable"
|
@@ -0,0 +1,108 @@
|
|
1
|
+
require "active_support/concern"
|
2
|
+
require "active_support/core_ext/hash/reverse_merge"
|
3
|
+
require "active_support/core_ext/object/json"
|
4
|
+
require "active_support/core_ext/string/inflections"
|
5
|
+
require "active_support/json"
|
6
|
+
|
7
|
+
require "json/encodable/version"
|
8
|
+
|
9
|
+
# Makes an included module encodable into JSON format by putting .as_json method.
|
10
|
+
#
|
11
|
+
# @example
|
12
|
+
# class Blog
|
13
|
+
# include JSON::Encodable
|
14
|
+
#
|
15
|
+
# property :id
|
16
|
+
# property :title
|
17
|
+
# property :username
|
18
|
+
#
|
19
|
+
# def id
|
20
|
+
# 1
|
21
|
+
# end
|
22
|
+
#
|
23
|
+
# def title
|
24
|
+
# "wonderland"
|
25
|
+
# end
|
26
|
+
#
|
27
|
+
# def username
|
28
|
+
# "alice"
|
29
|
+
# end
|
30
|
+
# end
|
31
|
+
#
|
32
|
+
# puts Blog.new.to_json
|
33
|
+
# #=> {"id":1,"title":"wonderland","username":"alice"}
|
34
|
+
#
|
35
|
+
# You can pass :only and :except options to .to_json method.
|
36
|
+
#
|
37
|
+
# @example
|
38
|
+
# puts Blog.new.to_json(only: [:id, :username])
|
39
|
+
# #=> {"id":1,"username":"alice"}
|
40
|
+
#
|
41
|
+
# puts Blog.new.to_json(except: [:username])
|
42
|
+
# #=> {"id":1,"title":"wonderland"}
|
43
|
+
#
|
44
|
+
module JSON
|
45
|
+
module Encodable
|
46
|
+
extend ActiveSupport::Concern
|
47
|
+
|
48
|
+
# @return [Hash] An object representation of its properties.
|
49
|
+
# @example
|
50
|
+
# {
|
51
|
+
# id: 1,
|
52
|
+
# title: "wonderland",
|
53
|
+
# username: "alice",
|
54
|
+
# }
|
55
|
+
def as_json(options = {})
|
56
|
+
properties(options).as_json(options)
|
57
|
+
end
|
58
|
+
|
59
|
+
private
|
60
|
+
|
61
|
+
# @note All Time values will be encoded in ISO 8601 format
|
62
|
+
# @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
|
65
|
+
# @return [Hash{Symbol => Object}] Properties as a key-value pairs Hash
|
66
|
+
# @example
|
67
|
+
# {
|
68
|
+
# id: 1,
|
69
|
+
# title: "wonderland",
|
70
|
+
# username: "alice",
|
71
|
+
# created_at: "2014-07-11T11:28:54+09:00",
|
72
|
+
# }
|
73
|
+
def properties(options = {})
|
74
|
+
names = self.class.property_names
|
75
|
+
names = names - options[:except] if options[:except]
|
76
|
+
names = names & options[:only] if options[:only]
|
77
|
+
names.inject({}) do |hash, property_name|
|
78
|
+
key = property_name
|
79
|
+
value = send(property_name)
|
80
|
+
value = value.iso8601 if value.is_a?(Time)
|
81
|
+
hash.merge(key => value)
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
module ClassMethods
|
86
|
+
attr_writer :property_names
|
87
|
+
|
88
|
+
# Stores property names, used to build JSON properties
|
89
|
+
# @return [Array]
|
90
|
+
def property_names
|
91
|
+
@property_names ||= []
|
92
|
+
end
|
93
|
+
|
94
|
+
# Defines a given property name as a property of the JSON prepresentation its class
|
95
|
+
# @param normal_property_name [Symbol]
|
96
|
+
def property(property_name)
|
97
|
+
property_names << property_name
|
98
|
+
end
|
99
|
+
|
100
|
+
# Inherits property_names, while they are not shared between a parent and their children
|
101
|
+
# @note Override
|
102
|
+
def inherited(child)
|
103
|
+
super
|
104
|
+
child.property_names = property_names.clone
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
@@ -0,0 +1,78 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe JSON::Encodable do
|
4
|
+
subject do
|
5
|
+
instance.to_json(options)
|
6
|
+
end
|
7
|
+
|
8
|
+
let(:klass) do
|
9
|
+
Class.new do
|
10
|
+
include JSON::Encodable
|
11
|
+
|
12
|
+
property :id
|
13
|
+
property :title
|
14
|
+
property :username
|
15
|
+
|
16
|
+
# Dummy name to prevent defining a class in the global namespace
|
17
|
+
def self.name
|
18
|
+
"Foo::BarBaz"
|
19
|
+
end
|
20
|
+
|
21
|
+
def id
|
22
|
+
1
|
23
|
+
end
|
24
|
+
|
25
|
+
def title
|
26
|
+
"wonderland"
|
27
|
+
end
|
28
|
+
|
29
|
+
def username
|
30
|
+
"alice"
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
let(:options) do
|
36
|
+
{}
|
37
|
+
end
|
38
|
+
|
39
|
+
let(:instance) do
|
40
|
+
klass.new
|
41
|
+
end
|
42
|
+
|
43
|
+
describe ".to_json" do
|
44
|
+
context "without any options" do
|
45
|
+
it "returns an Object in JSON format" do
|
46
|
+
should be_json_as(
|
47
|
+
id: 1,
|
48
|
+
title: "wonderland",
|
49
|
+
username: "alice",
|
50
|
+
)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
context "with :except option" do
|
55
|
+
before do
|
56
|
+
options[:except] = [:id]
|
57
|
+
end
|
58
|
+
|
59
|
+
it "excludes those properties" do
|
60
|
+
instance.should_not_receive(:id)
|
61
|
+
should be_json_as(
|
62
|
+
title: "wonderland",
|
63
|
+
username: "alice",
|
64
|
+
)
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
context "with :only option" do
|
69
|
+
before do
|
70
|
+
options[:only] = [:id]
|
71
|
+
end
|
72
|
+
|
73
|
+
it "excludes those properties" do
|
74
|
+
should be_json_as(id: 1)
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,9 @@
|
|
1
|
+
require "json/encodable"
|
2
|
+
require "rspec/json_matcher"
|
3
|
+
|
4
|
+
RSpec.configure do |config|
|
5
|
+
config.treat_symbols_as_metadata_keys_with_true_values = true
|
6
|
+
config.run_all_when_everything_filtered = true
|
7
|
+
config.filter_run :focus
|
8
|
+
config.include RSpec::JsonMatcher
|
9
|
+
end
|
metadata
ADDED
@@ -0,0 +1,128 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: json-encodable
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Ryo Nakamura
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-09-08 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activesupport
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.6'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.6'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - '='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 2.14.1
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - '='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 2.14.1
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rspec-json_matcher
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
description:
|
84
|
+
email:
|
85
|
+
- r7kamura@gmail.com
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- ".gitignore"
|
91
|
+
- Gemfile
|
92
|
+
- LICENSE.txt
|
93
|
+
- README.md
|
94
|
+
- Rakefile
|
95
|
+
- json-encodable.gemspec
|
96
|
+
- lib/json-encodable.rb
|
97
|
+
- lib/json/encodable.rb
|
98
|
+
- lib/json/encodable/version.rb
|
99
|
+
- spec/json/encodable_spec.rb
|
100
|
+
- spec/spec_helper.rb
|
101
|
+
homepage: https://github.com/r7kamura/json-encodable
|
102
|
+
licenses:
|
103
|
+
- MIT
|
104
|
+
metadata: {}
|
105
|
+
post_install_message:
|
106
|
+
rdoc_options: []
|
107
|
+
require_paths:
|
108
|
+
- lib
|
109
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
110
|
+
requirements:
|
111
|
+
- - ">="
|
112
|
+
- !ruby/object:Gem::Version
|
113
|
+
version: '0'
|
114
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
115
|
+
requirements:
|
116
|
+
- - ">="
|
117
|
+
- !ruby/object:Gem::Version
|
118
|
+
version: '0'
|
119
|
+
requirements: []
|
120
|
+
rubyforge_project:
|
121
|
+
rubygems_version: 2.2.2
|
122
|
+
signing_key:
|
123
|
+
specification_version: 4
|
124
|
+
summary: Make a class encodable into JSON format.
|
125
|
+
test_files:
|
126
|
+
- spec/json/encodable_spec.rb
|
127
|
+
- spec/spec_helper.rb
|
128
|
+
has_rdoc:
|