foraneus 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,72 @@
1
+ require 'spec_helper'
2
+
3
+ describe Foraneus::Converters::Float do
4
+
5
+ describe '#parse' do
6
+ context 'with valid values' do
7
+ let(:number) { 2.3412 }
8
+ let(:raw_number) { number.to_s }
9
+
10
+ it 'returns a float number' do
11
+ parsed = subject.parse(raw_number)
12
+
13
+ parsed.should be_a(Float)
14
+ end
15
+
16
+ it 'parses the number' do
17
+ parsed = subject.parse(raw_number)
18
+
19
+ parsed.should == number
20
+ end
21
+
22
+ context 'with big ones' do
23
+ let(:big_number) { (11 ** 20) + 0.33 }
24
+ let(:raw_big_number) { big_number.to_s }
25
+
26
+ it 'also returns a float number' do
27
+ parsed = subject.parse(raw_big_number)
28
+
29
+ parsed.should be_a(Float)
30
+ end
31
+
32
+ it 'also parses the number' do
33
+ parsed = subject.parse(raw_big_number)
34
+
35
+ parsed.should == big_number
36
+ end
37
+ end
38
+ end
39
+
40
+ context 'with invalid values' do
41
+ let(:raw_invalid) { 'INVALID' }
42
+
43
+ it 'raises an error' do
44
+ expect {
45
+ subject.parse(raw_invalid)
46
+ }.to raise_error
47
+ end
48
+ end
49
+
50
+ context 'with empty values' do
51
+ it 'raises an error' do
52
+ expect {
53
+ subject.parse('')
54
+ }.to raise_error
55
+ end
56
+ end
57
+
58
+ context 'with nil values' do
59
+ it 'raises an error' do
60
+ expect {
61
+ subject.parse(nil)
62
+ }.to raise_error
63
+ end
64
+ end
65
+ end
66
+
67
+ describe '#raw' do
68
+ it 'returns a string representation' do
69
+ subject.raw(2.34).should eq('2.34')
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,73 @@
1
+ require 'spec_helper'
2
+
3
+ describe Foraneus::Converters::Integer do
4
+
5
+ describe '#parse' do
6
+ context 'with valid values' do
7
+ let(:number) { 2 }
8
+ let(:raw_number) { number.to_s }
9
+
10
+ it 'returns an integer number' do
11
+ parsed = subject.parse(raw_number)
12
+
13
+ parsed.should be_a(Integer)
14
+ end
15
+
16
+ it 'parses the number' do
17
+ parsed = subject.parse(raw_number)
18
+
19
+ parsed.should == number
20
+ end
21
+
22
+ context 'with big ones' do
23
+ let(:big_number) { (11 ** 20) }
24
+ let(:raw_big_number) { big_number.to_s }
25
+
26
+ it 'also returns an integer' do
27
+ parsed = subject.parse(raw_big_number)
28
+
29
+ parsed.should be_a(Integer)
30
+ end
31
+
32
+ it 'also parses the number' do
33
+ parsed = subject.parse(raw_big_number)
34
+
35
+ parsed.should == big_number
36
+ end
37
+ end
38
+ end
39
+
40
+ context 'with invalid values' do
41
+ let(:raw_invalid) { 'INVALID' }
42
+
43
+ it 'raises an error' do
44
+ expect {
45
+ subject.parse(raw_invalid)
46
+ }.to raise_error
47
+ end
48
+ end
49
+
50
+ context 'with empty values' do
51
+ it 'raises an error' do
52
+ expect {
53
+ subject.parse('')
54
+ }.to raise_error
55
+ end
56
+ end
57
+
58
+ context 'with nil values' do
59
+ it 'raises an error' do
60
+ expect {
61
+ subject.parse(nil)
62
+ }.to raise_error
63
+ end
64
+ end
65
+ end
66
+
67
+ describe '#raw' do
68
+ it 'returns a string representation' do
69
+ subject.raw(2).should eq('2')
70
+ end
71
+ end
72
+
73
+ end
@@ -0,0 +1,18 @@
1
+ require 'spec_helper'
2
+
3
+ describe Foraneus::Converters::String do
4
+
5
+ describe '#parse' do
6
+ it 'returns the string' do
7
+ parsed = subject.parse('string')
8
+
9
+ parsed.should == 'string'
10
+ end
11
+ end
12
+
13
+ describe '#raw' do
14
+ it 'returns the string' do
15
+ subject.raw('a string').should eq('a string')
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,136 @@
1
+ require 'spec_helper'
2
+
3
+ describe Foraneus do
4
+ let(:converter) { Foraneus::Converters::Integer.new }
5
+
6
+ let(:form_spec) {
7
+ c = converter
8
+ Class.new(Foraneus) do
9
+ field :delay, c
10
+ end
11
+ }
12
+
13
+ describe '.new' do
14
+ subject(:form) { form_spec.new }
15
+
16
+ its(:delay) { should be_nil }
17
+
18
+ its([:delay]) { should be_nil }
19
+
20
+ its(:data) { should be_empty }
21
+
22
+ its([]) { should be_empty }
23
+ end
24
+
25
+ describe '.parse' do
26
+ context 'with parseable data' do
27
+ subject(:form) { form_spec.parse(:delay => '5') }
28
+
29
+ its(:delay) { should eq(5) }
30
+
31
+ its([:delay]) { should eq('5') }
32
+
33
+ its(:data) { should include(:delay => 5) }
34
+
35
+ its([]) { should include(:delay => '5') }
36
+
37
+ it { should be_valid }
38
+
39
+ its([:errors]) { should be_empty }
40
+
41
+ context 'when strings as keys' do
42
+ subject(:form) { form_spec.parse('delay' => '5') }
43
+
44
+ its(['delay']) { should eq('5') }
45
+
46
+ its([:delay]) { should eq('5') }
47
+
48
+ its(:data) { should include('delay' => 5) }
49
+
50
+ its([]) { should include('delay' => '5') }
51
+ end
52
+
53
+ context 'with nil values' do
54
+ subject(:form) { form_spec.parse('delay' => nil) }
55
+
56
+ its(['delay']) { should eq(nil) }
57
+
58
+ its([:delay]) { should eq(nil) }
59
+
60
+ its(:data) { should include('delay' => nil) }
61
+
62
+ its([]) { should include('delay' => nil) }
63
+ end
64
+ end
65
+
66
+ context 'with non parseable data' do
67
+ subject(:form) { form_spec.parse(:delay => 'FIVE') }
68
+
69
+ its(:delay) { should be_nil }
70
+
71
+ its([:delay]) { should eq('FIVE') }
72
+
73
+ it { should_not be_valid }
74
+
75
+ its([:errors]) { should include(:delay) }
76
+
77
+ describe 'an error' do
78
+ subject(:error) { form[:errors].values.first }
79
+
80
+ let(:converter_exception) do
81
+ begin
82
+ converter.parse('FIVE')
83
+ rescue
84
+ e = $!
85
+ end
86
+
87
+ e
88
+ end
89
+
90
+ its(:key) { should eq(converter_exception.class.name) }
91
+
92
+ its(:message) { should eq(converter_exception.message) }
93
+ end
94
+ end
95
+ end
96
+
97
+ describe '.raw' do
98
+ subject(:form) { form_spec.raw(:delay => 5) }
99
+
100
+ its(:data) { should include(:delay => 5) }
101
+
102
+ its(:delay) { should eq(5) }
103
+
104
+ its([:delay]) { should eq('5') }
105
+
106
+ its([]) { should include(:delay => '5') }
107
+
108
+ it { should be_valid }
109
+
110
+ its([:errors]) { should be_empty }
111
+
112
+ context 'when strings as keys' do
113
+ subject(:form) { form_spec.raw('delay' => 5) }
114
+
115
+ its(:data) { should include('delay' => 5) }
116
+
117
+ its(['delay']) { should eq('5') }
118
+
119
+ its([:delay]) { should eq('5') }
120
+
121
+ its([]) { should include('delay' => '5') }
122
+ end
123
+
124
+ context 'with nil values' do
125
+ subject(:form) { form_spec.raw('delay' => nil) }
126
+
127
+ its(:data) { should include('delay' => nil) }
128
+
129
+ its(['delay']) { should eq(nil) }
130
+
131
+ its([:delay]) { should eq(nil) }
132
+
133
+ its([]) { should include('delay' => nil) }
134
+ end
135
+ end
136
+ end
@@ -0,0 +1,2 @@
1
+ require 'rspec'
2
+ require 'foraneus'
metadata CHANGED
@@ -1,80 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: foraneus
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
5
- prerelease:
4
+ version: 0.0.2
6
5
  platform: ruby
