Module: Unbounded::Formats
- Extended by:
- ActiveSupport::Concern
- Includes:
- Utility
- Included in:
- Range
- Defined in:
- lib/unbounded/formats.rb
Overview
Methods for parsing alternative range formats Presently there are two:
Constant Summary collapse
- POSTGRES_FORMAT =
A regular expression that loosely describes the format used within Postgres for their range types. They look something like:
"[4,6]"
==4..6
"[1,9)"
==1...9
An endpoint can be omitted to create an unbounded type:
"[1,]"
==1..INFINITY
"[,0)"
==-INFINITY...0
/^ (?<lower_inc>[\[\(]) (?<minimum>\S*) ,\s* (?<maximum>\S*) (?<upper_inc>[\)\]]) $/x
- SIMPLE_FORMAT =
A regular expression for parsing the simple format used in Range.new shorthand, e.g.
'1..3'
,'4...10'
/^(?<minimum>[^\.]+)(?<inclusiveness>\.\.\.?)(?<maximum>[^\.]+)$/
Instance Method Summary collapse
-
#alter_by_postgres_style!(options) ⇒ void
private
Modify the options slightly for postgres formats.
-
#alter_by_simple_style!(options) ⇒ void
private
Modify the options slightly to account for simple (e.g. 1..3) formatting.
- #parse_for_range(args) ⇒ OpenStruct
-
#parse_standard_range_options(min, max, exclude_end = false) ⇒ {format: :standard, minimum: Numeric, maximum: Numeric, exclude_end: Boolean}
private
Parse the standard range initialization options.
-
#parse_string_for_range_options(s) ⇒ Hash
private
Parse string using POSTGRES_FORMAT or SIMPLE_FORMAT.
Methods included from Utility
Instance Method Details
#alter_by_postgres_style!(options) ⇒ void (private)
This method returns an undefined value.
Modify the options slightly for postgres formats.
91 92 93 94 95 96 97 98 99 |
# File 'lib/unbounded/formats.rb', line 91 def alter_by_postgres_style!() .minimum = numerify(.minimum, NINFINITY) .maximum = numerify(.maximum, INFINITY) .exclude_end = .upper_inc == ')' if .lower_inc == '(' .minimum += 1 end end |
#alter_by_simple_style!(options) ⇒ void (private)
This method returns an undefined value.
Modify the options slightly to account for simple (e.g. 1..3) formatting
104 105 106 107 108 |
# File 'lib/unbounded/formats.rb', line 104 def alter_by_simple_style!() .minimum = numerify(.minimum, NINFINITY) .maximum = numerify(.maximum, INFINITY) .exclude_end = .inclusiveness.length == 3 end |
#parse_for_range(args) ⇒ OpenStruct
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/unbounded/formats.rb', line 43 def parse_for_range(args) hashed = case args.length when 0 then (nil, nil) when 1 then (args.shift) when 2..3 then (*args) end.presence or raise ArgumentError, "Don't know how to parse arguments: #{args.inspect}" OpenStruct.new(hashed).tap do |o| if o.format == :postgres alter_by_postgres_style! o elsif o.format == :simple alter_by_simple_style! o end end end |
#parse_standard_range_options(min, max, exclude_end = false) ⇒ {format: :standard, minimum: Numeric, maximum: Numeric, exclude_end: Boolean} (private)
Parse the standard range initialization options
67 68 69 70 71 72 73 74 |
# File 'lib/unbounded/formats.rb', line 67 def (min, max, exclude_end = false) { :format => :standard, :minimum => min.presence || NINFINITY, :maximum => max.presence || INFINITY, :exclude_end => !!exclude_end } end |
#parse_string_for_range_options(s) ⇒ Hash (private)
Parse string using POSTGRES_FORMAT or SIMPLE_FORMAT.
79 80 81 82 83 84 85 86 |
# File 'lib/unbounded/formats.rb', line 79 def (s) case s.to_s when POSTGRES_FORMAT match_to_hash(Regexp.last_match).merge(:format => :postgres) when SIMPLE_FORMAT match_to_hash(Regexp.last_match).merge(:format => :simple) end end |