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

Returns:

  • (Regexp)

See Also:

/^
  (?<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'

Returns:

  • (Regexp)
/^(?<minimum>[^\.]+)(?<inclusiveness>\.\.\.?)(?<maximum>[^\.]+)$/

Instance Method Summary collapse

Methods included from Utility

#match_to_hash, #numerify

Instance Method Details

#alter_by_postgres_style!(options) ⇒ void (private)

This method returns an undefined value.

Modify the options slightly for postgres formats.

Parameters:

  • options (OpenStruct)


91
92
93
94
95
96
97
98
99
# File 'lib/unbounded/formats.rb', line 91

def alter_by_postgres_style!(options)
  options.minimum     = numerify(options.minimum, NINFINITY)
  options.maximum     = numerify(options.maximum, INFINITY)
  options.exclude_end = options.upper_inc == ')'

  if options.lower_inc == '('
    options.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

Parameters:

  • options (OpenStruct)


104
105
106
107
108
# File 'lib/unbounded/formats.rb', line 104

def alter_by_simple_style!(options)
  options.minimum     = numerify(options.minimum, NINFINITY)
  options.maximum     = numerify(options.maximum, INFINITY)
  options.exclude_end = options.inclusiveness.length == 3
end

#parse_for_range(args) ⇒ OpenStruct

Parameters:

Returns:

  • (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 parse_standard_range_options(nil, nil)
  when 1    then parse_string_for_range_options(args.shift)
  when 2..3 then parse_standard_range_options(*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

Parameters:

  • min (Object)
  • max (Object)
  • exclude_end (Boolean) (defaults to: false)

Returns:

  • ({format: :standard, minimum: Numeric, maximum: Numeric, exclude_end: Boolean})

See Also:



67
68
69
70
71
72
73
74
# File 'lib/unbounded/formats.rb', line 67

def parse_standard_range_options(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.

Parameters:

  • s (#to_s)

Returns:

  • (Hash)


79
80
81
82
83
84
85
86
# File 'lib/unbounded/formats.rb', line 79

def parse_string_for_range_options(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