7
6
  authors:
8
7
  - Gianfranco Zas
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2013-09-21 00:00:00.000000000 Z
11
+ date: 2014-05-04 00:00:00.000000000 Z
13
12
  dependencies:
14
- - !ruby/object:Gem::Dependency
15
- name: redcarpet
16
- requirement: !ruby/object:Gem::Requirement
17
- none: false
18
- requirements:
19
- - - '='
20
- - !ruby/object:Gem::Version
21
- version: 2.1.1
22
- type: :development
23
- prerelease: false
24
- version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
- requirements:
27
- - - '='
28
- - !ruby/object:Gem::Version
29
- version: 2.1.1
30
13
  - !ruby/object:Gem::Dependency
31
14
  name: rspec
32
15
  requirement: !ruby/object:Gem::Requirement
33
- none: false
34
- requirements:
35
- - - '='
36
- - !ruby/object:Gem::Version
37
- version: 2.10.0
38
- type: :development
39
- prerelease: false
40
- version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
- requirements:
43
- - - '='
44
- - !ruby/object:Gem::Version
45
- version: 2.10.0
46
- - !ruby/object:Gem::Dependency
47
- name: simplecov
48
- requirement: !ruby/object:Gem::Requirement
49
- none: false
50
- requirements:
51
- - - '='
52
- - !ruby/object:Gem::Version
53
- version: 0.6.4
54
- type: :development
55
- prerelease: false
56
- version_requirements: !ruby/object:Gem::Requirement
57
- none: false
58
- requirements:
59
- - - '='
60
- - !ruby/object:Gem::Version
61
- version: 0.6.4
62
- - !ruby/object:Gem::Dependency
63
- name: yard
64
- requirement: !ruby/object:Gem::Requirement
65
- none: false
66
16
  requirements:
