Class: Sukima

Inherits:
Object
  • Object
show all
Defined in:
lib/sukima.rb,
lib/sukima/dsl.rb,
lib/sukima/constraints.rb,
lib/sukima/error_field.rb

Defined Under Namespace

Modules: Constraints Classes: Dsl, ErrorField

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**constraints, &block) ⇒ Sukima

Returns a new instance of Sukima.



10
11
12
13
# File 'lib/sukima.rb', line 10

def initialize(**constraints, &block)
  @constraints = constraints
  @block = block
end

Instance Attribute Details

#blockObject (readonly)

Returns the value of attribute block.



8
9
10
# File 'lib/sukima.rb', line 8

def block
  @block
end

#constraintsObject (readonly)

Returns the value of attribute constraints.



8
9
10
# File 'lib/sukima.rb', line 8

def constraints
  @constraints
end

Instance Method Details

#validate(value) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/sukima.rb', line 15

def validate(value)
  error_field = ErrorField.new
  return error_field if !@constraints[:nonnil] && value.nil?

  @constraints.each do |key, args|
    next if key == :required

    message = Constraints.send(key, args, value)
    error_field.errors << message if message
  end

  Dsl.new(value, error_field).instance_exec(value, &@block) if @block && value.is_a?(@constraints[:type])
  error_field
end