Class: Tchae::Validator

Inherits:
Object show all
Defined in:
lib/tchae/core.rb

Direct Known Subclasses

Validator1

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(proc, msg: nil) ⇒ Validator

Returns a new instance of Validator.



117
118
119
120
# File 'lib/tchae/core.rb', line 117

def initialize(proc, msg: nil)
  @proc = proc
  @message = msg.nil? ? "does not fulfill #{proc}.to_s" : msg
end

Instance Attribute Details

#messageObject (readonly)

Returns the value of attribute message.



115
116
117
# File 'lib/tchae/core.rb', line 115

def message
  @message
end

Instance Method Details

#execute(input) ⇒ Object



122
123
124
# File 'lib/tchae/core.rb', line 122

def execute(input)
  input.instance_exec(&@proc)
end

#return_error(input, eklass) ⇒ Object



136
137
138
# File 'lib/tchae/core.rb', line 136

def return_error(input, eklass)
  eklass.new(input)
end

#valid_or_raise_exception(input, eklass) ⇒ Object



126
127
128
129
130
# File 'lib/tchae/core.rb', line 126

def valid_or_raise_exception(input, eklass)
  raise eklass unless execute(input)

  input
end

#valid_or_return_error(input, eklass) ⇒ Object



132
133
134
# File 'lib/tchae/core.rb', line 132

def valid_or_return_error(input, eklass)
  execute(input) ? input : return_error(input, eklass)
end

#wrapped_result_or_error(result, eklass) ⇒ Object



140
141
142
# File 'lib/tchae/core.rb', line 140

def wrapped_result_or_error(result, eklass)
  execute(result) ? Return(result) : Return(nil, return_error(result, eklass))
end