Module: Lab42::BasicConstraints::Implementation

Extended by:
Implementation
Included in:
Implementation
Defined in:
lib/lab42/basic_constraints/implementation.rb

Constant Summary collapse

IntConstraints =
{
  day: 1..31,
  hour: 0..23,
  minute: 0..59,
  month: 1..12,
  second: 0..59
}
StringConstraints =
{
  all_digits_string: %r{\A\d*\z},
  alphanumeric_string: %r{\A[[:alnum:]]*\z}
}

Instance Method Summary collapse

Instance Method Details

#date(default: nil) ⇒ Object



36
37
38
39
40
41
42
43
44
# File 'lib/lab42/basic_constraints/implementation.rb', line 36

def date default: nil
  require "date"
  Constraint.new(:date) do |value|
    Date.parse(value) rescue false
  end
    .set_default default do
    Date.today.iso8601
  end
end

#date_time(default: nil) ⇒ Object



46
47
48
49
50
51
52
53
54
# File 'lib/lab42/basic_constraints/implementation.rb', line 46

def date_time default: nil
  require "date"
  Constraint.new(:date_time) do |value|
    DateTime.parse(value) rescue false
  end
    .set_default default do
    DateTime.now.iso8601
  end
end

#int_range(range: nil, min: nil, max: nil) ⇒ Object



21
22
23
# File 'lib/lab42/basic_constraints/implementation.rb', line 21

def int_range(range: nil, min: nil, max: nil)
  Constraint::IntConstraint.new(:int_range, range: range, min: min, max: max)
end

#limited_string(size: nil, min: nil, max: nil) ⇒ Object



56
57
58
59
60
61
# File 'lib/lab42/basic_constraints/implementation.rb', line 56

def limited_string size: nil, min: nil, max: nil
  size_range = Helpers::RangeHelper.make_range!(range: size, min: min, max: max)
  Constraint.new(:limited_string) do |value|
    String === value && size_range === value.size
  end
end

#lowercase_stringObject



63
64
65
66
67
# File 'lib/lab42/basic_constraints/implementation.rb', line 63

def lowercase_string
  Constraint.new :lowercase_string do |value|
    String === value && value.downcase == value
  end
end

#non_negative_intObject



69
70
71
72
# File 'lib/lab42/basic_constraints/implementation.rb', line 69

def non_negative_int
  Constraint::IntConstraint.new(:non_negative_int, min: 0)
    .set_default(0)
end

#non_negative_numberObject



74
75
76
77
78
# File 'lib/lab42/basic_constraints/implementation.rb', line 74

def non_negative_number
  Constraint.new :non_negative_number do |value|
    Numeric === value && !(Complex === value) && value >= 0
  end
end

#positive_intObject



80
81
82
83
# File 'lib/lab42/basic_constraints/implementation.rb', line 80

def positive_int
  Constraint::IntConstraint.new(:positive_int, min: 1)
    .set_default(1)
end

#positive_numberObject



85
86
87
88
89
# File 'lib/lab42/basic_constraints/implementation.rb', line 85

def positive_number
  Constraint.new :positive_number do |value|
    Numeric === value && value > 0
  end
end

#uppercase_stringObject



91
92
93
94
95
# File 'lib/lab42/basic_constraints/implementation.rb', line 91

def uppercase_string
  Constraint.new :uppercase_string do |value|
    String === value && value.upcase == value
  end
end

#yearObject



97
98
99
100
101
102
103
104
# File 'lib/lab42/basic_constraints/implementation.rb', line 97

def year
  Constraint.new(:year) do |value|
    Integer === value
  end
    .set_default do 
      Time.now.utc.year
    end
end