ruson 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: a150c64d273d0a798732848c4269ccf33353e696
4
- data.tar.gz: a67ca62f8b6e3853ad3a93fe8a889a2b2a1a1428
2
+ SHA256:
3
+ metadata.gz: 0eaac3b601d5b1ca7e7f99852749e5a24bfe05b070945e8ff7c577d7f89f22fe
4
+ data.tar.gz: 75e7430384d80df1694dbf328283775b1f4fabfa1e944d0f84acd543732cf74e
5
5
  SHA512:
6
- metadata.gz: 21887ee37dd096de8f366734bcca9ec01d173e8672514908551120a4df69980bcd7a6cc5841a1a5c8f277bfd681ca91e78c198fc623ee62b019b73f92681315b
7
- data.tar.gz: 3dcf18d17ffafe73612de90b8cea4cb0586aa5bd8b9af1dbe4e02aba0fa0d76d603b83bdfc79cd17a585e175a1831ff7d536a2bee8010dcfe2c12ce67ec8153d
6
+ metadata.gz: a230098ac5bef3d5f0c0e010bb8f0fcc172de10a47e34a742de3da531cf2aa261c67a31fe499cf5d070ebfb3ed604906a651e512b89fc88fc071f3b3e478172d
7
+ data.tar.gz: c262e5d587400aa2ca8eeda57716e2228469ceff286ac46acbe6ee6550b49711f147bbe217b3e6c251fba5971a5e58a68127813be7a6005b3cf1758731cf25c5
data/README.md CHANGED
@@ -31,11 +31,9 @@ post.json
31
31
  ```ruby
32
32
  require 'ruson'
33
33
 
34
- class Post < Ruson::Base
35
- def fields
36
- field :title
37
- field :content
38
- end
34
+ class Post < Ruson::Base
35
+ field :title
36
+ field :content
39
37
  end
40
38
 
41
39
  json = File.read('post.json')
@@ -58,10 +56,8 @@ post.json
58
56
  require 'ruson'
59
57
 
60
58
  class Post < Ruson::Base
61
- def fields
62
- field :title
63
- field :url, name: 'post_url'
64
- end
59
+ field :title
60
+ field :url, name: 'post_url'
65
61
  end
66
62
 
67
63
  json = File.read('post.json')
@@ -89,17 +85,13 @@ post.json
89
85
  require 'ruson'
90
86
 
91
87
  class Post < Ruson::Base
92
- def fields
93
- field :title
94
- field :picture, class: Picture
95
- end
88
+ field :title
89
+ field :picture, class: Picture
96
90
  end
97
91
 
98
92
  class Picture < Ruson::Base
99
- def fields
100
- field :title
101
- field :url
102
- end
93
+ field :title
94
+ field :url
103
95
  end
104
96
 
105
97
  json = File.read('post.json')
@@ -128,16 +120,12 @@ post.json
128
120
  require 'ruson'
129
121
 
130
122
  class Post < Ruson::Base
131
- def fields
132
- field :title
133
- field :tags, each_class: Tag
134
- end
123
+ field :title
124
+ field :tags, each_class: Tag
135
125
  end
136
126
 
137
127
  class Tag < Ruson::Base
138
- def fields
139
- field :name
140
- end
128
+ field :name
141
129
  end
142
130
 
143
131
  json = File.read('post.json')
@@ -1,24 +1,34 @@
1
1
  require 'json'
2
+ require 'active_support'
3
+ require 'active_support/core_ext'
2
4
 
3
5
  module Ruson
4
6
  class Base
5
- def initialize(json, options={})
6
- params = convert(json)
7
- params = params[options[:root].to_s] unless options[:root].nil?
8
- @params = params
9
- fields
10
- end
7
+ class << self
8
+ def field(attr, options = {})
9
+ add_accessor attr.to_s, options
10
+ end
11
11
 
12
- def fields
12
+ def add_accessor(name, options)
13
+ instance_eval("attr_accessor :#{name}")
14
+ @accessors ||= {}
15
+ @accessors.merge!({ name.to_sym => options })
16
+ end
13
17
 
18
+ def accessors
19
+ @accessors
20
+ end
14
21
  end
15
22
 
16
- def field(attr, options={})
17
- attr_name = attr.to_s
18
- key_name = options[:name] || attr_name
19
- self.class.class_eval("attr_accessor :#{attr_name}")
20
- val = get_val(key_name, options)
21
- set_attribute(attr_name, val)
23
+ def initialize(json, options = {})
24
+ params = convert(json)
25
+ params = params[options[:root].to_s] unless options[:root].nil?
26
+ @params = params
27
+
28
+ self.class.accessors.each do |key, options|
29
+ val = get_val(options[:name] || key, options)
30
+ set_attribute(key, val)
31
+ end
22
32
  end
23
33
 
24
34
  private
@@ -51,8 +61,8 @@ module Ruson
51
61
  end
52
62
 
53
63
  def convert(json)
54
- return json if json.class == Hash
55
- JSON.parse(json)
64
+ return json if json.class == ActiveSupport::HashWithIndifferentAccess
65
+ (json.class == Hash ? json : JSON.parse(json)).with_indifferent_access
56
66
  end
57
67
  end
58
- end
68
+ end
@@ -1,3 +1,3 @@
1
1
  module Ruson
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -4,15 +4,15 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
  require "ruson/version"
5
5
 
6
6
  Gem::Specification.new do |spec|
7
- spec.name = "ruson"
8
- spec.version = Ruson::VERSION
9
- spec.authors = ["klriutsa"]
10
- spec.email = ["hiroya.kurushima@litalico.co.jp"]
7
+ spec.name = "ruson"
8
+ spec.version = Ruson::VERSION
9
+ spec.authors = ["klriutsa"]
10
+ spec.email = ["hiroya.kurushima@litalico.co.jp"]
11
11
 
12
- spec.summary = %q{ruby json library.}
13
- spec.description = %q{ruby json library.}
14
- spec.homepage = "https://github.com/klriutsa/ruson"
15
- spec.license = "MIT"
12
+ spec.summary = %q{ruby json library.}
13
+ spec.description = %q{ruby json library.}
14
+ spec.homepage = "https://github.com/klriutsa/ruson"
15
+ spec.license = "MIT"
16
16
 
17
17
  spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
18
  f.match(%r{^(test|spec|features)/})
@@ -24,4 +24,5 @@ Gem::Specification.new do |spec|
24
24
  spec.add_development_dependency "bundler", "~> 1.15"
25
25
  spec.add_development_dependency "rake", "~> 10.0"
26
26
  spec.add_development_dependency "rspec", "~> 3.0"
27
+ spec.add_development_dependency 'activesupport'
27
28
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruson
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - klriutsa
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-11-02 00:00:00.000000000 Z
11
+ date: 2018-08-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -52,6 +52,20 @@ dependencies:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
54
  version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: activesupport
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
55
69
  description: ruby json library.
56
70
  email:
57
71
  - hiroya.kurushima@litalico.co.jp
@@ -93,7 +107,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
93
107
  version: '0'
94
108
  requirements: []
95
109
  rubyforge_project:
96
- rubygems_version: 2.6.13
110
+ rubygems_version: 2.7.7
97
111
  signing_key:
98
112
  specification_version: 4
99
113
  summary: ruby json library.