Module: Stretchy::Utils::Contract::ClassMethods
- Defined in:
- lib/stretchy/utils/contract.rb
Instance Attribute Summary collapse
-
#contracts ⇒ Object
readonly
Returns the value of attribute contracts.
Instance Method Summary collapse
- #assert_in(name, value, options) ⇒ Object
- #assert_matches(name, value, options) ⇒ Object
- #assert_required(name, value, options) ⇒ Object
- #assert_responds_to(name, value, options) ⇒ Object
- #assert_type(name, value, options) ⇒ Object
- #contract(var, options = {}) ⇒ Object
- #fail_assertion(msg) ⇒ Object
- #is_empty?(value) ⇒ Boolean
Instance Attribute Details
#contracts ⇒ Object (readonly)
Returns the value of attribute contracts.
42 43 44 |
# File 'lib/stretchy/utils/contract.rb', line 42 def contracts @contracts end |
Instance Method Details
#assert_in(name, value, options) ⇒ Object
106 107 108 109 110 |
# File 'lib/stretchy/utils/contract.rb', line 106 def assert_in(name, value, ) collection = [:in] msg = "Expected #{name} to be one of #{collection}, but got #{value}" fail_assertion(msg) unless collection.include?(value) end |
#assert_matches(name, value, options) ⇒ Object
112 113 114 115 116 |
# File 'lib/stretchy/utils/contract.rb', line 112 def assert_matches(name, value, ) matcher = [:matches] msg = "Expected #{name} to match #{matcher}, but #{value} does not" fail_assertion(msg) unless matcher.match(value) end |
#assert_required(name, value, options) ⇒ Object
63 64 65 66 |
# File 'lib/stretchy/utils/contract.rb', line 63 def assert_required(name, value, ) msg = "Expected to have param #{name}, but got an #{value.inspect}" fail_assertion(msg) if is_empty?(value) end |
#assert_responds_to(name, value, options) ⇒ Object
100 101 102 103 104 |
# File 'lib/stretchy/utils/contract.rb', line 100 def assert_responds_to(name, value, ) method = [:responds_to] msg = "Expected #{name} to respond_to #{method}, but #{value.class.name} does not" fail_assertion(msg) unless value.respond_to?(method) end |
#assert_type(name, value, options) ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/stretchy/utils/contract.rb', line 68 def assert_type(name, value, ) type = [:type] case type when :distance msg = "Expected #{name} to be a distance measure, but #{value} is not a valid distance" fail_assertion(msg) unless String(value).match(DISTANCE_FORMAT) when :lat msg = "Expected #{name} to be between 90 and -90, but was #{value}" value = Float(value) rescue nil fail_assertion(msg) unless value && value <= 90 && value >= -90 when :lng msg = "Expected #{name} to be between 180 and -180, but was #{value}" value = Float(value) rescue nil fail_assertion(msg) unless value && value.to_f <= 180 && value.to_f >= -180 when :field msg = "Expected #{name} to be a string, symbol, or number, but got #{value.class.name}" fail_assertion(msg) unless value.is_a?(String) || value.is_a?(Symbol) || value.is_a?(Numeric) msg = "Expected #{name} to be a string, symbol, or number, but was blank" fail_assertion(msg) if value.is_a?(String) && value.empty? when :decay msg = "Expected #{name} to be one of #{DECAY_FUNCTIONS.join(', ')}, but got #{value}" fail_assertion(msg) unless DECAY_FUNCTIONS.any?{|f| f == value || f.to_s == value } when Array msg = "Expected #{name} to be one of #{type.map{|t| t.name}}, but got #{value.class.name}" fail_assertion(msg) unless type.any?{|t| value.is_a?(t)} else msg = "Expected #{name} to be of type #{type}, but found #{value.class.name}" fail_assertion(msg) unless value.is_a?(type) end end |
#contract(var, options = {}) ⇒ Object
50 51 52 53 54 55 56 57 |
# File 'lib/stretchy/utils/contract.rb', line 50 def contract(var, = {}) @contracts ||= {} if var.is_a?(Hash) var.each {|k,v| @contracts[k] = v } else @contracts[var] = end end |
#fail_assertion(msg) ⇒ Object
59 60 61 |
# File 'lib/stretchy/utils/contract.rb', line 59 def fail_assertion(msg) raise Stretchy::Errors::ContractError.new(msg) end |
#is_empty?(value) ⇒ Boolean
44 45 46 47 48 |
# File 'lib/stretchy/utils/contract.rb', line 44 def is_empty?(value) value.nil? || (value.respond_to?(:any?) && !value.any?) || (value.respond_to?(:length) && value.length == 0) end |