otoroshi 0.1.0 → 0.1.1

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: 12cba26aea6aca35ae46692ed5f9e9e4a72c3231651a132c25e793d697c17dbc
4
- data.tar.gz: eb24b1df208ef59c64f3a2ea045a52b578969d75fb6cef6907189b6512bb30e1
3
+ metadata.gz: 8928c55b726e2db1fb3bd19948c23be9c8fb0175650ae1f65cd0489bf7751c38
4
+ data.tar.gz: db9cc8034319672be477f1352508e6258d9ce105d6eedca6501630776e196407
5
5
  SHA512:
6
- metadata.gz: cc8bd50a648a681442722ad6456535d76fe3bc2dfa2bb2e4041a1548f710c64634c9ebc10e5e02fd7b397f608f8abbc34f17a6aa4dc7dd190780e0d77cd9e60a
7
- data.tar.gz: ab934b7e39d816afcaae0bb07d0a142904aecb11ece6e75fb4590974483fa9bcc476767583a2c74ae907d2e77afb04501e35044e9e999e89197025881c0a863b
6
+ metadata.gz: 44856f1f0d75272e70a49d07db5d7ce5218849ca9a0a04164c875d9277f18ce2afddc2cb254e8d9079232a23f384ec58c9ea0602ea6e170504ad1bd59e280895
7
+ data.tar.gz: 7978129f51d5ef3b224efa547e0ede889ffd4010ca8f571dde79830190f21b7a69defd70f8f44059ef1daf8647310fd9583944e7bbbcca05753417b1fec9d72a
@@ -17,11 +17,6 @@ module Otoroshi
17
17
  # self.number = number
18
18
  # self.message = message
19
19
  # self.fruits = fruits
20
- # bind = self
21
- # @fruits.singleton_class.send(:define_method, :<<) do |v|
22
- # bind.send(:"validate_fruits!", [v])
23
- # push(v)
24
- # end
25
20
  # end
26
21
  # RUBY
27
22
  def draw(properties)
@@ -50,8 +45,7 @@ module Otoroshi
50
45
  def draw
51
46
  <<~RUBY