67
- - - '='
17
+ - - ! '>='
68
18
  - !ruby/object:Gem::Version
69
- version: 0.8.2.1
19
+ version: '0'
70
20
  type: :development
71
21
  prerelease: false
72
22
  version_requirements: !ruby/object:Gem::Requirement
73
- none: false
74
23
  requirements:
75
- - - '='
24
+ - - ! '>='
76
25
  - !ruby/object:Gem::Version
77
- version: 0.8.2.1
26
+ version: '0'
78
27
  description: Provides validation and transformation mechanisms to external data.
79
28
  email: snmgian@gmail.com
80
29
  executables: []
@@ -82,40 +31,55 @@ extensions: []
82
31
  extra_rdoc_files: []
83
32
  files:
84
33
  - lib/foraneus.rb
85
- - lib/foraneus/simple_converters.rb
86
- - lib/foraneus/value_set.rb
87
- - lib/foraneus/hashlike.rb
88
34
  - lib/foraneus/errors.rb
89
- - lib/foraneus/raw_value_set_builder.rb
90
- - lib/foraneus/value_set_builder.rb
91
- - lib/foraneus/markers.rb
92
- - lib/foraneus/converters.rb
35
+ - lib/foraneus/converters/boolean.rb
36
+ - lib/foraneus/converters/string.rb
37
+ - lib/foraneus/converters/float.rb
38
+ - lib/foraneus/converters/decimal.rb
39
+ - lib/foraneus/converters/date.rb
40
+ - lib/foraneus/converters/integer.rb
93
41
  - COPYING
94
42
  - COPYING.LESSER
95
43
  - README.md
96
- homepage: http://rubygems.org/gems/rake
97
- licenses: []
44
+ - spec/lib/foraneus_spec.rb
45
+ - spec/lib/foraneus/converters/decimal_converter_spec.rb
46
+ - spec/lib/foraneus/converters/date_converter_spec.rb
47
+ - spec/lib/foraneus/converters/integer_converter_spec.rb
48
+ - spec/lib/foraneus/converters/string_converter_spec.rb
49
+ - spec/lib/foraneus/converters/float_converter_spec.rb
50
+ - spec/lib/foraneus/converters/boolean_converter_spec.rb
51
+ - spec/spec_helper.rb
52
+ homepage: https://github.com/snmgian/foraneus
53
+ licenses:
54
+ - LGPL
55
+ metadata: {}
98
56
  post_install_message:
99
57
  rdoc_options: []
100
58
  require_paths:
101
59
  - lib
102
60
  required_ruby_version: !ruby/object:Gem::Requirement
103
- none: false
104
61
  requirements:
105
62
  - - ! '>='
106
63
  - !ruby/object:Gem::Version
107
64
  version: '0'
108
65
  required_rubygems_version: !ruby/object:Gem::Requirement
109
- none: false
110
66
  requirements:
111
67
  - - ! '>='
112
68
  - !ruby/object:Gem::Version
113
69
  version: '0'
114
70
  requirements: []
115
71
  rubyforge_project:
116
- rubygems_version: 1.8.23
72
+ rubygems_version: 2.1.5
117
73
  signing_key:
118
- specification_version: 3
74
+ specification_version: 4
119
75
  summary: Validates and transform external data.
120
- test_files: []
76
+ test_files:
77
+ - spec/lib/foraneus_spec.rb
78
+ - spec/lib/foraneus/converters/decimal_converter_spec.rb
79
+ - spec/lib/foraneus/converters/date_converter_spec.rb
80
+ - spec/lib/foraneus/converters/integer_converter_spec.rb
81
+ - spec/lib/foraneus/converters/string_converter_spec.rb
82
+ - spec/lib/foraneus/converters/float_converter_spec.rb
83
+ - spec/lib/foraneus/converters/boolean_converter_spec.rb
84
+ - spec/spec_helper.rb
121
85
  has_rdoc:
@@ -1,50 +0,0 @@
1
- require 'delegate'
2
-
3
- module Foraneus
4
-
5
- # A converter is intended for parsing a string and returning a value.
6
- module Converters
7
-
8
- # A decorator for concrete converters. It prevents nil values and manages errors raised by {AbstractConverter#parse}
9
- class ConverterDecorator < SimpleDelegator
10
-
11
- # @param [AbstractConverter] converter The converter to be decorated
12
- def initialize(converter)
13
- super(converter)
14
- @source = converter
15
- end
16
-
17
- # Invokes {AbstractConverter#parse}. Manages errors raised by the converter.
18
- # Also, it returns nil if value.nil?
19
- # @param [String] value Value to be parsed
20
- # @return [Object] Parsed value
21
- # @raise [Foraneus::ConverterError] if the concrete converter raises an error
22
- def parse(value)
23
- return nil if value.nil?
24
-
25
- begin
26
- @source.parse(value)
27
- rescue
28
- raise Foraneus::ConverterError.new(value, @source.name)
29
- end
30
- end
31
- end
32
-
33
- # @abstract Converters should inherit from this class and override #{code_name} and #{parse}
34
- class AbstractConverter
35
-
36
- # Returns the of this converter
37
- # @return [String]
38
- def name
39
- raise NotImplementedError
40
- end
41
-
42
- # Parses a value and returns the obtained parsed value
43
- # @param [String] value Value to be parsed
44
- # @return [Object]
45
- def parse(value)
46
- raise NotImplementedError
47
- end
48
- end
49
- end
50
- end
@@ -1,36 +0,0 @@
1
- module Foraneus
2
-
3
- # Adds hash read-only capabilities.
4
- module HashlikeValueSet
5
-
6
- # Returns the value associated with a key
7
- #
8
- # Possible keys are:
9
- # - valid?
10
- # - errors
11
- # - raw_values
12
- # - as_hash
13
- #
14
- # @param [Symbol] key The key for the value to retrieve
15
- # @return [Object, nil] The value associated with key, or nil if the key is unknown
16
- def [](key)
17
- ivar = case key
18
- when :valid?
19
- :@valid
20
- when :errors
21
- :@errors
22
- when :raw_values
23
- :@raw_values
24
- when :as_hash
25
- :@hash_values
26
- else
27
- nil
28
- end
29
-
30
- if ivar
31
- self.instance_variable_get(ivar)
32
- end
33
- end
34
- end
35
-
36
- end
@@ -1,9 +0,0 @@
1
- module Foraneus
2
-
3
- # Marks a value_set as invalid.
4
- module InvalidValueSet; end
5
-
6
- # Marks a value_set whose getter methods return raw values.
7
- module RawValueSet; end
8
-
9
- end
@@ -1,43 +0,0 @@
1
- module Foraneus
2
-
3
- # Module for building raw value_sets
4
- module RawValueSetBuilder
5
-
6
- # Builds a RawValueSet.
7
- # @param [Class] form_class A ValueSet class
8
- # @param [Hash] meta Meta information about fields and converters
9
- # @param [ValueSet, Hash] form_or_params ValueSet or Hash that will be used to build a RawValueSet
10
- # @return [RawValueSet]
11
- def self.build(form_class, meta, form_or_params)
12
- if form_or_params.is_a?(Foraneus::ValueSet)
13
- self.raw_form(form_or_params)
14
- elsif form_or_params.is_a?(Hash)
15
- self.raw_params(form_class, meta, form_or_params)
16
- end
17
- end
18
-
19
- # Builds a RawValueSet from a ValueSet object
20
- # @api private
21
- # @param [ValueSet] value_set
22
- # @return [RawValueSet]
23
- def self.raw_form(value_set)
24
- raw_vs_class = Class.new(value_set.class) do
25
- include Foraneus::RawValueSet
26
- end
27
-
28
- raw_vs = raw_vs_class.new
29
- value_set[:raw_values].each do |name, value|
30
- raw_vs.instance_variable_set("@#{name}", value)
31
- end
32
- raw_vs
33
- end
34
-
35
- # Builds a value_set and returns a RawValueSet
36
- # @api private
37
- def self.raw_params(form_class, meta, params)
38
- form = Foraneus::ValueSetBuilder.build(form_class, meta, params)
39
- raw_form(form)
40
- end
41
-
42
- end
43
- end
@@ -1,79 +0,0 @@
1
- require 'delegate'
2
-
3
- module Foraneus
4
- module Converters
5
-
6
- # Boolean converter.
7
- class Boolean < AbstractConverter
8
-
9
- # @see {AbstractConverter#see}
10
- def name
11
- :boolean
12
- end
13
-
14
- # @see AbstractConverter#parse
15
- # @param [String] value The value to be parsed
16
- # @return [Boolean] Returns true only if value == 'true'
17
- def parse(value)
18
- if value == true
19
- true
20
- elsif value == 'true'
21
- true
22
- else
23
- false
24
- end
25
- end
26
- end
27
-
28
- # Float converter.
29
- class Float < AbstractConverter
30
-
31
- # @see {AbstractConverter#see}
32
- def name
33
- :float
34
- end
35
-
36
- # @see AbstractConverter#parse
37
- # @return [Float] Returns a float number
38
- def parse(value)
39
- Kernel.Float(value)
40
- end
41
- end
42
-
43
- # Integer converter.
44
- class Integer < AbstractConverter
45
-
46
- # @see {AbstractConverter#see}
47
- def name
48
- :integer
49
- end
50
-
51
- # @see AbstractConverter#parse
52
- # @return [Integer] Returns an integer number
53
- def parse(value)
54
- Kernel.Integer(value)
55
- end
56
- end
57
-
58
- # String converter.
59
- class String < AbstractConverter
60
-
61
- # @see {AbstractConverter#see}
62
- def name
63
- :string
64
- end
65
-
66
- # @see AbstractConverter#parse
67
- # @return [String] Returns a String reprensentation of the given value.
68
- def parse(value)
69
- if value.is_a?(::String)
70
- value
71
- else
72
- value.to_s
73
- end
74
- end
75
- end
76
-
77
- end
78
- end
79
-