foraneus 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +15 -0
- data/README.md +64 -4
- data/lib/foraneus/converters/boolean.rb +23 -0
- data/lib/foraneus/converters/date.rb +24 -0
- data/lib/foraneus/converters/decimal.rb +49 -0
- data/lib/foraneus/converters/float.rb +15 -0
- data/lib/foraneus/converters/integer.rb +15 -0
- data/lib/foraneus/converters/string.rb +15 -0
- data/lib/foraneus/errors.rb +4 -55
- data/lib/foraneus.rb +117 -42
- data/spec/lib/foraneus/converters/boolean_converter_spec.rb +36 -0
- data/spec/lib/foraneus/converters/date_converter_spec.rb +59 -0
- data/spec/lib/foraneus/converters/decimal_converter_spec.rb +81 -0
- data/spec/lib/foraneus/converters/float_converter_spec.rb +72 -0
- data/spec/lib/foraneus/converters/integer_converter_spec.rb +73 -0
- data/spec/lib/foraneus/converters/string_converter_spec.rb +18 -0
- data/spec/lib/foraneus_spec.rb +136 -0
- data/spec/spec_helper.rb +2 -0
- metadata +35 -71
- data/lib/foraneus/converters.rb +0 -50
- data/lib/foraneus/hashlike.rb +0 -36
- data/lib/foraneus/markers.rb +0 -9
- data/lib/foraneus/raw_value_set_builder.rb +0 -43
- data/lib/foraneus/simple_converters.rb +0 -79
- data/lib/foraneus/value_set.rb +0 -46
- data/lib/foraneus/value_set_builder.rb +0 -132
data/lib/foraneus/value_set.rb
DELETED
@@ -1,46 +0,0 @@
|
|
1
|
-
module Foraneus
|
2
|
-
|
3
|
-
# Base class from which all the value_sets should inherit.
|
4
|
-
#
|
5
|
-
# Concrete classes model a set of params/values that are parsed from an external source (like HTTP query params)
|
6
|
-
class ValueSet
|
7
|
-
|
8
|
-
# Builds an instance of ValueSet that conforms with its fields specification.
|
9
|
-
#
|
10
|
-
# If any of the params can be parsed by its corresponding convertor then an
|
11
|
-
# {InvalidValueSet} and {RawValueSet} is returned that holds raw_values
|
12
|
-
#
|
13
|
-
# @param [Hash<Symbol, String>] params Hash that holds values that will be parsed and associated
|
14
|
-
# with the created ValueSet instance.
|
15
|
-
# @return [ValueSet, InvalidValueSet, RawValueSet] A value_set
|
16
|
-
def self.build(params = {})
|
17
|
-
Foraneus::ValueSetBuilder.build(self, @meta, params)
|
18
|
-
end
|
19
|
-
|
20
|
-
# Builds an instance of ValueSet and raises an {ValueSetError} if invalid params.
|
21
|
-
# @see .build
|
22
|
-
# @param (see .build)
|
23
|
-
def self.build!(params = {})
|
24
|
-
value_set = self.build(params)
|
25
|
-
if value_set.kind_of?(Foraneus::InvalidValueSet)
|
26
|
-
raise Foraneus::ValueSetError.new(value_set)
|
27
|
-
end
|
28
|
-
|
29
|
-
value_set
|
30
|
-
end
|
31
|
-
|
32
|
-
# Returns a {RawValueSet} that corresponds to the given argument.
|
33
|
-
#
|
34
|
-
# @overload raw(value_set)
|
35
|
-
# @param [ValueSet] value_set ValueSet whose raw value will be used to create a {RawValueSet}
|
36
|
-
#
|
37
|
-
# @overload raw(params)
|
38
|
-
# @param [Hash] params Params for creating a {RawValueSet}
|
39
|
-
#
|
40
|
-
# @return [RawValueSet]
|
41
|
-
def self.raw(vs_or_params)
|
42
|
-
Foraneus::RawValueSetBuilder.build(self, @meta, vs_or_params)
|
43
|
-
end
|
44
|
-
|
45
|
-
end
|
46
|
-
end
|
@@ -1,132 +0,0 @@
|
|
1
|
-
module Foraneus
|
2
|
-
|
3
|
-
# Module for building value_sets
|
4
|
-
module ValueSetBuilder
|
5
|
-
|
6
|
-
# Builds an instance of a value_set
|
7
|
-
#
|
8
|
-
# @param [Class<? extends ValueSet>] vs_class ValueSet subclass from with the value_set will be instantiated
|
9
|
-
#
|
10
|
-
# @param [Hash<Symbol, Symbol>] meta Hash with metainformation. Each key is a field name, and each value is the name of the converter to be used
|
11
|
-
#
|
12
|
-
# @param [Hash<Symbol, Object>] params Parameters that will be parsed and set as attributes for the newly value_set
|
13
|
-
#
|
14
|
-
# @return [ValueSet] An instance of (or an instance of subclass of) vs_class
|
15
|
-
def self.build(vs_class, meta, params = {})
|
16
|
-
|
17
|
-
parsed_params, raw_params, errors = self.parse_params(meta, params)
|
18
|
-
|
19
|
-
if errors.empty?
|
20
|
-
form = vs_class.new
|
21
|
-
set_instance_vars(form, parsed_params)
|
22
|
-
form.instance_variable_set(:@hash_values, parsed_params)
|
23
|
-
else
|
24
|
-
form = create_invalid_value_set(vs_class)
|
25
|
-
set_instance_vars(form, raw_params)
|
26
|
-
end
|
27
|
-
|
28
|
-
form.instance_variable_set(:@valid, errors.empty?)
|
29
|
-
form.instance_variable_set(:@errors, errors)
|
30
|
-
form.instance_variable_set(:@raw_values, raw_params)
|
31
|
-
form
|
32
|
-
end
|
33
|
-
|
34
|
-
# Creates a value_set marked as invalid.
|
35
|
-
#
|
36
|
-
# It creates a value set by creating an instance from a subclass of vs_class.
|
37
|
-
# That subclass is mixed in with {InvalidValueSet} and {RawValueSet}
|
38
|
-
#
|
39
|
-
# @api private
|
40
|
-
#
|
41
|
-
# @param [Class<? extends ValueSet>] vs_class Subclass of ValueSet from which the invalid value_set
|
42
|
-
# will be instantiated
|
43
|
-
#
|
44
|
-
# @return [ValueSet] A kind of ValueSet, mixed with {InvalidValueSet} and {RawValueSet}
|
45
|
-
def self.create_invalid_value_set(vs_class)
|
46
|
-
form_class = Class.new(vs_class) do
|
47
|
-
include Foraneus::InvalidValueSet
|
48
|
-
include Foraneus::RawValueSet
|
49
|
-
end
|
50
|
-
form = form_class.new
|
51
|
-
end
|
52
|
-
|
53
|
-
# Parses a given value.
|
54
|
-
#
|
55
|
-
# The converter is selected by querying the given metadata, searching for the given name.
|
56
|
-
# @api private
|
57
|
-
#
|
58
|
-
# @param [Hash<Symbol, Symbol>] meta (see .build)
|
59
|
-
# @param [Symbol] name Name of the field to be parsed
|
60
|
-
# @param [String] value Value to be parsed
|
61
|
-
#
|
62
|
-
# @return [Array] An array of two elements. The first of them is the result of the parsing,
|
63
|
-
# and the last one is a boolean value that indicates if an error occured during the parsing.
|
64
|
-
def self.parse(meta, name, value)
|
65
|
-
parsed_value = nil
|
66
|
-
error = false
|
67
|
-
|
68
|
-
parser_code = meta[name]
|
69
|
-
parser = Foraneus.registry[parser_code]
|
70
|
-
|
71
|
-
begin
|
72
|
-
parsed_value = parser.parse(value)
|
73
|
-
rescue StandardError => e
|
74
|
-
error = true
|
75
|
-
end
|
76
|
-
|
77
|
-
[parsed_value, error]
|
78
|
-
end
|
79
|
-
|
80
|
-
# Parses each of the given params.
|
81
|
-
#
|
82
|
-
# The converter is select by searching for the param name in the metadata.
|
83
|
-
# If a param is not expected by the metadata, then it is simply ignored.
|
84
|
-
#
|
85
|
-
# @api private
|
86
|
-
#
|
87
|
-
# @param [Hash<Symbol, Symbol>] meta (see .build)
|
88
|
-
# @param [Hash<Symbol, Object>] params (see .build)
|
89
|
-
#
|
90
|
-
# @return [Array] An array consisting of three elements:
|
91
|
-
# - Hash { Symbol => Object } Parsed params/values
|
92
|
-
# - Hash { Symbol => Object } Given params/values, it only contains params that are present in the metadata.
|
93
|
-
# - Hash { Symbol => {ValueError} } Errors ocurred during parsing, each key is the name of a field with an associated error
|
94
|
-
def self.parse_params(meta, params)
|
95
|
-
parsed_params = {}
|
96
|
-
raw_params = {}
|
97
|
-
errors = {}
|
98
|
-
|
99
|
-
params.each do |name, value|
|
100
|
-
normalized_name = name.to_sym
|
101
|
-
next unless meta.include?(normalized_name)
|
102
|
-
|
103
|
-
raw_params[name] = value
|
104
|
-
parsed_value, error = self.parse(meta, normalized_name, value)
|
105
|
-
unless error
|
106
|
-
parsed_params[normalized_name] = parsed_value
|
107
|
-
else
|
108
|
-
errors[normalized_name] = Foraneus::ValueError.new(normalized_name, value, meta[normalized_name])
|
109
|
-
end
|
110
|
-
end
|
111
|
-
|
112
|
-
[parsed_params, raw_params, errors]
|
113
|
-
end
|
114
|
-
|
115
|
-
# Sets instance variables to an object.
|
116
|
-
#
|
117
|
-
# @api private
|
118
|
-
#
|
119
|
-
# @param [Object] o The object to set the instance variables
|
120
|
-
# @param [Hash<Symbol, Object>] params A hash with the names and values of instance variables that will be set.
|
121
|
-
#
|
122
|
-
# @return [Object] The received object with the instance variables set
|
123
|
-
def self.set_instance_vars(o, params)
|
124
|
-
params.each do |name, value|
|
125
|
-
o.instance_variable_set("@#{name}", value)
|
126
|
-
end
|
127
|
-
|
128
|
-
o
|
129
|
-
end
|
130
|
-
|
131
|
-
end
|
132
|
-
end
|