json-encodable 0.0.2 → 0.0.3

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: 248cb9a0f0dfd9d4f5af77644a848cb110dc97ed
4
- data.tar.gz: 575ec091a261b281b9fec69cb818304d76faa740
3
+ metadata.gz: 60320d82f60c457b9360b943d1c73cff3fc4b61e
4
+ data.tar.gz: 32bac5f41d30df13ccd1f24b00f0738a6b2e81d5
5
5
  SHA512:
6
- metadata.gz: 183566b3b5874a0c1ae2b7b2c8a4fd16d5c94dcc4dc98e5a8d868637b01d58157bf9197c7cd30ba7f21da5d6147417a449c22f13fdc8fa20bede76d063a1115c
7
- data.tar.gz: b97c23bd2917c9a01a8fde16254207b35bb7102dca33e94383b1c440d2fc49ef2fbd91f3af7819f053dee219f2e74c5d212672392a8d0fb1bcbd17f397b7ba05
6
+ metadata.gz: d21247643d7c2f7d70ff302b131f626b7061b71644988f440a1704a45942ad2969021a07486be2f432470af884ef97f557aaf293b6f2b8e37a981ce81ad0ec77
7
+ data.tar.gz: 913b8affcdd2804cba377121934e3731c28106db44206f184af28c3d09e172e35f2e7c50f2d2ef856f9b6f7980f9fc8857171541d4d7f4042af8f3443befb58e
data/CHANGELOG.md CHANGED
@@ -1,5 +1,8 @@
1
+ ## 0.0.3
2
+ - Support non-Array arguments in :except and only option (thx @yujinakayama)
3
+
1
4
  ## 0.0.2
2
- * Fix loading logic for bundler :bug:
5
+ - Fix loading logic for bundler :bug:
3
6
 
4
7
  ## 0.0.1
5
- * 1st Release :octocat:
8
+ - 1st Release :octocat:
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 `.to_json` method
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":1,"username":"alice"}
40
+ #=> { "id" => 1, "username" => "alice" }
41
41
 
42
- Blog.new.as_json(except: [:username])
43
- #=> {"id":1,"title":"wonderland"}
42
+ Blog.new.as_json(except: :username)
43
+ #=> { "id" => 1, "title" => "wonderland" }
44
44
  ```
@@ -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)
@@ -1,5 +1,5 @@
1
1
  module JSON
2
2
  module Encodable
3
- VERSION = "0.0.2"
3
+ VERSION = "0.0.3"
4
4
  end
5
5
  end
@@ -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 :only option" do
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.2
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: 2014-09-08 00:00:00.000000000 Z
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.2.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.