foraneus 0.0.13 → 0.0.14

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
- ---
2
- SHA1:
3
- metadata.gz: f3f668a304ebd112cf2651006dc871679cac85db
4
- data.tar.gz: 607492a6bd176a001eca2f2f0971e2f6f6dca73f
5
- SHA512:
6
- metadata.gz: 1954c53b5f18b2099d55a2e4c0d92dc09cb6cfca491e0db191b220a997cb8abedd580392edcb70c8ab3d80363d97b938fee4d6e99531d2fe13436b0074eb1f8c
7
- data.tar.gz: 8689562a31c5c06aade9b290f663539f0e8f042bddb1a71638b92a70f24944f853ee48fa3458b7310cfac8dc304b0d09db019023bded19bb9d6fd694b8d415f3
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 6a1af4481bfbdeb0cc0aa78900c6b7f4512d7a21
4
+ data.tar.gz: 58cd1c7f084558def0575754919263b6763ba17a
5
+ SHA512:
6
+ metadata.gz: 093a3fdc5eecea2934cd2ef01c88191bcf62fe317f61ed73519d8a77b4afcadd2ff96b5af17d4e5337e43da96bdd325861b6f94ca53d0a6642db6ffaf0aa5a03
7
+ data.tar.gz: cbf2a485c1df7a4da036256ea27f6a8097f7433cb8b4bcd3e5c922e1ef985b2003ed6a5e442f3755af1f89a66da393efd6c80f314787f54c6d9767223304fd1f
data/README.md CHANGED
@@ -251,12 +251,24 @@ Tests are written in MiniTest. To run them all just execute the following from y
251
251
  ruby spec/runner.rb
252
252
  ```
253
253
 
254
+ Execute the following when ruby 1.8.7:
255
+
256
+ ``` shell
257
+ ruby -rubygems spec/runner.rb
258
+ ```
259
+
254
260
  To run a specific test case:
255
261
 
256
262
  ``` shell
257
263
  ruby -Ispec -Ilib spec/lib/foraneus_spec.rb
