Class: Tchae::PositionalArgs

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

Instance Method Summary collapse

Constructor Details

#initialize(wrapper) ⇒ PositionalArgs

Returns a new instance of PositionalArgs.



198
199
200
201
# File 'lib/tchae/core.rb', line 198

def initialize(wrapper)
  super()
  @wrapper = wrapper
end

Instance Method Details

#arg(&proc) ⇒ Object



203
204
205
206
# File 'lib/tchae/core.rb', line 203

def arg(&proc)
  push Tchae(proc)
  @wrapper
end

#expect(*constraints, &proc) ⇒ Object

def expect(constraints = EMPTY_ARRAY, &proc)



209
210
211
212
213
# File 'lib/tchae/core.rb', line 209

def expect(*constraints, &proc)
  constraints.each { |inp| push Tchae(inp) }
  instance_exec(&proc) if block_given?
  @wrapper
end

#valid_or_raise_exception(params) ⇒ Object



215
216
217
218
219
220
221
# File 'lib/tchae/core.rb', line 215

def valid_or_raise_exception(params)
  params.zip(self).each do |parm, valtor|
    next unless valtor

    valtor.valid_or_raise_exception(parm, ::Tchae::ArgumentTypeException)
  end
end

#valid_or_return_error(params) ⇒ Object



223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/tchae/core.rb', line 223

def valid_or_return_error(params)
  # returns nil if everything valid or an Error object otherwise
  valid = true
  failed = params.zip(self).each do |parm, valtor|
    next unless valtor

    unless valtor.execute(parm)
      valid = false
      break valtor.return_error(parm, ::Tchae::ArgumentTypeError)
    end
  end
  valid ? nil : failed
end