Module: NCipher::ArgumentValidation

Included in:
NCipher, Configuration
Defined in:
lib/n_cipher/argument_validation.rb

Defined Under Namespace

Modules: ClassMethod, InheritValidation, MonkeyPatchToMethodClass

Constant Summary collapse

@@validations =
{}

Class Method Summary collapse

Class Method Details

.around_alias(owner, new_name, old_name) ⇒ Object



72
73
74
75
76
77
# File 'lib/n_cipher/argument_validation.rb', line 72

def around_alias(owner, new_name, old_name)
  owner.class_eval do
    private(old_name)
    alias_method new_name, old_name
  end
end

.define_proxy_method(owner, name) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/n_cipher/argument_validation.rb', line 55

def define_proxy_method(owner, name)
  owner.class_eval do
    define_method(name) do |*args, &block|
      validations = ::NCipher::ArgumentValidation.validations.tap do |hash|
        break hash.has_key?(self.class) ? hash[self.class] : hash[self.singleton_class]
      end

      validations["__#{name}__"].each do |pattern|
        # ブロックをインスタンスのコンテキストで評価
        raise(pattern[:exception]) unless self.instance_exec(*args, block, &pattern[:validation])
      end

      __send__("__#{name}__", *args, &block)
    end
  end
end

.included(klass) ⇒ Object



4
5
6
7
8
# File 'lib/n_cipher/argument_validation.rb', line 4

def self.included(klass)
  klass.extend(ClassMethod)
  klass.extend(InheritValidation)
  Method.send(:include, MonkeyPatchToMethodClass)
end

.store_validation(owner, method_name, validation, exception) ⇒ Object



46
47
48
49
50
51
52
53
# File 'lib/n_cipher/argument_validation.rb', line 46

def store_validation(owner, method_name, validation, exception)
  @@validations[owner] ||= {}
  @@validations[owner][method_name] ||= []
  @@validations[owner][method_name] << {
    :validation => validation,
    :exception  => exception
  }
end

.validationsObject



79
80
81
# File 'lib/n_cipher/argument_validation.rb', line 79

def validations
  @@validations
end