Module: Resto::Validate

Included in:
Inclusion, Length, Presence
Defined in:
lib/resto/validate.rb,
lib/resto/validate/length.rb,
lib/resto/validate/presence.rb,
lib/resto/validate/inclusion.rb

Defined Under Namespace

Classes: Inclusion, Length, Presence

Instance Method Summary collapse

Instance Method Details

#if(&block) ⇒ Object



23
24
25
# File 'lib/resto/validate.rb', line 23

def if(&block)
  tap { @if = block }
end

#initializeObject



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

def initialize
  @if     = lambda { |resource| true  }
  @unless = lambda { |resource| false }
end

#message(error_message) ⇒ Object



19
20
21
# File 'lib/resto/validate.rb', line 19

def message(error_message)
  tap { @message = error_message }
end

#unless(&block) ⇒ Object



27
28
29
# File 'lib/resto/validate.rb', line 27

def unless(&block)
  tap { @unless = block }
end

#validate?(resource) ⇒ Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/resto/validate.rb', line 15

def validate?(resource)
  (@if.call(resource) && !@unless.call(resource))
end