Class: Ree::Contracts::ArgContracts::RangeOf
- Extended by:
- Squarable
- Includes:
- Truncatable
- Defined in:
- lib/ree/contracts/arg_contracts/range_of.rb
Instance Attribute Summary collapse
-
#validator ⇒ Object
readonly
Returns the value of attribute validator.
Instance Method Summary collapse
-
#initialize(*contracts) ⇒ RangeOf
constructor
A new instance of RangeOf.
- #message(value, name, lvl = 1) ⇒ Object
- #to_s ⇒ Object
- #valid?(value) ⇒ Boolean
Methods included from Squarable
Methods included from Truncatable
Constructor Details
#initialize(*contracts) ⇒ RangeOf
Returns a new instance of RangeOf.
11 12 13 14 15 16 17 |
# File 'lib/ree/contracts/arg_contracts/range_of.rb', line 11 def initialize(*contracts) if contracts.size != 1 raise BadContractError, 'RangeOf should accept exactly one contract' end @validator = Validators.fetch_for(contracts.first) end |
Instance Attribute Details
#validator ⇒ Object (readonly)
Returns the value of attribute validator.
9 10 11 |
# File 'lib/ree/contracts/arg_contracts/range_of.rb', line 9 def validator @validator end |
Instance Method Details
#message(value, name, lvl = 1) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/ree/contracts/arg_contracts/range_of.rb', line 29 def (value, name, lvl = 1) unless value.is_a?(Range) return "expected Range, got #{value.class} => #{truncate(value.inspect)}" end errors = [] sps = " " * lvl unless validator.call(value.begin) msg = validator.(value.begin, "#{name}.begin", lvl + 1) errors << "\n\t#{sps} - #{name}.begin: #{msg}" end unless validator.call(value.end) msg = validator.(value.end, "#{name}.end", lvl + 1) errors << "\n\t#{sps} - #{name}.end: #{msg}" end errors.join end |
#to_s ⇒ Object
25 26 27 |
# File 'lib/ree/contracts/arg_contracts/range_of.rb', line 25 def to_s "RangeOf[#{validator.to_s}]" end |
#valid?(value) ⇒ Boolean
19 20 21 22 23 |
# File 'lib/ree/contracts/arg_contracts/range_of.rb', line 19 def valid?(value) value.is_a?(Range) && validator.call(value.begin) && validator.call(value.end) end |