Module: NRSER::Types

Defined in:
lib/nrser/types/combinators.rb,
lib/nrser/types.rb,
lib/nrser/types/is.rb,
lib/nrser/types/any.rb,
lib/nrser/types/hash.rb,
lib/nrser/types/is_a.rb,
lib/nrser/types/type.rb,
lib/nrser/types/array.rb,
lib/nrser/types/attrs.rb,
lib/nrser/types/maybe.rb,
lib/nrser/types/where.rb,
lib/nrser/types/bounded.rb,
lib/nrser/types/numbers.rb,
lib/nrser/types/strings.rb,
lib/nrser/types/booleans.rb

Overview

base class for Union and Intersection which combine over a set of types.

Defined Under Namespace

Classes: Array, Attrs, Bounded, Combinator, Hash, Intersection, Is, IsA, Type, Union, Where

Constant Summary collapse

ANY =
where(name: 'Any', from_s: ->(s) { s }) { true }
ZERO =

zero

is 0, name: 'zero', from_s: method(:parse_number)
NUM =

number (Numeric)

IsA.new Numeric, name: 'Num', from_s: method(:parse_number)
INT =

integers

IsA.new Integer, name: 'Int', from_s: method(:parse_number)
POS_INT =

positive integer


integer greater than zero.

intersection INT, bounded(min: 1), name: 'PosInt'
NEG_INT =

negative integer


integer less than zero

intersection INT, bounded(max: -1), name: 'NegInt'
NON_NEG_INT =

non-negative integer


positive integers and zero… but it seems more efficient to define these as bounded instead of a union.

intersection INT, bounded(min: 0), name: 'NonNegInt'
NON_POS_INT =

non-positive integer


negative integers and zero.

intersection INT, bounded(max: 0), name: 'NonPosInt'
STR =
IsA.new String, name: 'Str', from_s: ->(s) { s }
EMPTY_STR =
Is.new ''
NON_EMPTY_STR =
str length: {min: 1}, name: "NonEmptyStr"
TRUE =

booleans

is true, name: 'true', from_s: ->(string) {
  if ['true', 't', '1', 'yes', 'y', 'on'].include? string.downcase
    true
  else
    raise TypeError, "can not convert to true: #{ string.inspect }"
  end
}
FALSE =
is false, name: 'false', from_s: ->(string) {
  if ['false', 'f', '0', 'no', 'n', 'off'].include? string.downcase
    false
  else
    raise TypeError, "can not convert to true: #{ string.inspect }"
  end
}
BOOL =
union TRUE, FALSE

Class Method Summary collapse

Class Method Details

.all_of(*types, **options) ⇒ Object

match all of the types



83
84
85
# File 'lib/nrser/types/combinators.rb', line 83

def self.all_of *types, **options
  intersection *types, **options
end

.anyObject

anything



10
11
12
# File 'lib/nrser/types/any.rb', line 10

def self.any
  ANY
end

.array(*args) ⇒ Object

array



54
55
56
# File 'lib/nrser/types/array.rb', line 54

def self.array *args
  Array.new *args
end

.attrs(attrs, options = {}) ⇒ Object

Attrs



29
30
31
# File 'lib/nrser/types/attrs.rb', line 29

def self.attrs attrs, options = {}
  Attrs.new attrs, **options
end

.boolObject

true or false



39
40
41
# File 'lib/nrser/types/booleans.rb', line 39

def self.bool
  BOOL
end

.booleanObject



43
44
45
# File 'lib/nrser/types/booleans.rb', line 43

def self.boolean
  bool
end

.bounded(**options) ⇒ Object

Bounded



32
33
34
# File 'lib/nrser/types/bounded.rb', line 32

def self.bounded **options
  Bounded.new **options
end

.check(value, type) ⇒ Object

raise an error if value doesn’t match type.



27
28
29
# File 'lib/nrser/types.rb', line 27

def self.check value, type
  make(type).check value
end

.falseObject



32
33
34
# File 'lib/nrser/types/booleans.rb', line 32

def self.false
  FALSE
end

.from_repr(repr) ⇒ Object

make a type instance from a object representation that can come from a YAML or JSON declaration.



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/nrser/types.rb', line 56

def self.from_repr repr
  match repr, {
    str => ->(string) {
      NRSER::Types.method(string.downcase).call
    },
    
    Hash => ->(hash) {
      
    },
  }
end

.intObject



39
40
41
# File 'lib/nrser/types/numbers.rb', line 39

def self.int
  INT
end

.integerObject



43
44
45
# File 'lib/nrser/types/numbers.rb', line 43

def self.integer
  int
end

.intersection(*types, **options) ⇒ Object

match all of the types



78
79
80
# File 'lib/nrser/types/combinators.rb', line 78

def self.intersection *types, **options
  Intersection.new *types, **options
