method_found 0.1.4 → 0.1.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9bc50110082afd5058e16422c320e7b48ca7ea75
4
- data.tar.gz: 154aa54df987957c7b405ac9f669546a69790f33
3
+ metadata.gz: 5170324feef4ea0010c3934edbac271307fedd1d
4
+ data.tar.gz: fade9008001c4fce5121f64dfad3086e9a4764be
5
5
  SHA512:
6
- metadata.gz: 3ccf3e88a3726c8c709b0df1c479952d85f08e787e2d1cf218eeef2a59f1f62128dbc27e534fb13c214b55f0c0c7644b418ec66d7502247316fe209c11f544b0
7
- data.tar.gz: 2bb0be7f5e689e5aec743de2dc90d5bbb0bcf534ff76207afede9373eac35ae52073fcd63951859d4e656f7c227f646dc98fc12837fdf6689188db1311f035ba
6
+ metadata.gz: 1b6607f908acf0a62cd15c67903f5c5acbd2d5bec5b5b8f2e20eca93f45f4e670ade5181762816207ed9824b86cb7eceaac95f5b393455fe4cf49f922861ddd6
7
+ data.tar.gz: ed308bf21a68260776231d450a1d57ba565861d1e9065fc844f32130ccb8043f96edc2333d9cb415e9051acd6beb81bd724328209e20b8244d594543bba69c6f
data/README.md CHANGED
@@ -14,7 +14,7 @@ Intercept `method_missing` and do something useful with it.
14
14
  Add to your Gemfile:
15
15
 
16
16
  ```ruby
17
- gem 'method_found', '~> 0.1.4'
17
+ gem 'method_found', '~> 0.1.5'
18
18
  ```
19
19
 
20
20
  And bundle it.
@@ -3,6 +3,7 @@ require "securerandom"
3
3
  require "method_found/version"
4
4
  require "method_found/builder"
5
5
  require "method_found/interceptor"
6
+ require "method_found/attribute_methods"
6
7
 
7
8
  module MethodFound
8
9
  def self.new(*args, &block)
@@ -43,31 +43,49 @@ attribute name or set of attribute names.
43
43
  =end
44
44
  class AttributeInterceptor < Interceptor
45
45
  def initialize(prefix: '', suffix: '')
