Module: MethodAttributes

Extended by:
MethodAttributes
Included in:
MethodAttributes
Defined in:
lib/common/method_attr.rb

Overview

ap Foo.method_attr {

"test": {
  "name": [
    "Test method desc"
  ],
  "param": [
    {
      "name": "email",
      "type": "String"
    }
  ]
}

}

Constant Summary collapse

@@G_OPTS =
{}
@@M_OPTS =
{}

Instance Method Summary collapse

Instance Method Details

#define(klass, param_name, &block) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/common/method_attr.rb', line 36

def define klass, param_name, &block
  klass.define_singleton_method(param_name) do |*args|
    @@M_OPTS[param_name] ||= []
    @@M_OPTS[param_name].push block ? block.call(*args) : args[0]
  end

  klass.define_singleton_method(:method_added) do |name|
    return unless @@M_OPTS.keys.first

    @@G_OPTS[to_s] ||= {}
    @@G_OPTS[to_s][name] = @@M_OPTS.dup
    @@M_OPTS.clear
  end
end

#get(klass, method_name = nil) ⇒ Object



51
52
53
54
55
56
57
58
# File 'lib/common/method_attr.rb', line 51

def get klass, method_name=nil
  return @@G_OPTS[klass.to_s] unless method_name

  klass.ancestors.map(&:to_s).each do |a_klass|
    v = @@G_OPTS[a_klass][method_name]
    return v if v
  end
end