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

Constant Summary collapse

DEFAULT_CONTEXT =
:default

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)


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

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)


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

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)


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

def initialize(resource)
  @resource = resource
end

#run(context = DEFAULT_CONTEXT) ⇒ Result

Return the validation result

Examples:

user = User.new

class RegistrationValidator
  include Knight::InstanceMethods
end

result = RegistrationValidator.new(user).run

Parameters:

  • context (Symbol) (defaults to: DEFAULT_CONTEXT)

Returns:



55
56
57
# File 'lib/knight/instance_methods.rb', line 55

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