52
47
  def initialize(#{initialize_parameters})
53
- #{initialize_assignments}
54
- #{initialize_push_singletons}
48
+ #{initialize_body}
55
49
  end
56
50
  RUBY
57
51
  end
@@ -120,64 +114,12 @@ module Otoroshi
120
114
  # self.foo = foo
121
115
  # self.bar = bar
122
116
  # RUBY
123
- def initialize_assignments
117
+ def initialize_body
124
118
  assignments =
125
119
  properties.keys.map do |name|
126
120
  "self.#{name} = #{name}"
127
121
  end
128
122
  assignments.join("\n ")
129
123
  end
130
-
131
- # Generates push singleton for each array property
132
- #
133
- # @return [String]
134
- #
135
- # @example
136
- # <<-RUBY
137
- # bind = self
138
- # @fruits.singleton_class.send(:define_method, :<<) do |v|
139
- # bind.send(:"validate_fruits!", [v])
140
- # push(v)
141
- # end
142
- # @numbers.singleton_class.send(:define_method, :<<) do |v|
143
- # bind.send(:"validate_numbers!", [v])
144
- # push(v)
145
- # end
146
- # RUBY
147
- def initialize_push_singletons
148
- collections =
149
- properties.select do |_, options|
150
- options[:type].is_a?(Array) || options[:type] == Array
151
- end
152
- return if collections.empty?
153
-
154
- singletons =
155
- collections.keys.map do |name|
156
- initialize_push_singleton(name)
157
- end
158
- # assign self to a variable so the instance is accessible from the singleton scope
159
- ['bind = self', singletons.join].join("\n")
160
- end
161
-
162
- # Generates singleton on collection instance variable to overide <<
163
- # so value is validated before being added to the collection
164
- #
165
- # @return [String]
166
- #
167
- # @example
168
- # <<-RUBY
169
- # @fruits.singleton_class.send(:define_method, :<<) do |v|
170
- # bind.send(:"validate_fruits!", [v])
171
- # push(v)
172
- # end
173
- # RUBY
174
- def initialize_push_singleton(name)
175
- <<-RUBY
176
- @#{name}.singleton_class.send(:define_method, :<<) do |v|
177
- bind.send(:"validate_#{name}!", [v])
178
- push(v)
179
- end
180
- RUBY
181
- end
182
124
  end
183
125
  end
@@ -47,10 +47,10 @@ module Otoroshi
47
47
  add_to_properties(name, type, allow_nil, default)
48
48
  expected_type = type.is_a?(Array) ? type.first || Object : type
49
49
  collection = expected_type == Array || type.is_a?(Array)
50
- define_validate_type!(name, expected_type, collection, allow_nil)
51
- define_validate_one_of!(name, collection, one_of, allow_nil)
52
- define_validate_assertion!(name, collection, assert, allow_nil)
53
- define_validate!(name)
50
+ define_validate_type(name, expected_type, collection, allow_nil)
51
+ define_validate_one_of(name, collection, one_of, allow_nil)
52
+ define_validate_assertion(name, collection, assert, allow_nil)
53
+ define_validate(name)
54
54
  define_getter(name)
55
55
  define_setter(name)
56
56
  class_eval Initializer.draw(properties), __FILE__, __LINE__ + 1
@@ -86,18 +86,18 @@ module Otoroshi
86
86
  #
87
87
  # Given name = :score, type = Integer, allow_nil = false
88
88
  #
89
- # def validate_score_type!(value)
89
+ # def validate_score_type(value)
90
90
  # return if Integer.nil? || false && value.nil?
91
91
  # return if value.is_a? Integer
92
92
  #
93
93
  # raise Otoroshi::TypeError, :score, Integer
94
94
  # end
95
- def define_validate_type!(name, type, collection, allow_nil)
95
+ def define_validate_type(name, type, collection, allow_nil)
96
96
  validator = validate_type?(name, type, collection)
97
- define_method :"validate_#{name}_type!" do |value|
97
+ define_method :"validate_#{name}_type" do |value|
98
98
  allow_nil && value.nil? || validator.call(value)
99
99
  end
100
- private :"validate_#{name}_type!"
100
+ private :"validate_#{name}_type"
101
101
  end
102
102
 
103
103
  # Lambda that validates (value) respects the type condition
@@ -119,18 +119,18 @@ module Otoroshi
119
119
  #
120
120
  # Given name = :side, collection = false, one_of = [:left, :right], allow_nil = false
121
121
  #
122
- # def validate_side_type!(value)
122
+ # def validate_side_type(value)
123
123
  # return if false && value.nil?
124
124
  # return if [:left, :right].include? value
125
125
  #
126
126
  # raise Otoroshi::OneOfError, :side, [:left, :right]
127
127
  # end
128
- def define_validate_one_of!(name, collection, one_of, allow_nil)
128
+ def define_validate_one_of(name, collection, one_of, allow_nil)
129
129
  validator = validate_one_of?(name, one_of, collection)
130
- define_method(:"validate_#{name}_one_of!") do |value|
130
+ define_method(:"validate_#{name}_one_of") do |value|
131
131
  allow_nil && value.nil? || validator.call(value)
132
132
  end
133
- private :"validate_#{name}_one_of!"
133
+ private :"validate_#{name}_one_of"
134
134
  end
135
135
 
136
136
  # Lambda that validates (value) respects the one_of condition
@@ -153,18 +153,18 @@ module Otoroshi
153
153
  #
154
154
  # Given name = :score, assert = ->(v) { v >= 0 }, allow_nil = false
155
155
  #
156
- # def validate_score_assertion!(value)
156
+ # def validate_score_assertion(value)
157
157
  # return if false && value.nil?
158
158
  # return if value >= 0
159
159
  #
160
160
  # raise Otoroshi::AssertError, :score
161
161
  # end
162
- def define_validate_assertion!(name, collection, assert, allow_nil)
162
+ def define_validate_assertion(name, collection, assert, allow_nil)
163
163
  validator = validate_assert?(name, assert, collection)
164
- define_method :"validate_#{name}_assertion!" do |value|
164
+ define_method :"validate_#{name}_assertion" do |value|
165
165
  allow_nil && value.nil? || validator.call(value)
166
166
  end
167
- private :"validate_#{name}_assertion!"
167
+ private :"validate_#{name}_assertion"
168
168
  end
169
169
 
170
170
  # Lambda that validates (value) respects the assert condition
@@ -182,15 +182,15 @@ module Otoroshi
182
182
  # Given name = :score
183
183
  #
184
184
  # def validate_score!(value)
185
- # validate_score_type!(value)
186
- # validate_score_one_of!(value)
185
+ # validate_score_type(value)
186
+ # validate_score_one_of(value)
187
187
  # validate_score_assert(value)
188
188
  # end
189
- def define_validate!(name)
189
+ def define_validate(name)
190
190
  define_method :"validate_#{name}!" do |value|
191
- __send__(:"validate_#{name}_type!", value)
192
- __send__(:"validate_#{name}_one_of!", value)
193
- __send__(:"validate_#{name}_assertion!", value)
191
+ __send__(:"validate_#{name}_type", value)
192
+ __send__(:"validate_#{name}_one_of", value)
193
+ __send__(:"validate_#{name}_assertion", value)
194
194
  end
195
195
  private :"validate_#{name}!"
196
196
  end
@@ -203,7 +203,7 @@ module Otoroshi
203
203
  # instance_variable_get(@score)
204
204
  # end
205
205
  def define_getter(name)
206
- define_method(name) { instance_variable_get("@#{name}") }
206
+ define_method(name) { instance_variable_get("@#{name}").clone.freeze }
207
207
  end
208
208
 
209
209
  # Defines a setter
@@ -211,7 +211,7 @@ module Otoroshi
211
211
  # Given name = :score
212
212
  #
213
213
  # def score=(value)
214
- # validate_score_type!(value)
214
+ # validate_score_type(value)
215
215
  # validate_score!(value)
216
216
  # instance_variable_set(@score, value)
217
217
  # end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: otoroshi
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Edouard Piron