method_found 0.1.4 → 0.1.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +1 -1
- data/lib/method_found.rb +1 -0
- data/lib/method_found/attribute_interceptor.rb +35 -17
- data/lib/method_found/attribute_methods.rb +18 -8
- data/lib/method_found/builder.rb +2 -2
- data/lib/method_found/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5170324feef4ea0010c3934edbac271307fedd1d
|
4
|
+
data.tar.gz: fade9008001c4fce5121f64dfad3086e9a4764be
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1b6607f908acf0a62cd15c67903f5c5acbd2d5bec5b5b8f2e20eca93f45f4e670ade5181762816207ed9824b86cb7eceaac95f5b393455fe4cf49f922861ddd6
|
7
|
+
data.tar.gz: ed308bf21a68260776231d450a1d57ba565861d1e9065fc844f32130ccb8043f96edc2333d9cb415e9051acd6beb81bd724328209e20b8244d594543bba69c6f
|
data/README.md
CHANGED
data/lib/method_found.rb
CHANGED
@@ -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
|
-
@
|
47
|
-
|
48
|
-
|
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
|
56
|
-
send(
|
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
|
-
|
56
|
+
handler = @method_missing_target
|
66
57
|
attr_names.each do |attr_name|
|
67
|
-
define_method
|
68
|
-
send(
|
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
|
50
|
-
include
|
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
|
54
|
-
include
|
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
|
58
|
-
|
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
|
71
|
+
def alias_attribute(new_name, old_name)
|
62
72
|
ancestors.each do |ancestor|
|
63
|
-
ancestor.
|
73
|
+
ancestor.alias_attribute(new_name, old_name) if ancestor.is_a?(AttributeInterceptor)
|
64
74
|
end
|
65
75
|
end
|
66
76
|
end
|
data/lib/method_found/builder.rb
CHANGED
@@ -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
|
30
|
+
def initialize
|
31
31
|
@interceptors = []
|
32
|
-
|
32
|
+
super
|
33
33
|
end
|
34
34
|
|
35
35
|
def intercept(*args, &block)
|
data/lib/method_found/version.rb
CHANGED
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
|
+
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-
|
11
|
+
date: 2017-05-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|