end

.is(value, **options) ⇒ Object

an exact value (using ===)



29
30
31
# File 'lib/nrser/types/is.rb', line 29

def self.is value, **options
  Is.new value, **options
end

.is_a(klass) ⇒ Object

class membership



24
25
26
# File 'lib/nrser/types/is_a.rb', line 24

def self.is_a klass
  IsA.new klass
end

.length(*args) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/nrser/types/attrs.rb', line 33

def self.length *args
  bounds = {}
  options = {}
  
  case args.length
  when 1
    case args[0]
    when ::Integer
      bounds[:min] = bounds[:max] = non_neg_int.check(args[0])
      
    when ::Hash
      options = args[0].reject {|k, v|
        if k == :min || k == :max
          bounds[k] = non_neg_int.check(v)
        end
      }
      
    else        
      raise ArgumentError, "        arg must be positive integer or option hash, found:\n        \#{ args[0].inspect } of type \#{ args[0].class }\n      END\n    end\n    \n  when 2\n    bounds[:min] = bounds[:max] = non_neg_int.check(args[0])\n    options = args[1]\n    \n  else\n    raise ArgumentError, <<-END.squish\n      must provided 1 or 2 args.\n    END\n  end\n  \n  attrs({length: intersection(non_neg_int, bounded(bounds))}, options)\nend\n".squish

.listObject



58
59
60
# File 'lib/nrser/types/array.rb', line 58

def self.list
  array
end

.make(value) ⇒ Object

make a type.



16
17
18
19
20
21
22
23
24
# File 'lib/nrser/types.rb', line 16

def self.make value
  if value.is_a? NRSER::Types::Type
    value
  elsif value.is_a? ::Class
    IsA.new value
  else
    Is.new value
  end
end

.match(value, type_map) ⇒ Object

Raises:

  • (TypeError)


35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/nrser/types.rb', line 35

def self.match value, type_map
  type_map.each {|type, block|
    if test value, type
      return block.call value
    end
  }
  
  raise TypeError, "    could not match value\n    \n      \#{ value.inspect }\n    \n    to any of types\n    \n        \#{ type_map.keys.map {|type| \"\\n    \#{ type.inspect }\"} }\n    \n  END\nend\n".dedent

.maybe(type) ⇒ Object

nil or the argument type



8
9
10
# File 'lib/nrser/types/maybe.rb', line 8

def self.maybe type
  union nil, type, name: "Maybe(#{ type.name })"
end

.neg_intObject



71
72
73
# File 'lib/nrser/types/numbers.rb', line 71

def self.neg_int
  NEG_INT
end

.non_neg_intObject



84
85
86
# File 'lib/nrser/types/numbers.rb', line 84

def self.non_neg_int
  NON_NEG_INT
end

.non_pos_intObject



96
97
98
# File 'lib/nrser/types/numbers.rb', line 96

def self.non_pos_int
  NON_POS_INT
end

.numObject



30
31
32
# File 'lib/nrser/types/numbers.rb', line 30

def self.num
  NUM
end

.one_of(*types, **options) ⇒ Object

match any of the types



67
68
69
# File 'lib/nrser/types/combinators.rb', line 67

def self.one_of *types, **options
  union *types, **options
end

.parse_number(s) ⇒ Object



10
11
12
13
14
# File 'lib/nrser/types/numbers.rb', line 10

def self.parse_number s
  float = s.to_f
  int = float.to_i
  if float == int then int else float end
end

.pos_intObject



59
60
61
# File 'lib/nrser/types/numbers.rb', line 59

def self.pos_int
  POS_INT
end

.str(**options) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/nrser/types/strings.rb', line 11

def self.str **options
  if options.empty?
    # if there are no options can point to the constant for efficiency
    STR
  else
    types = []
    
    if options[:length]
      types << length(options[:length])
    end
    
    intersection STR, *types
  end
end

.stringObject

string



26
27
28
# File 'lib/nrser/types/strings.rb', line 26

def self.string
  str
end

.test(value, type) ⇒ Object



31
32
33
# File 'lib/nrser/types.rb', line 31

def self.test value, type
  make(type).test value
end

.trueObject



20
21
22
# File 'lib/nrser/types/booleans.rb', line 20

def self.true
  TRUE
end

.union(*types, **options) ⇒ Object

match any of the types



62
63
64
# File 'lib/nrser/types/combinators.rb', line 62

def self.union *types, **options
  NRSER::Types::Union.new *types, **options
end

.where(**options, &block) ⇒ Object

create a type based on a predicate



19
20
21
# File 'lib/nrser/types/where.rb', line 19

def self.where **options, &block
  Where.new block, **options
end

.zeroObject



21
22
23
# File 'lib/nrser/types/numbers.rb', line 21

def self.zero
  ZERO
end