Class: ParamsReady::Value::Constraint

Inherits:
Object
  • Object
show all
Extended by:
Extensions::Registry
Defined in:
lib/params_ready/value/constraint.rb

Defined Under Namespace

Classes: Error

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Extensions::Registry

human_string, registry

Constructor Details

#initialize(cond) ⇒ Constraint

Returns a new instance of Constraint.



21
22
23
24
# File 'lib/params_ready/value/constraint.rb', line 21

def initialize(cond)
  @condition = cond.freeze
  freeze
end

Instance Attribute Details

#conditionObject (readonly)

Returns the value of attribute condition.



19
20
21
# File 'lib/params_ready/value/constraint.rb', line 19

def condition
  @condition
end

Class Method Details

.build(cond, *args, **opts, &block) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/params_ready/value/constraint.rb', line 28

def self.build(cond, *args, **opts, &block)
  if block.nil?
    new cond, *args, **opts
  else
    new cond, block, *args, **opts
  end
end

.instance(cond, *args, **opts) ⇒ Object



36
37
38
39
40
41
42
43
44
45
# File 'lib/params_ready/value/constraint.rb', line 36

def self.instance(cond, *args, **opts)
  case cond
  when Range
    RangeConstraint.new(cond, *args, **opts)
  when Array, Set
    EnumConstraint.new(cond, *args, **opts)
  else
    raise ParamsReadyError, "Unknown constraint type: " + cond.class.name
  end
end

.register(name) ⇒ Object



15
16
17
# File 'lib/params_ready/value/constraint.rb', line 15

def self.register(name)
  Constraint.register_constraint_type(name, self)
end

Instance Method Details

#clamp?Boolean

Returns:

  • (Boolean)


26
# File 'lib/params_ready/value/constraint.rb', line 26

def clamp?; false; end

#error_messageObject



51
52
53
# File 'lib/params_ready/value/constraint.rb', line 51

def error_message
  "didn't pass validation"
end

#valid?(input) ⇒ Boolean

Returns:

  • (Boolean)

Raises:



47
48
49
# File 'lib/params_ready/value/constraint.rb', line 47

def valid?(input)
  raise ParamsReadyError, 'This is an abstract class'
end