active_stripper 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: cb13381584a43b9d0d5cd2a1c09d2483b2189cce
4
+ data.tar.gz: cdd4e9b489b8d72623d50581215bb63f38336991
5
+ SHA512:
6
+ metadata.gz: ed21f7847a0f5ed0609c363970c1cdf89e23357cabb5dc72f66acaf898e93fcb6ca69badee626735734e2bb3e5a32a15494bf3f90f6eb69953330b4193788b29
7
+ data.tar.gz: 69151cf3a0670d8b8a8e6aab8e6e0d498ba264f825eb9c296a7897ca45ebfeeb2d8a38cd701093f6fe0f14a1091f2a6752f61350207313e691249c8215c76cb3
@@ -0,0 +1,94 @@
1
+ require "active_stripper/version"
2
+ require "active_stripper/helpers"
3
+
4
+ module ActiveStripper # Pun intended
5
+
6
+ def self.included(base)
7
+ base.extend(ClassMethods)
8
+ end
9
+
10
+ #
11
+ # Iterate over `iterable_operators` and apply each of them on `val`
12
+ #
13
+ # @param [String] val String to manipulate
14
+ # @param [Hash/Array] iterable_operators Methods details to use on `val`
15
+ # @param [Object] base Instance including the current module
16
+ #
17
+ # @return [String] Modified String
18
+ #
19
+ def self.iterable_apply(val, iterable_operators, base)
20
+ iterable_operators.each do | operator_name, operator_args |
21
+
22
+ val = case
23
+ when !operator_args
24
+ ActiveStripper.method_apply(val, operator_name, base)
25
+ when operator_args[:module]
26
+ Module.const_get(operator_args[:module])
27
+ .send(operator_name, val)
28
+ end
29
+ end
30
+
31
+ return val
32
+ end
33
+
34
+ #
35
+ # Look up for method `operator` in ActiveStripper::Helpers and current
36
+ # class and call it
37
+ #
38
+ # @param [String] val Value to modify
39
+ # @param [String/Symbol] operator Method name to execute
40
+ # @param [Object] base Instance including the current module
41
+ #
42
+ # @return [String] Modifies value
43
+ #
44
+ def self.method_apply(val, operator, base)
45
+ case
46
+ when operator.class.name === "Proc"
47
+ operator.call(val)
48
+ when ActiveStripper::Helpers.respond_to?(operator)
49
+ ActiveStripper::Helpers.send(operator, val)
50
+ when base.respond_to?(operator)
51
+ base.send(operator, val)
52
+ else
53
+ val
54
+ end
55
+ end
56
+
57
+ module ClassMethods
58
+
59
+ #
60
+ # Helper to use in your Class
61
+ #
62
+ # @param [Array<Any>] *args List of attributes to use for setter generation
63
+ # Length must be > 2,
64
+ # last argument is the preprocessor to run on value
65
+ #
66
+ def strip_value_from(*args)
67
+ raise if args.count < 2
68
+ operator = args.pop
69
+
70
+ args = args.reject { | field | self.respond_to?(field) }
71
+
72
+ # Dynamically generate an anonymous module to be prepended
73
+ mod = Module.new do
74
+ args.each do | field |
75
+
76
+ define_method :"#{field}=" do | val |
77
+ val = case operator.class.name
78
+ when "Proc"
79
+ operator.call(val)
80
+ when "Hash", "Array"
81
+ ActiveStripper.iterable_apply(val, operator, self)
82
+ when "String", "Symbol"
83
+ ActiveStripper.method_apply(val, operator, self)
84
+ end
85
+
86
+ super(val)
87
+ end
88
+ end
89
+ end
90
+
91
+ prepend mod
92
+ end
93
+ end
94
+ end
@@ -0,0 +1,67 @@
1
+ module ActiveStripper
2
+ module Helpers
3
+ class << self
4
+
5
+ #
6
+ # Apply String#strip to value
7
+ #
8
+ # @param [String] val String to strip
9
+ #
10
+ # @return [String] Stripped value
11
+ #
12
+ def white_space_stripper(val)
13
+ return if !val
14
+ return val.strip
15
+ end
16
+
17
+ #
18
+ # Replace double spaces with single space
19
+ #
20
+ # @param [String] val String to clean up
21
+ #
22
+ # @return [String] Cleaned up string
23
+ #
24
+ def multiple_space_stripper(val)
25
+ return if !val
26
+ return val.gsub(/\s+/, " ")
27
+ end
28
+
29
+ #
30
+ # Remove all trailing spaces at the end of the string
31
+ #
32
+ # @param [String] val String to clean up
33
+ #
34
+ # @return [String] Cleaned up string
35
+ #
36
+ def end_space_stripper(val)
37
+ return if !val
38
+ return val.gsub(/\s+$/, "")
39
+ end
40
+
41
+ #
42
+ # Remove all trailong spaces at the beginning of the string
43
+ #
44
+ # @param [String] val String to clean up
45
+ #
46
+ # @return [String] Cleaned up string
47
+ #
48
+ def start_space_stripper(val)
49
+ return if !val
50
+ return val.gsub(/^\s+/, "")
51
+ end
52
+
53
+ #
54
+ # Lower case the entire string through String#downcase
55
+ #
56
+ # @param [String] val String to lower
57
+ #
58
+ # @return [String] Lowered case string
59
+ #
60
+ def to_lower_stripper(val)
61
+ return if !val
62
+ return val.downcase
63
+ end
64
+
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,3 @@
1
+ module ActiveStripper
2
+ VERSION = "0.1.1"
3
+ end
metadata ADDED
@@ -0,0 +1,95 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: active_stripper
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - François TCHENG
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-08-23 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.11'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.11'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description: |2-
56
+
57
+ Strip or preprocess Active Record / Object attributes by creating custom setter through module prepending.
58
+ Work with any Object in ruby and still allow custom setter to be defined inside the class.
59
+ For exemple, can lowercase an email field during attribute setting for ActiveRecord instances
60
+ instead of having to hook on validation hook on ActiveRecord.
61
+ email:
62
+ - tcheng.f@gmail.com
63
+ executables: []
64
+ extensions: []
65
+ extra_rdoc_files: []
66
+ files:
67
+ - "./lib/active_stripper.rb"
68
+ - "./lib/active_stripper/helpers.rb"
69
+ - "./lib/active_stripper/version.rb"
70
+ homepage: https://github.com/nekogami/active_stripper
71
+ licenses:
72
+ - MIT
73
+ metadata: {}
74
+ post_install_message:
75
+ rdoc_options: []
76
+ require_paths:
77
+ - lib
78
+ required_ruby_version: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '2.0'
83
+ required_rubygems_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ requirements: []
89
+ rubyforge_project:
90
+ rubygems_version: 2.4.5.1
91
+ signing_key:
92
+ specification_version: 4
93
+ summary: Strip or preprocess Active Record / Object attributes with prepended setter
94
+ test_files: []
95
+ has_rdoc: