foraneus 0.0.2 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- YjUyZWQ0OTM3OTA4MWE0MzBlNmEyZmUzYWI5ZGM2ZTdlYzBiMTJhOQ==
4
+ MzE1YmEwZTI1OThmZDhkNjIxOGIyM2ZkYWQzMDJjMjYxYmUxYWU4NA==
5
5
  data.tar.gz: !binary |-
6
- NTcyZjkzYjM3NWE3N2QzYjQ3MzYyNjMyOGQ3ZTBiZjE1Mjc5Y2VmOQ==
6
+ MjVjZjQwOTQ2MGE2OWQ5MWEzN2MyMDU3ZWVhZTEyZGVhOTkyMWJjNA==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- ZTMzMzNhYjg4MmJkYTZkODhmZTllYTgwN2ZhZDQ0NjA5OTM1NWUxNDU5YjA5
10
- ZjQ5YzY1NjZhMDc4OThlNmJkN2YxNWE4MDQ0MzQ5YjBjMjMwODQ1MTczZTJl
11
- NjExNmI0OWQyMWNlOTNmYjA5NzE0YzUwOWViYjQwZWY2NGU4Mzk=
9
+ ZjM1OTA2MDZmNjZjOWRkYmUyODVmZjRmMTc2N2U1NGY1MTc2OTI1NzM3MTJj
10
+ YzlhNDY1OWVmYTc5YzEzMzE5NGY4ZDE2MDg5M2EyOGExMzE0YzM1OGQyYTU2
11
+ N2JlYTA1MTY1YTFkMzQwNTY2YjJiY2I3OWM2ODJhYjQ2MGI1NmY=
12
12
  data.tar.gz: !binary |-
13
- ZjllNzU3NzQxNWUzZGEzOTk2OWMwNWUwZTY1OGQ4NTkxYWNiZWI1Mjg1N2Jh
14
- YzMxZjc3YTI1OGM1MWViNzI4ZDg2ZTdjYzYxOWE0YTM1Yzc0ODMxNDI2MDJh
15
- MzcxMzVlYWE0ZTM0OThiYmI1NGY1YmUxZmJlOTc1OTI1MmVkZDM=
13
+ YzEzM2VkYWQ5Y2NlOTU5MTMwY2U4MDliOTg2ZTkzNmMzZTBhMGY2NDgwZTVi
14
+ OWJhYjlhOGFjNzZiZDdkMzdiMDcwNGQ2NjVhZGE4OWIxNTc2ZGYxMGFjYjJi
15
+ OTQ1NWI4YzIxYmNlMDgwYmFkYjRiNmM1OTQ0MzQ1OGJiOGIwYzE=
data/README.md CHANGED
@@ -2,12 +2,13 @@
2
2
 
3
3
  ## Usage
4
4
 
5
- ``` ruby
6
- class MyForm < Foraneus
7
- integer :delay
8
- float :duration
9
- end
10
- ```
5
+ - Declaration:
6
+ ``` ruby
7
+ class MyForm < Foraneus
8
+ integer :delay
9
+ float :duration
10
+ end
11
+ ```
11
12
 
12
13
  - From the outside:
13
14
 
@@ -22,7 +23,7 @@ end
22
23
 
23
24
  ``` ruby
24
25
  form.data # => { :delay => 5, :duration => 2.14 }
25
- form[] # => { :delay => '5', :duration => '2.14' }
26
+ form[] # => { :delay => '5', :duration => '2.14' }
26
27
  ```
27
28
 
28
29
  ``` ruby
@@ -30,7 +31,7 @@ end
30
31
  form.errors # => {}
31
32
  ```
32
33
 
33
- - From the inside
34
+ - From the inside:
34
35
 
35
36
  ``` ruby
36
37
  form = MyForm.new
@@ -41,11 +42,11 @@ end
41
42
  form[:delay] # => nil
42
43
  ```
43
44
 
44
- - From the inside
45
+ - From the inside:
45
46
 
46
47
  ``` ruby
47
48
  form = MyForm.raw(:delay => 5, :duration => 2.14)
48
- ``` ruby
49
+ ```
49
50
 
50
51
  ``` ruby
51
52
  form.delay # => 5
@@ -54,9 +55,28 @@ end
54
55
 
55
56
  ``` ruby
56
57
  form.data # => { :delay => 5, :duration => 2.14 }
57
- form[] # => { :delay => '5', :duration => '2.14' }
58
+ form[] # => { :delay => '5', :duration => '2.14' }
59
+ ```
60
+
61
+ ## Installation
62
+
63
+ - Install `foraneus` as a gem.
64
+
65
+ ``` shell
66
+ gem install foraneus
67
+ ```
68
+
69
+ ## Running tests
70
+
71
+ Tests are written in RSpec. To run them all just execute the following from your command line:
72
+
73
+ ``` shell
74
+ rspec
58
75
  ```
59
76
 
77
+ ## Badges
78
+
79
+ [![Build Status](https://travis-ci.org/snmgian/foraneus.svg?branch=master)](https://travis-ci.org/snmgian/foraneus) [![Code Climate](https://codeclimate.com/github/snmgian/foraneus.png)](https://codeclimate.com/github/snmgian/foraneus)
60
80
 
61
81
  ## License
62
82
 
@@ -42,7 +42,6 @@ class Foraneus
42
42
 
43
43
  "#{s}#{zeros}"
44
44
  end
45
-
46
45
  end
47
46
 
48
47
  end
@@ -2,12 +2,54 @@ class Foraneus
2
2
  module Converters
3
3
 
4
4
  class Float
5
+
6
+ DEFAULT_SEPARATOR = '.'
7
+
8
+ DELIMITED_REGEX = /(\d)(?=(\d\d\d)+(?!\d))/
9
+
10
+ def initialize(opts = {})
11
+ @delimiter = opts[:delimiter]
12
+ @precision = opts[:precision]
13
+ @separator = opts[:separator] || DEFAULT_SEPARATOR
14
+ end
15
+
5
16
  def parse(s)
6
- Kernel.Float(s)
17
+ if s == ''
18
+ raise ArgumentError, 'invalid value for Float(): ""'
19
+ end
20
+
21
+ parts = s.split(@separator)
22
+
23
+ integer_part = parts[0] || '0'
24
+
25
+ if @delimiter
26
+ integer_part.gsub!(@delimiter, '')
27
+ end
28
+
29
+ fractional_part = parts[1] || '0'
30
+
31
+ Kernel.Float("#{integer_part}.#{fractional_part}")
7
32
  end
8
33
 
9
34
  def raw(v)
10
- v.to_s
35
+ left, right = v.to_s.split('.')
36
+
37
+ if @precision && right.length < @precision
38
+ right = add_trailing_zeros(right, @precision - right.length)
39
+ end
40
+
41
+ if @delimiter
42
+ left.gsub!(DELIMITED_REGEX) { "#{$1}#{@delimiter}" }
43
+ end
44
+
45
+ "#{left}#{@separator}#{right}"
46
+ end
47
+
48
+ private
49
+ def add_trailing_zeros(s, n)
50
+ zeros = '0' * n
51
+
52
+ "#{s}#{zeros}"
11
53
  end
12
54
  end
13
55
 
@@ -2,12 +2,26 @@ class Foraneus
2
2
  module Converters
3
3
 
4
4
  class Integer
5
+ DELIMITED_REGEX = /(\d)(?=(\d\d\d)+(?!\d))/
6
+
7
+ def initialize(opts = {})
8
+ @delimiter = opts[:delimiter]
9
+ end
10
+
5
11
  def parse(s)
12
+ s = s.gsub(@delimiter, '') if @delimiter
13
+
6
14
  Kernel.Integer(s)
7
15
  end
8
16
 
9
17
  def raw(v)
10
- v.to_s
18
+ s = v.to_s
19
+
20
+ if @delimiter
21
+ s.gsub!(DELIMITED_REGEX) { "#{$1}#{@delimiter}" }
22
+ end
23
+
24
+ s
11
25
  end
12
26
  end
13
27
 
data/lib/foraneus.rb CHANGED
@@ -66,29 +66,36 @@ class Foraneus
66
66
  parsed_data = {}
67
67
 
68
68
  raw_data.each do |k, v|
69
- field = k.to_s
70
- converter = fields[field]
71
- next unless converter
72
-
73
- instance[k] = v
74
- begin
75
- v = if v.nil?
76
- nil
77
- else
78
- converter.parse(v)
79
- end
80
-
81
- instance.send("#{field}=", v)
82
- instance.data[k] = v
83
- rescue
84
- error = Foraneus::Error.new($!.class.name, $!.message)
85
- instance.instance_variable_get(:@errors)[k] = error
86
- end
69
+ __parse_raw_datum(instance, k, v)
87
70
  end
88
71
 
89
72
  instance
90
73
  end
91
74
 
75
+ # @param [Foraneus] foraneus
76
+ # @param [String, Symbol] k
77
+ # @param [String] v
78
+ def self.__parse_raw_datum(foraneus, k, v)
79
+ field = k.to_s
80
+ converter = fields[field]
81
+
82
+ return unless converter
83
+
84
+ foraneus[k] = v
85
+
86
+ unless v.nil?
87
+ v = converter.parse(v)
88
+ end
89
+
90
+ foraneus.send("#{field}=", v)
91
+ foraneus.data[k] = v
92
+
93
+ rescue
94
+ error = Foraneus::Error.new($!.class.name, $!.message)
95
+ foraneus.instance_variable_get(:@errors)[k] = error
96
+ end
97
+ private_class_method :__parse_raw_datum
98
+
92
99
  def self.raw(data)
93
100
  instance = self.new
94
101
 
@@ -66,12 +66,17 @@ describe Foraneus::Converters::Decimal do
66
66
  Foraneus::Converters::Decimal.new(:precision => 2)
67
67
  }
68
68
 
69
- it 'x' do
69
+ it 'fills with zeros when value precision is smaller than converter precision' do
70
70
  n = BigDecimal.new('3.1')
71
71
  converter.raw(n).should eq('3.10')
72
72
  end
73
73
 
74
- it 'y' do
74
+ it 'does not affect the representation when precision and converter precision are both equal' do
75
+ n = BigDecimal.new('3.14')
76
+ converter.raw(n).should eq('3.14')
77
+ end
78
+
79
+ it 'does not truncate the representation when precision is larger than converter precision' do
75
80
  n = BigDecimal.new('3.145')
76
81
  converter.raw(n).should eq('3.145')
77
82
  end
@@ -37,6 +37,26 @@ describe Foraneus::Converters::Float do
37
37
  end
38
38
  end
39
39
 
40
+ context 'when separator and delimiter are given' do
41
+ subject(:converter) {
42
+ Foraneus::Converters::Float.new(:delimiter => '.', :separator => ',')
43
+ }
44
+
45
+ it 'parses a float representation' do
46
+ s = '1.234.567,89'
47
+ n = 1_234_567.89
48
+
49
+ converter.parse(s).should eq(n)
50
+ end
51
+
52
+ it 'parses a float representation when no integer part' do
53
+ s = ',56'
54
+ n = 0.56
55
+
56
+ converter.parse(s).should eq(n)
57
+ end
58
+ end
59
+
40
60
  context 'with invalid values' do
41
61
  let(:raw_invalid) { 'INVALID' }
42
62
 
@@ -68,5 +88,40 @@ describe Foraneus::Converters::Float do
68
88
  it 'returns a string representation' do
69
89
  subject.raw(2.34).should eq('2.34')
70
90
  end
91
+
92
+ context 'when separator and delimiter are given' do
93
+ subject(:converter) {
94
+ Foraneus::Converters::Float.new(:delimiter => '.', :separator => ',')
95
+ }
96
+
97
+ it 'returns a float representation' do
98
+ n = 1_234_567.89
99
+ s = '1.234.567,89'
100
+
101
+ converter.raw(n).should eq(s)
102
+ end
103
+ end
104
+
105
+ context 'when precision is given' do
106
+ subject(:converter) {
107
+ Foraneus::Converters::Float.new(:precision => 2)
108
+ }
109
+
110
+ it 'fills with zeros when value precision is smaller than converter precision' do
111
+ n = 3.1
112
+ converter.raw(n).should eq('3.10')
113
+ end
114
+
115
+ it 'does not affect the representation when precision and converter precision are both equal' do
116
+ n = 3.14
117
+ converter.raw(n).should eq('3.14')
118
+ end
119
+
120
+ it 'does not truncate the representation when precision is larger than converter precision' do
121
+ n = 3.145
122
+ converter.raw(n).should eq('3.145')
123
+ end
124
+ end
125
+
71
126
  end
72
127
  end
@@ -37,6 +37,19 @@ describe Foraneus::Converters::Integer do
37
37
  end
38
38
  end
39
39
 
40
+ context 'when delimiter is given' do
41
+ subject(:converter) {
42
+ Foraneus::Converters::Integer.new(:delimiter => '.')
43
+ }
44
+
45
+ it 'parses an integer representation' do
46
+ s = '1.234.567'
47
+ n = 1_234_567
48
+
49
+ converter.parse(s).should eq(n)
50
+ end
51
+ end
52
+
40
53
  context 'with invalid values' do
41
54
  let(:raw_invalid) { 'INVALID' }
42
55
 
@@ -68,6 +81,20 @@ describe Foraneus::Converters::Integer do
68
81
  it 'returns a string representation' do
69
82
  subject.raw(2).should eq('2')
70
83
  end
84
+
85
+ context 'when delimiter is given' do
86
+ subject(:converter) {
87
+ Foraneus::Converters::Integer.new(:delimiter => '.')
88
+ }
89
+
90
+ it 'parses an integer representation' do
91
+ n = 1_234_567
92
+ s = '1.234.567'
93
+
94
+ converter.raw(n).should eq(s)
95
+ end
96
+ end
97
+
71
98
  end
72
99
 
73
100
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: foraneus
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gianfranco Zas
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-05-04 00:00:00.000000000 Z
11
+ date: 2014-05-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec