ruson 1.1.0 → 1.2.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: 39dd7c6ce4a45497f8925a29bd35bc4cead7c1a7de5b540f82ac3792be969305
4
- data.tar.gz: ddd14dd0b2f08c7f2fc2a6502908234411ae6a28e0eb6d222f5736d9668411bb
3
+ metadata.gz: df8812fa2f70ad440c16a4647961a68955b3909d481db38bd6271534afce154c
4
+ data.tar.gz: d8877e9e6f435d319a736e0157845dae2264ba6468a2ed918b158601de2a8881
5
5
  SHA512:
6
- metadata.gz: b908373f13bca616112e8bc1ef73d237cd1a44aa06102e9c4b7309befb2358530ea6622688a088f1293b63eb387798561bc9363453d7962b01b4df67169b3644
7
- data.tar.gz: cb648ad162cbfbc5a25d3814ec686d116ddcfed0e75c59e7137cf5492b13bbfae8e940842c25c8bf075b9674d038a7cbfeab058bd350efc05ef755aa5a53d0f4
6
+ metadata.gz: 5883b0fc7b4e95a713a906f83c1d3f07a2761cd1cf4a246f2ae6ee2591322fd24791c8ce99f37dc6e62338c4da55494d160428206838de34154e5402efcad409
7
+ data.tar.gz: 20fb24a41b36de01dcb8bfcb32a864ed88808f49229617a610ceb5ced0ee8a0ca3bf4204c5100120349fee63f76a79948cd09f7d4b16afcb02e34dc8a0afd44e
data/README.md CHANGED
@@ -65,6 +65,26 @@ post = Post.new(json)
65
65
  post.url #=> 'http://sample.com'
66
66
  ```
67
67
 
68
+ ### nilable
69
+
70
+ post.json
71
+ ```json
72
+ {
73
+ "title": "Ruson",
74
+ "post_url": "http://sample.com"
75
+ }
76
+ ```
77
+
78
+ ```ruby
79
+ class Post < Ruson::Base
80
+ field :title, nilable: false
81
+ field :url, name: 'post_url'
82
+ end
83
+
84
+ json = File.read('post.json')
85
+ post = Post.new(json) #=> Ruson::NotNilException
86
+ ```
87
+
68
88
  ### nested class
69
89
 
70
90
  #### class
@@ -9,6 +9,9 @@ require 'ruson/class/float'
9
9
  require 'ruson/converter'
10
10
  require 'ruson/json'
11
11
  require 'ruson/value'
12
+ require 'ruson/nilable'
13
+
14
+ require 'ruson/error'
12
15
 
13
16
  module Ruson
14
17
  class Base
@@ -53,6 +56,7 @@ module Ruson
53
56
  include Ruson::Converter
54
57
  include Ruson::Json
55
58
  include Ruson::Value
59
+ include Ruson::Nilable
56
60
 
57
61
  def initialize(json, root_key: nil)
58
62
  params = get_hash_from_json(json)
@@ -73,7 +77,11 @@ module Ruson
73
77
 
74
78
  def init_attributes(accessors, params)
75
79
  accessors.each do |key, options|
76
- val = get_val(params, options[:name] || key, options)
80
+ key_name = options[:name] || key
81
+ value = params[key_name]
82
+
83
+ check_nilable(value, options)
84
+ val = get_val(value, options)
77
85
  set_attribute(key, val)
78
86
  end
79
87
  end
@@ -0,0 +1,4 @@
1
+ module Ruson
2
+ class NotNilException < StandardError
3
+ end
4
+ end
@@ -0,0 +1,16 @@
1
+ module Ruson
2
+ module Nilable
3
+ def check_nilable(value, options)
4
+ return if nilable?(options)
5
+ raise Ruson::NotNilException if value.nil?
6
+ end
7
+
8
+ private
9
+
10
+ def nilable?(options)
11
+ nilable = options[:nilable]
12
+ return true if nilable.nil?
13
+ nilable
14
+ end
15
+ end
16
+ end
@@ -1,12 +1,12 @@
1
1
  module Ruson
2
2
  module Value
3
- def get_val(params, key_name, options)
3
+ def get_val(value, options)
4
4
  if !options[:class].nil?
5
- class_param(params[key_name], options[:class])
5
+ class_param(value, options[:class])
6
6
  elsif !options[:each_class].nil?
7
- each_class_param(params[key_name], options[:each_class])
7
+ each_class_param(value, options[:each_class])
8
8
  else
9
- params[key_name]
9
+ value
10
10
  end
11
11
  end
12
12
 
@@ -1,3 +1,3 @@
1
1
  module Ruson
2
- VERSION = '1.1.0'
2
+ VERSION = '1.2.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.1.0
4
+ version: 1.2.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-31 00:00:00.000000000 Z
11
+ date: 2019-09-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -89,7 +89,9 @@ files:
89
89
  - lib/ruson/class/float.rb
90
90
  - lib/ruson/class/integer.rb
91
91
  - lib/ruson/converter.rb
92
+ - lib/ruson/error.rb
92
93
  - lib/ruson/json.rb
94
+ - lib/ruson/nilable.rb
93
95
  - lib/ruson/value.rb
94
96
  - lib/ruson/version.rb
95
97
  - ruson.gemspec