Class: MethodFound::AttributeInterceptor
- Inherits:
-
Interceptor
- Object
- Module
- Interceptor
- MethodFound::AttributeInterceptor
- Defined in:
- lib/method_found/attribute_interceptor.rb
Overview
Class defining prefix, suffix and affix methods to a class for a given attribute name or set of attribute names.
Instance Attribute Summary
Attributes inherited from Interceptor
Instance Method Summary collapse
- #define_attribute_methods(*attr_names) ⇒ Object
-
#initialize(prefix: '', suffix: '') ⇒ AttributeInterceptor
constructor
A new instance of AttributeInterceptor.
- #regex ⇒ Object
Methods inherited from Interceptor
#define_method_missing, #inspect
Constructor Details
#initialize(prefix: '', suffix: '') ⇒ AttributeInterceptor
Returns a new instance of AttributeInterceptor.
45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/method_found/attribute_interceptor.rb', line 45 def initialize(prefix: '', suffix: '') @prefix, @suffix = prefix, suffix regex_ = regex attribute_matcher = proc do |method_name| (matches = regex_.match(method_name)) && attributes.include?(matches[1]) && matches[1] end attribute_matcher.define_singleton_method :inspect do regex_.inspect end super attribute_matcher do |_, attr_name, *arguments, &block| send("#{prefix}attribute#{suffix}", attr_name, *arguments, &block) end end |
Instance Method Details
#define_attribute_methods(*attr_names) ⇒ Object
64 65 66 67 68 69 70 71 |
# File 'lib/method_found/attribute_interceptor.rb', line 64 def define_attribute_methods(*attr_names) prefix, suffix = @prefix, @suffix attr_names.each do |attr_name| define_method "#{@prefix}#{attr_name}#{@suffix}".freeze do |*arguments, &block| send("#{prefix}attribute#{suffix}".freeze, attr_name, *arguments, &block) end end end |
#regex ⇒ Object
60 61 62 |
# File 'lib/method_found/attribute_interceptor.rb', line 60 def regex /\A(?:#{Regexp.escape(@prefix)})(.*)(?:#{Regexp.escape(@suffix)})\z/.freeze end |