46
- @prefix, @suffix = prefix, suffix
47
- regex_ = regex
48
- attribute_matcher = proc do |method_name|
49
- (matches = regex_.match(method_name)) && attributes.include?(matches[1]) && matches[1]
50
- end
51
- attribute_matcher.define_singleton_method :inspect do
52
- regex_.inspect
53
- end
46
+ @regex = /\A(?:#{Regexp.escape(prefix)})(.*)(?:#{Regexp.escape(suffix)})\z/
47
+ @method_missing_target = method_missing_target = "#{prefix}attribute#{suffix}"
48
+ @method_name = "#{prefix}%s#{suffix}"
54
49
 
55
- super attribute_matcher do |_, attr_name, *arguments, &block|
56
- send("#{prefix}attribute#{suffix}", attr_name, *arguments, &block)
50
+ super to_proc do |_, attr_name, *args, &block|
51
+ send(method_missing_target, attr_name, *args, &block)
57
52
  end
58
53
  end
59
54
 
60
- def regex
61
- /\A(?:#{Regexp.escape(@prefix)})(.*)(?:#{Regexp.escape(@suffix)})\z/.freeze
62
- end
63
-
64
55
  def define_attribute_methods(*attr_names)
65
- prefix, suffix = @prefix, @suffix
56
+ handler = @method_missing_target
66
57
  attr_names.each do |attr_name|
67
- define_method "#{@prefix}#{attr_name}#{@suffix}".freeze do |*arguments, &block|
68
- send("#{prefix}attribute#{suffix}".freeze, attr_name, *arguments, &block)
58
+ define_method method_name(attr_name) do |*arguments, &block|
59
+ send(handler, attr_name, *arguments, &block)
69
60
  end
70
61
  end
71
62
  end
63
+
64
+ def alias_attribute(new_name, old_name)
65
+ handler = method_name(old_name)
66
+ define_method method_name(new_name) do |*arguments, &block|
67
+ send(handler, *arguments, &block)
68
+ end
69
+ end
70
+
71
+ def to_proc
72
+ regex = @regex
73
+ proc do |method_name|
74
+ (matches = regex.match(method_name)) &&
75
+ (method_name != :attributes) &&
76
+ respond_to?(:attributes) &&
77
+ (attributes.keys & matches.captures).first
78
+ end
79
+ end
80
+
81
+ def inspect
82
+ "<#{self.class.name}: #{@regex.inspect}>"
83
+ end
84
+
85
+ private
86
+
87
+ def method_name(attr_name)
88
+ @method_name % attr_name
89
+ end
72
90
  end
73
91
  end
@@ -3,6 +3,9 @@ require "method_found/attribute_interceptor"
3
3
  module MethodFound
4
4
  =begin
5
5
 
6
+ Re-implementation of ActiveModel::AttributeMethods from Rails using
7
+ {MethodFound::Interceptor} modules instead of class variables.
8
+
6
9
  @example
7
10
  class Person < Struct.new(:attributes)
8
11
  include MethodFound::AttributeMethods
@@ -45,22 +48,29 @@ module MethodFound
45
48
  =end
46
49
  module AttributeMethods
47
50
  def self.included(base)
51
+ base.include(AttributeInterceptor.new)
48
52
  base.instance_eval do
49
- def attribute_method_affix(prefix: '', suffix: '')
50
- include(AttributeInterceptor.new(prefix: prefix, suffix: suffix))
53
+ def attribute_method_prefix(*prefixes)
54
+ prefixes.each { |prefix| include AttributeInterceptor.new(prefix: prefix) }
55
+ end
56
+
57
+ def attribute_method_suffix(*suffixes)
58
+ suffixes.each { |suffix| include AttributeInterceptor.new(suffix: suffix) }
51
59
  end
52
60
 
53
- def attribute_method_suffix(suffix)
54
- include(AttributeInterceptor.new(suffix: suffix))
61
+ def attribute_method_affix(*affixes)
62
+ affixes.each { |affix| include AttributeInterceptor.new(prefix: affix[:prefix], suffix: affix[:suffix]) }
55
63
  end
56
64
 
57
- def attribute_method_prefix(prefix)
58
- include(AttributeInterceptor.new(prefix: prefix))
65
+ def define_attribute_methods(*attr_names)
66
+ ancestors.each do |ancestor|
67
+ ancestor.define_attribute_methods(*attr_names) if ancestor.is_a?(AttributeInterceptor)
68
+ end
59
69
  end
60
70
 
61
- def define_attribute_methods(*attributes)
71
+ def alias_attribute(new_name, old_name)
62
72
  ancestors.each do |ancestor|
63
- ancestor.define_attribute_methods(*attributes) if ancestor.is_a?(AttributeInterceptor)
73
+ ancestor.alias_attribute(new_name, old_name) if ancestor.is_a?(AttributeInterceptor)
64
74
  end
65
75
  end
66
76
  end
@@ -27,9 +27,9 @@ Creates set of interceptors to include into a class.
27
27
 
28
28
  # @yield Yields builder as context to block, to allow calling builder
29
29
  # methods to create interceptors in included class.
30
- def initialize(&block)
30
+ def initialize
31
31
  @interceptors = []
32
- instance_eval &block
32
+ super
33
33
  end
34
34
 
35
35
  def intercept(*args, &block)
@@ -1,3 +1,3 @@
1
1
  module MethodFound
2
- VERSION = "0.1.4"
2
+ VERSION = "0.1.5"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: method_found
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chris Salzberg
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-05-06 00:00:00.000000000 Z
11
+ date: 2017-05-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler