active_stripper 0.3.0 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/active_stripper.rb +38 -21
- data/lib/active_stripper/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1d39d3a9cc0d5806ad23edbe74c7eaa97caefe3d
|
4
|
+
data.tar.gz: 8f8fe1a5a8f5e776c8f3edcfefffa49f0fc1195e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d5335aebed86f6d18daa880f2178cbd714124dd200666fb1cff855c30b9518e532578d10ab4507d631169fc7380d0e8d13f8e58186b5c6a17515aea27a5f3d16
|
7
|
+
data.tar.gz: 15bdb138efc4d1b1fcf449c0209f2defb63b86cbefaa08028d9cca58a570bf86442c7cc43f28d294339470a87df2f11288d7adb7ba858ae4e5fd0cee4a78d783
|
data/lib/active_stripper.rb
CHANGED
@@ -21,36 +21,50 @@ module ActiveStripper # Pun intended
|
|
21
21
|
def self.iterable_apply(val, iterable_operators, base)
|
22
22
|
iterable_operators.each do | operator_name, operator_args |
|
23
23
|
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
24
|
+
additionnal_args = nil
|
25
|
+
lookup_module_name = "ActiveStripper::Helpers"
|
26
|
+
|
27
|
+
if operator_args
|
28
|
+
additionnal_args = operator_args[:additionnal_args]
|
29
|
+
lookup_module_name = operator_args[:module] if operator_args[:module]
|
30
|
+
end
|
31
|
+
|
32
|
+
val = ActiveStripper.method_apply(val,
|
33
|
+
operator_name,
|
34
|
+
base,
|
35
|
+
lookup_module_name,
|
36
|
+
additionnal_args)
|
31
37
|
end
|
32
38
|
|
33
39
|
return val
|
34
40
|
end
|
35
41
|
|
36
42
|
#
|
37
|
-
# Look up for method `operator` in ActiveStripper::Helpers
|
38
|
-
# class and call it
|
43
|
+
# Look up for method `operator` in ActiveStripper::Helpers THEN in current
|
44
|
+
# class if not found and call it
|
39
45
|
#
|
40
|
-
# @param [String] val
|
41
|
-
# @param [
|
42
|
-
# @param [Object] base
|
46
|
+
# @param [String] val Value to modify
|
47
|
+
# @param [Symbol] operator Method name to execute
|
48
|
+
# @param [Object] base Instance including the current module
|
49
|
+
# @param [String] module_name Name of the module where we are going to lookup
|
50
|
+
# a method which name is contained in `operator`
|
51
|
+
# if equal to EMPTY STRING the lookup context will
|
52
|
+
# be the object passed through `base`
|
53
|
+
# @param [Array] args (Optional)
|
54
|
+
# Additionnal args to pass to the processor method
|
43
55
|
#
|
44
|
-
# @return [String]
|
56
|
+
# @return [String] Processed val if possible otherwise return val is it was
|
45
57
|
#
|
46
|
-
def self.method_apply(val, operator, base)
|
58
|
+
def self.method_apply(val, operator, base, module_name, args = nil)
|
59
|
+
lookup_module = (module_name == "") ? base : Module.const_get(module_name)
|
60
|
+
|
47
61
|
case
|
48
62
|
when operator.class.name === "Proc"
|
49
|
-
operator.call(val)
|
50
|
-
when
|
51
|
-
|
52
|
-
when base.respond_to?(operator)
|
53
|
-
base.send(operator, val)
|
63
|
+
operator.call(val, *args)
|
64
|
+
when lookup_module.respond_to?(operator)
|
65
|
+
lookup_module.send(operator, val, *args)
|
66
|
+
when lookup_module != base && base.respond_to?(operator)
|
67
|
+
base.send(operator, val, *args)
|
54
68
|
else
|
55
69
|
val
|
56
70
|
end
|
@@ -72,9 +86,9 @@ module ActiveStripper # Pun intended
|
|
72
86
|
args = args.reject { | field | self.respond_to?(field) }
|
73
87
|
|
74
88
|
# Dynamically generate an anonymous module to be prepended
|
89
|
+
# self is equal to the object where the current method is called
|
75
90
|
mod = Module.new do
|
76
91
|
args.each do | field |
|
77
|
-
|
78
92
|
define_method :"#{field}=" do | val |
|
79
93
|
val = case operator.class.name
|
80
94
|
when "Proc"
|
@@ -82,7 +96,10 @@ module ActiveStripper # Pun intended
|
|
82
96
|
when "Hash", "Array"
|
83
97
|
ActiveStripper.iterable_apply(val, operator, self)
|
84
98
|
when "String", "Symbol"
|
85
|
-
ActiveStripper.method_apply(val,
|
99
|
+
ActiveStripper.method_apply(val,
|
100
|
+
operator,
|
101
|
+
self,
|
102
|
+
"ActiveStripper::Helpers")
|
86
103
|
end
|
87
104
|
|
88
105
|
super(val)
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: active_stripper
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- François TCHENG
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-08
|
11
|
+
date: 2017-09-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -54,7 +54,7 @@ dependencies:
|
|
54
54
|
version: '3.0'
|
55
55
|
description: |2-
|
56
56
|
|
57
|
-
Strip
|
57
|
+
Strip / preprocess selected Active Record / Object attributes by creating custom setter through module prepending.
|
58
58
|
Work with any Object in ruby and still allow custom setter to be defined inside the class.
|
59
59
|
For exemple, can lowercase an email field during attribute setting for ActiveRecord instances
|
60
60
|
instead of having to hook on validation hook on ActiveRecord.
|