ruson 1.0.0 → 1.1.0

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
2
  SHA256:
3
- metadata.gz: 51918f002d66ba2ffcf4e7e06c4a3b0e31892426ae3fa61df9671a68322cec73
4
- data.tar.gz: 7c49ce59c40f75e62989ce794b84d009018fd7c6402c046a86208ce27fbc2ae1
3
+ metadata.gz: 39dd7c6ce4a45497f8925a29bd35bc4cead7c1a7de5b540f82ac3792be969305
4
+ data.tar.gz: ddd14dd0b2f08c7f2fc2a6502908234411ae6a28e0eb6d222f5736d9668411bb
5
5
  SHA512:
6
- metadata.gz: a2adab4f47a5c93f73c96151da9a072aed2fc835c359ac49e1f7d2a97dd9ff46898da2c3ece5374af1a139212352f4b37edec74e9fe0e73853ca36b2f760eac8
7
- data.tar.gz: fe318ac05c022af0885cc3ea1d52238db8af0672638cfb8f121d9250054796fd81482f6bdacc7884abbf0d43c03310f0b002f18a4f95200148bf0991c947dc97
6
+ metadata.gz: b908373f13bca616112e8bc1ef73d237cd1a44aa06102e9c4b7309befb2358530ea6622688a088f1293b63eb387798561bc9363453d7962b01b4df67169b3644
7
+ data.tar.gz: cb648ad162cbfbc5a25d3814ec686d116ddcfed0e75c59e7137cf5492b13bbfae8e940842c25c8bf075b9674d038a7cbfeab058bd350efc05ef755aa5a53d0f4
data/README.md CHANGED
@@ -99,6 +99,38 @@ post = Post.new(json)
99
99
  post.picture.url #=> 'http://sample.com/picture.png'