258
264
  ```
259
265
 
266
+ When running under ruby 1.8.7:
267
+
268
+ ``` shell
269
+ ruby -rubygems -Ispec -Ilib spec/lib/foraneus_spec.rb
270
+ ```
271
+
260
272
  ## Code documentation
261
273
 
262
274
  Documentation is written in Yard. To see it in a browser, execute this command:
data/lib/foraneus.rb CHANGED
@@ -1,16 +1,19 @@
1
- require_relative 'foraneus/converters/boolean'
2
- require_relative 'foraneus/converters/date'
3
- require_relative 'foraneus/converters/decimal'
4
- require_relative 'foraneus/converters/float'
5
- require_relative 'foraneus/converters/integer'
6
- require_relative 'foraneus/converters/noop'
7
- require_relative 'foraneus/converters/string'
8
- require_relative 'foraneus/errors'
1
+ if RUBY_VERSION == '1.8.7'
2
+ require 'foraneus/compatibility/ruby-1.8.7'
3
+ end
4
+
5
+ require 'foraneus/converters/boolean'
6
+ require 'foraneus/converters/date'
7
+ require 'foraneus/converters/decimal'
8
+ require 'foraneus/converters/float'
9
+ require 'foraneus/converters/integer'
10
+ require 'foraneus/converters/noop'
11
+ require 'foraneus/converters/string'
12
+ require 'foraneus/errors'
9
13
 
10
14
  # Foraneus base class used to declare a data set, aka 'form'.
11
15
  class Foraneus
12
16
 
13
-
14
17
  # @api private
15
18
  def initialize
16
19
  @_ = {}
@@ -105,18 +108,9 @@ class Foraneus
105
108
 
106
109
  def self.create_instance
107
110
  instance = self.new
108
- spec = self
109
-
110
- instance.singleton_class.send(:attr_reader, self.accessors[:data])
111
111
 
112
- instance.instance_exec do
113
- instance.instance_variable_set(:"@#{spec.accessors[:data]}", {})
114
- end
115
-
116
- instance.singleton_class.send(:attr_reader, self.accessors[:errors])
117
- instance.instance_exec do
118
- instance.instance_variable_set(:"@#{spec.accessors[:errors]}", {})
119
- end
112
+ __singleton_attr_reader(instance, :data, {})
113
+ __singleton_attr_reader(instance, :errors, {})
120
114
 
121
115
  instance
122
116
  end
@@ -167,20 +161,7 @@ class Foraneus
167
161
  data.fetch(given_key, nil)
168
162
  end
169
163
 
170
- instance.send("#{field}=", v)
171
-
172
- if v.nil?
173
- v = converter.opts[:default]
174
- end
175
-
176
- s = if v.nil?
177
- nil
178
- else
179
- converter.raw(v)
180
- end
181
-
182
- instance[given_key] = s
183
- instance.send(self.accessors[:data])[given_key] = v
164
+ __raw_datum(given_key, v, instance, converter)
184
165
  end
185
166
 
186
167
  instance
@@ -232,6 +213,29 @@ class Foraneus
232
213
 
233
214
  foraneus[k] = v
234
215
 
216
+ v = __parse(k, v, converter)
217
+
218
+ foraneus.send("#{field}=", v)
219
+ foraneus.send(self.accessors[:data])[k] = v
220
+
221
+ rescue
222
+ error = Foraneus::Error.new($!.class.name, $!.message)
223
+ foraneus.send(self.accessors[:errors])[k] = error
224
+ end
225
+ private_class_method :__parse_raw_datum
226
+
227
+ # @api private
228
+ #
229
+ # Parses a raw value.
230
+ #
231
+ # @param [String, Symbol] field
232
+ # @param [String] v Raw value.
233
+ # @param converter
234
+ #
235
+ # @raise [KeyError] if converter requires a value but no one is given.
236
+ #
237
+ # @return [Object]
238
+ def self.__parse(field, v, converter)
235
239
  if v == '' && converter.opts.fetch(:blanks_as_nil, true)
236
240
  v = nil
237
241
  end
@@ -239,18 +243,55 @@ class Foraneus
239
243
  if v.nil? && converter.opts[:required]
240
244
  raise KeyError, "required parameter not found: #{field.inspect}"
241
245
  elsif v.nil?
242
- v = converter.opts[:default]
246
+ converter.opts[:default]
243
247
  else
244
- v = converter.parse(v)
248
+ converter.parse(v)
245
249
  end
250
+ end
251
+ private_class_method :__parse
246
252
 
247
- foraneus.send("#{field}=", v)
253
+ # @api private
254
+ #
255
+ # Obtains a raw representation of a value and assigns it to the corresponding field.
256
+ #
257
+ # It also registers errors if the conversion fails.
258
+ #
259
+ # @param [String, Symbol] k
260
+ # @param [String] v
261
+ # @param [Foraneus] foraneus
262
+ # @param [Converter] converter
263
+ def self.__raw_datum(k, v, foraneus, converter)
264
+ foraneus.send("#{k}=", v)
265
+
266
+ if v.nil?
267
+ v = converter.opts[:default]
268
+ end
269
+
270
+ s = unless v.nil?
271
+ converter.raw(v)
272
+ end
273
+
274
+ foraneus[k] = s
248
275
  foraneus.send(self.accessors[:data])[k] = v
276
+ end
277
+ private_class_method :__raw_datum
249
278
 
250
- rescue
251
- error = Foraneus::Error.new($!.class.name, $!.message)
252
- foraneus.send(self.accessors[:errors])[k] = error
279
+ # @api private
280
+ #
281
+ # Creates a singleton attribute reader on an instance.
282
+ #
283
+ # @param [Foraneus] instance
284
+ # @param [Symbol] attribute
285
+ # @param initial_value
286
+ def self.__singleton_attr_reader(instance, attribute, initial_value = nil)
287
+ spec = instance.class
288
+
289
+ instance.singleton_class.send(:attr_reader, spec.accessors[attribute])
290
+
291
+ instance.instance_exec do
292
+ instance.instance_variable_set(:"@#{spec.accessors[attribute]}", initial_value)
293
+ end
253
294
  end
254
- private_class_method :__parse_raw_datum
295
+ private_class_method :__singleton_attr_reader
255
296
 
256
297
  end
@@ -0,0 +1,12 @@
1
+ unless defined?(KeyError)
2
+ class KeyError < IndexError
3
+ end
4
+ end
5
+
6
+ if (Object.instance_methods & ['singleton_class', :singleton_class]).empty?
7
+ class Object
8
+ def singleton_class
9
+ class << self; self; end
10
+ end
11
+ end
12
+ end
@@ -13,10 +13,12 @@ class Foraneus
13
13
  @delimiter = opts[:delimiter]
14
14
  end
15
15
 
16
- # @raise [ArgumentError] with message 'invalid value for Integer(): ...'
16
+ # @raise [TypeError] with message 'invalid value for Integer(): ...'
17
17
  #
18
18
  # @return [Integer]
19
19
  def parse(s)
20
+ raise TypeError, "can't convert nil into Integer" if s.nil?
21
+
20
22
  s = s.gsub(@delimiter, '') if @delimiter
21
23
 
22
24
  Kernel.Integer(s)
@@ -22,7 +22,7 @@ describe Foraneus::Converters::Float do
22
22
  end
23
23
 
24
24
  describe 'with big ones' do
25
- let(:big_number) { (11 ** 20) + 0.33 }
25
+ let(:big_number) { (11 ** 11) + 0.33 }
26
26
  let(:raw_big_number) { big_number.to_s }
27
27
 
28
28
  it 'also returns a float number' do
@@ -197,7 +197,7 @@ describe Foraneus do
197
197
  end
198
198
  end
199
199
 
200
- an_absent_parameters_value_handler = ->(missing_value) do
200
+ an_absent_parameters_value_handler = lambda do |missing_value|
201
201
  subject { form_spec.parse(:delay => missing_value) }
202
202
 
203
203
  it 'parses' do
data/spec/runner.rb CHANGED
@@ -1,6 +1,4 @@
1
- require 'minitest/autorun'
2
-
3
- add_to_load_path = ->(path) {
1
+ add_to_load_path = lambda { |path|
4
2
  unless $LOAD_PATH.include?(path)
5
3
  $LOAD_PATH << path
6
4
  end
data/spec/spec_helper.rb CHANGED
@@ -1,3 +1,5 @@
1
+ #require 'rubygems'
2
+
1
3
  require 'minitest/autorun'
2
4
  require 'minitest/spec'
3
5
  require 'minitest/unit'
metadata CHANGED
@@ -1,53 +1,57 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: foraneus
3
- version: !ruby/object:Gem::Version
4
- version: 0.0.13
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.14
5
5
  platform: ruby
6
- authors:
6
+ authors:
7
7
  - Gianfranco Zas
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-11-28 00:00:00.000000000 Z
12
- dependencies:
13
- - !ruby/object:Gem::Dependency
14
- name: yard-kramdown
15
- requirement: !ruby/object:Gem::Requirement
16
- requirements:
17
- - - ">="
18
- - !ruby/object:Gem::Version
19
- version: '0'
20
- type: :development
11
+
12
+ date: 2014-12-04 00:00:00 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: minitest
21
16
  prerelease: false
22
- version_requirements: !ruby/object:Gem::Requirement
23
- requirements:
24
- - - ">="
25
- - !ruby/object:Gem::Version
26
- version: '0'
27
- - !ruby/object:Gem::Dependency
17
+ requirement: &id001 !ruby/object:Gem::Requirement
18
+ requirements:
19
+ - &id002
20
+ - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: "0"
23
+ type: :development
24
+ version_requirements: *id001
25
+ - !ruby/object:Gem::Dependency
28
26
  name: yard
29
- requirement: !ruby/object:Gem::Requirement
30
- requirements:
31
- - - ">="
32
- - !ruby/object:Gem::Version
33
- version: '0'
27
+ prerelease: false
28
+ requirement: &id003 !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - *id002
34
31
  type: :development
32
+ version_requirements: *id003
33
+ - !ruby/object:Gem::Dependency
34
+ name: kramdown
35
35
  prerelease: false
36
- version_requirements: !ruby/object:Gem::Requirement
37
- requirements:
38
- - - ">="
39
- - !ruby/object:Gem::Version
40
- version: '0'
36
+ requirement: &id004 !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - *id002
39
+ type: :development
40
+ version_requirements: *id004
41
41
  description: Provides two way transformation mechanisms to external data.
42
42
  email: snmgian@gmail.com
43
43
  executables: []
44
+
44
45
  extensions: []
46
+
45
47
  extra_rdoc_files: []
46
- files:
48
+
49
+ files:
47
50
  - COPYING
48
51
  - COPYING.LESSER
49
52
  - README.md
50
53
  - lib/foraneus.rb
54
+ - lib/foraneus/compatibility/ruby-1.8.7.rb
51
55
  - lib/foraneus/converters/boolean.rb
52
56
  - lib/foraneus/converters/date.rb
53
57
  - lib/foraneus/converters/decimal.rb
@@ -68,30 +72,31 @@ files:
68
72
  - spec/spec_helper.rb
69
73
  - spec/spec_helper_its.rb
70
74
  homepage: https://github.com/snmgian/foraneus
71
- licenses:
75
+ licenses:
72
76
  - LGPL
73
77
  metadata: {}
78
+
74
79
  post_install_message:
75
80
  rdoc_options: []
76
- require_paths:
81
+
82
+ require_paths:
77
83
  - lib
78
- required_ruby_version: !ruby/object:Gem::Requirement
79
- requirements:
80
- - - ">="
81
- - !ruby/object:Gem::Version
82
- version: '0'
83
- required_rubygems_version: !ruby/object:Gem::Requirement
84
- requirements:
84
+ required_ruby_version: !ruby/object:Gem::Requirement
85
+ requirements:
85
86
  - - ">="
86
- - !ruby/object:Gem::Version
87
- version: '0'
87
+ - !ruby/object:Gem::Version
88
+ version: 1.8.7
89
+ required_rubygems_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - *id002
88
92
  requirements: []
93
+
89
94
  rubyforge_project:
90
- rubygems_version: 2.2.2
95
+ rubygems_version: 2.4.5
91
96
  signing_key:
92
97
  specification_version: 4
93
98
  summary: Transforms external data.
94
- test_files:
99
+ test_files:
95
100
  - spec/lib/foraneus_spec.rb
96
101
  - spec/lib/foraneus/converters/noop_converter_spec.rb
97
102
  - spec/lib/foraneus/converters/decimal_converter_spec.rb
@@ -103,3 +108,4 @@ test_files:
103
108
  - spec/spec_helper.rb
104
109
  - spec/spec_helper_its.rb
105
110
  - spec/runner.rb
111
+ has_rdoc: