Module: Crabfarm::Assertion::Parsers
- Included in:
- Wrapper
- Defined in:
- lib/crabfarm/assertion/parsers.rb
Instance Method Summary collapse
- #clean_string(_string, _options) ⇒ Object
- #extract_number_from_string(_value, _options) ⇒ Object
- #fail_with_nil ⇒ Object
- #infer_thousand_separator(_decimal_mark) ⇒ Object
- #parse_boolean(_value, _options = {}) ⇒ Object
- #parse_by_map(_value, _map = {}) ⇒ Object
- #parse_float(_value, _options = {}) ⇒ Object
- #parse_integer(_value, _options = {}) ⇒ Object
- #parse_number(_to_fun, _value, _options = {}) ⇒ Object
- #parse_phrase(_value, _options = {}) ⇒ Object
- #return_default(_options) ⇒ Object
Instance Method Details
#clean_string(_string, _options) ⇒ Object
69 70 71 72 73 74 |
# File 'lib/crabfarm/assertion/parsers.rb', line 69 def clean_string _string, normalized = _string.strip.gsub(/\s+/, ' ') if normalized.empty? return_default else normalized end end |
#extract_number_from_string(_value, _options) ⇒ Object
85 86 87 88 89 90 91 92 93 |
# File 'lib/crabfarm/assertion/parsers.rb', line 85 def extract_number_from_string _value, decimal_mark = .fetch(:decimal_mark, '.') # TODO: make default decimal mark configurable thousand_mark = .fetch(:thousand_mark, infer_thousand_separator(decimal_mark)) num_rgx = Regexp.new "-?(?:\\#{decimal_mark}\\d+|\\d{1,3}(?:\\#{thousand_mark}\\d{3})+(?:\\#{decimal_mark}\\d+)?|\\d+(?:\\#{decimal_mark}\\d+)?)" matches = _value.scan num_rgx fail_with "'#{_value}' has an ambiguous numeric format" if matches.count > 1 fail_with "'#{_value}' does not contain any number" if matches.count < 1 matches.first.gsub(thousand_mark,'').gsub(decimal_mark, '.') end |
#fail_with_nil ⇒ Object
76 77 78 |
# File 'lib/crabfarm/assertion/parsers.rb', line 76 def fail_with_nil fail_with "'nil' cant be parsed" end |
#infer_thousand_separator(_decimal_mark) ⇒ Object
95 96 97 98 99 100 |
# File 'lib/crabfarm/assertion/parsers.rb', line 95 def infer_thousand_separator _decimal_mark case _decimal_mark when '.' then ',' when ',' then '.' else '' end end |
#parse_boolean(_value, _options = {}) ⇒ Object
27 28 29 30 31 32 33 34 35 36 |
# File 'lib/crabfarm/assertion/parsers.rb', line 27 def parse_boolean _value, ={} truthy = Array(.fetch(:truthy, ['true'])) falsy = Array(.fetch(:falsy, ['false'])) mapping = { :empty => false, true => true, false => false } truthy.each { |k| mapping[k] = true } falsy.each { |k| mapping[k] = false } parse_by_map _value, mapping end |
#parse_by_map(_value, _map = {}) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/crabfarm/assertion/parsers.rb', line 38 def parse_by_map _value, _map={} fail_with_nil if _value.nil? case _value when String, Symbol _value = _value.strip if _value.is_a? String return parse_by_map :empty, _map if _value.empty? fail_with "'#{_value.to_s}' is and invalid keyword" unless _map.key? _value _map[_value] else fail_with "'#{_value}' cannot be mapped" end end |
#parse_float(_value, _options = {}) ⇒ Object
9 10 11 |
# File 'lib/crabfarm/assertion/parsers.rb', line 9 def parse_float _value, ={} parse_number :to_f, _value, end |
#parse_integer(_value, _options = {}) ⇒ Object
5 6 7 |
# File 'lib/crabfarm/assertion/parsers.rb', line 5 def parse_integer _value, ={} parse_number :to_i, _value, end |
#parse_number(_to_fun, _value, _options = {}) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/crabfarm/assertion/parsers.rb', line 52 def parse_number _to_fun, _value, ={} fail_with_nil if _value.nil? if _value.is_a? String _value.strip! if _value.empty? return_default else extract_number_from_string(_value, ).send _to_fun end elsif _value.respond_to? _to_fun _value.send _to_fun else fail_with "'#{_value}' cannot be transformed to number" end end |
#parse_phrase(_value, _options = {}) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/crabfarm/assertion/parsers.rb', line 13 def parse_phrase _value, ={} fail_with_nil if _value.nil? if _value.is_a? String clean_string _value, else if _value.respond_to? :to_s clean_string _value.to_s, else fail_with "Value cannot be transformed to string" end end end |
#return_default(_options) ⇒ Object
80 81 82 83 |
# File 'lib/crabfarm/assertion/parsers.rb', line 80 def return_default fail_with "Value is an empty" unless .key? :default [:default] end |