json-encodable 0.0.3 → 0.1.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: 60320d82f60c457b9360b943d1c73cff3fc4b61e
4
- data.tar.gz: 32bac5f41d30df13ccd1f24b00f0738a6b2e81d5
3
+ metadata.gz: e414ac50d8cfab870205ef14aa3bb3447f6919dd
4
+ data.tar.gz: 5b6924e2bb5fb84dde7e40ff53493b5605c6bbbf
5
5
  SHA512:
6
- metadata.gz: d21247643d7c2f7d70ff302b131f626b7061b71644988f440a1704a45942ad2969021a07486be2f432470af884ef97f557aaf293b6f2b8e37a981ce81ad0ec77
7
- data.tar.gz: 913b8affcdd2804cba377121934e3731c28106db44206f184af28c3d09e172e35f2e7c50f2d2ef856f9b6f7980f9fc8857171541d4d7f4042af8f3443befb58e
6
+ metadata.gz: ade9a952eb1a3948f38adc341512917908bbf91ee484a1f90e387c3b9e07e3dc906f50fca4eadf7ee2f707d900dce43cc3fe86db2fe5102b5507eaf65ad0b997
7
+ data.tar.gz: 59296009778e8aec1c8d7f6f16b33e608c747a1070ba7b74ee32fb519048d2e86c94f7b2647a248f1e285121dd0d01c5b74e7e25f732edf679f2cd662f409c9b
data/CHANGELOG.md CHANGED
@@ -1,3 +1,6 @@
1
+ ## 0.1.0
2
+ - Support optional metadata on each property
3
+
1
4
  ## 0.0.3
2
5
  - Support non-Array arguments in :except and only option (thx @yujinakayama)
3
6
 
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
+ ```
@@ -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.14.1"
22
+ spec.add_development_dependency "rspec", "3.2.0"
23
23
  spec.add_development_dependency "rspec-json_matcher"
24
24
  end
@@ -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
- # Stores property names, used to build JSON properties
87
- # @return [Array]
86
+ # @return [Array<Symbol>]
88
87
  def property_names
89
- @property_names ||= []
88
+ properties.map(&:name)
89
+ end
90
+
91
+ # @return [Array<JSON::Encodable::Property>]
92
+ def properties
93
+ @properies ||= []
90
94
  end
91
95
 
92
- # Defines a given property name as a property of the JSON prepresentation its class
93
- # @param normal_property_name [Symbol]
94
- def property(property_name)
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.property_names = property_names.clone
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
@@ -1,5 +1,5 @@
1
1
  module JSON
2
2
  module Encodable
3
- VERSION = "0.0.3"
3
+ VERSION = "0.1.0"
4
4
  end
5
5
  end
@@ -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 ".to_json" do
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
@@ -2,7 +2,6 @@ require "json-encodable"
2
2
  require "rspec/json_matcher"
3
3
 
4
4
  RSpec.configure do |config|
5
- config.treat_symbols_as_metadata_keys_with_true_values = true
6
5
  config.run_all_when_everything_filtered = true
7
6
  config.filter_run :focus
8
7
  config.include RSpec::JsonMatcher
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.3
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-02-23 00:00:00.000000000 Z
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.14.1
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.14.1
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: