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



75
76
77
78
79
80
# File 'lib/n_cipher/argument_validation.rb', line 75

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



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

def define_proxy_method(owner, name)
  owner.class_eval do
    define_method(name) do |*args, &block|
      validations = ::NCipher::ArgumentValidation
        .validations
        .values_at(self.class, self.singleton_class)
        .compact
        .each_with_object({}) {|orig_h,rtn_h| rtn_h.merge!(orig_h) }

      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



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

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

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



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

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

.validationsObject



82
83
84
# File 'lib/n_cipher/argument_validation.rb', line 82

def validations
  @@validations
end