Module: Knight::InstanceMethods

Defined in:
lib/knight/instance_methods.rb

Overview

Base functionality that added when included to a class

Defined Under Namespace

Modules: ClassMethods

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#resourceObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Return the resource

Returns:

  • (Object)


13
14
15
# File 'lib/knight/instance_methods.rb', line 13

def resource
  @resource
end

Class Method Details

.included(base) ⇒ undefined

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Extend base class to support validation

Parameters:

  • object (Object)

Returns:

  • (undefined)


22
23
24
25
# File 'lib/knight/instance_methods.rb', line 22

def self.included(base)
  super
  base.extend(ClassMethods)
end

Instance Method Details

#initialize(resource) ⇒ undefined

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Set the resource

Parameters:

  • resource (Object)

Returns:

  • (undefined)


34
35
36
# File 'lib/knight/instance_methods.rb', line 34

def initialize(resource)
  @resource = resource
end

#run(context = Validator::DEFAULT_CONTEXT) ⇒ Result

Return the validation result

Examples:

user = User.new

class RegistrationValidator
  include Knight::InstanceMethods
  validator.add(Knight::Rule::Presence.new(:username))
end

result = RegistrationValidator.new(user).result

Returns:



53
54
55
# File 'lib/knight/instance_methods.rb', line 53

def run(context = Validator::DEFAULT_CONTEXT)
  self.class.validator.run(resource, context)
end