100
100
  ```
101
101
 
102
+ ##### Primary classes
103
+
104
+ * Boolean
105
+ * Integer
106
+ * Float
107
+
108
+
109
+ post.json
110
+ ```json
111
+ {
112
+ "title": "Ruson",
113
+ "is_new": "true",
114
+ "view": "1234",
115
+ "rate": "3.8"
116
+ }
117
+ ```
118
+
119
+ ```ruby
120
+ class Post < Ruson::Base
121
+ field :title
122
+ field :is_new, class: Boolean
123
+ field :view, class: Integer
124
+ field :rate, class: Float
125
+ end
126
+
127
+ json = File.read('post.json')
128
+ post = Post.new(json)
129
+ post.is_new #=> true
130
+ post.view #=> 1234
131
+ post.rate #=> 3.8
132
+ ```
133
+
102
134
  #### each class
103
135
 
104
136
  post.json
data/lib/ruson/base.rb CHANGED
@@ -2,6 +2,14 @@ require 'json'
2
2
  require 'active_support'
3
3
  require 'active_support/core_ext'
4
4
 
5
+ require 'ruson/class/boolean'
6
+ require 'ruson/class/integer'
7
+ require 'ruson/class/float'
8
+
9
+ require 'ruson/converter'
10
+ require 'ruson/json'
11
+ require 'ruson/value'
12
+
5
13
  module Ruson
6
14
  class Base
7
15
  class << self
@@ -42,22 +50,19 @@ module Ruson
42
50
  end
43
51
  end
44
52
 
53
+ include Ruson::Converter
54
+ include Ruson::Json
55
+ include Ruson::Value
56
+
45
57
  def initialize(json, root_key: nil)
46
- params = convert(json)
58
+ params = get_hash_from_json(json)
47
59
  params = params[root_key.to_s] unless root_key.nil?
48
60
 
49
- self.class.accessors.each do |key, options|
50
- val = get_val(params, options[:name] || key, options)
51
- set_attribute(key, val)
52
- end
61
+ init_attributes(self.class.accessors, params)
53
62
  end
54
63
 
55
64
  def to_hash
56
- self.class.accessors.keys.inject({}) do |result, key|
57
- value = send(key)
58
- result[key.to_sym] = convert_array_to_hash_value(value)
59
- result
60
- end
65
+ convert_to_hash(self.class.accessors)
61
66
  end
62
67
 
63
68
  def to_json
@@ -66,53 +71,15 @@ module Ruson
66
71
 
67
72
  private
68
73
 
69
- def set_attribute(attr_name, val)
70
- self.send("#{attr_name}=".to_sym, val)
71
- end
72
-
73
- def get_val(params, key_name, options)
74
- if !options[:class].nil?
75
- class_param(params[key_name], options[:class])
76
- elsif !options[:each_class].nil?
77
- each_class_param(params[key_name], options[:each_class])
78
- else
79
- params[key_name]
80
- end
81
- end
82
-
83
- def class_param(param, klass)
84
- return nil if param.nil?
85
- klass.new(param)
86
- end
87
-
88
- def each_class_param(params, klass)
89
- return nil if params.nil?
90
- params.inject([]) do |result, param|
91
- result << class_param(param, klass)
92
- result
74
+ def init_attributes(accessors, params)
75
+ accessors.each do |key, options|
76
+ val = get_val(params, options[:name] || key, options)
77
+ set_attribute(key, val)
93
78
  end
94
79
  end
95
80
 
96
- def convert(json)
97
- return json if json.class == ActiveSupport::HashWithIndifferentAccess
98
- (json.class == Hash ? json : JSON.parse(json)).with_indifferent_access
99
- end
100
-
101
- def ruson_class?(value)
102
- value.class < Ruson::Base
103
- end
104
-
105
- def convert_ruson_to_hash_value(value)
106
- return value.to_hash if ruson_class?(value)
107
- value
108
- end
109
-
110
- def convert_array_to_hash_value(value)
111
- if value.instance_of?(Array)
112
- value.inject([]) { |result, v| result << convert_ruson_to_hash_value(v) }
113
- else
114
- convert_ruson_to_hash_value(value)
115
- end
81
+ def set_attribute(attr_name, val)
82
+ self.send("#{attr_name}=".to_sym, val)
116
83
  end
117
84
  end
118
85
  end
@@ -0,0 +1,14 @@
1
+ class Boolean
2
+ def self.new(value)
3
+ return value if value.kind_of?(TrueClass) || value.kind_of?(FalseClass)
4
+
5
+ case value
6
+ when 'true'
7
+ return true
8
+ when 'false'
9
+ return false
10
+ else
11
+ return false
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,5 @@
1
+ class Float
2
+ def self.new(value)
3
+ value.to_f
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ class Integer
2
+ def self.new(value)
3
+ value.to_i
4
+ end
5
+ end
@@ -0,0 +1,30 @@
1
+ module Ruson
2
+ module Converter
3
+ def convert_to_hash(accessors)
4
+ accessors.keys.inject({}) do |result, key|
5
+ value = send(key)
6
+ result[key.to_sym] = convert_array_to_hash_value(value)
7
+ result
8
+ end
9
+ end
10
+
11
+ private
12
+
13
+ def convert_array_to_hash_value(value)
14
+ if value.instance_of?(Array)
15
+ value.inject([]) { |result, v| result << convert_ruson_to_hash_value(v) }
16
+ else
17
+ convert_ruson_to_hash_value(value)
18
+ end
19
+ end
20
+
21
+ def convert_ruson_to_hash_value(value)
22
+ return value.to_hash if ruson_class?(value)
23
+ value
24
+ end
25
+
26
+ def ruson_class?(value)
27
+ value.class < Ruson::Base
28
+ end
29
+ end
30
+ end
data/lib/ruson/json.rb ADDED
@@ -0,0 +1,8 @@
1
+ module Ruson
2
+ module Json
3
+ def get_hash_from_json(json)
4
+ return json if json.class == ActiveSupport::HashWithIndifferentAccess
5
+ (json.class == Hash ? json : JSON.parse(json)).with_indifferent_access
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,28 @@
1
+ module Ruson
2
+ module Value
3
+ def get_val(params, key_name, options)
4
+ if !options[:class].nil?
5
+ class_param(params[key_name], options[:class])
6
+ elsif !options[:each_class].nil?
7
+ each_class_param(params[key_name], options[:each_class])
8
+ else
9
+ params[key_name]
10
+ end
11
+ end
12
+
13
+ private
14
+
15
+ def class_param(param, klass)
16
+ return nil if param.nil?
17
+ klass.new(param)
18
+ end
19
+
20
+ def each_class_param(params, klass)
21
+ return nil if params.nil?
22
+ params.inject([]) do |result, param|
23
+ result << class_param(param, klass)
24
+ result
25
+ end
26
+ end
27
+ end
28
+ end
data/lib/ruson/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Ruson
2
- VERSION = '1.0.0'
2
+ VERSION = '1.1.0'
3
3
  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: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - klriutsa
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-05-21 00:00:00.000000000 Z
11
+ date: 2019-05-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -85,6 +85,12 @@ files:
85
85
  - bin/setup
86
86
  - lib/ruson.rb
87
87
  - lib/ruson/base.rb
88
+ - lib/ruson/class/boolean.rb
89
+ - lib/ruson/class/float.rb
90
+ - lib/ruson/class/integer.rb
91
+ - lib/ruson/converter.rb
92
+ - lib/ruson/json.rb
93
+ - lib/ruson/value.rb
88
94
  - lib/ruson/version.rb
89
95
  - ruson.gemspec
90
96
  homepage: https://github.com/klriutsa/ruson