ostruct-sanitizer 0.4.1 → 0.5.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/.travis.yml +2 -0
- data/lib/ostruct/sanitizer.rb +41 -16
- data/lib/ostruct/sanitizer/version.rb +1 -1
- data/ostruct-sanitizer.gemspec +1 -0
- metadata +16 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 34d10f9b80fcf1d992ed9f72333b6c40802ad242
|
4
|
+
data.tar.gz: 1d2deb49aa89ab53a36b713410c7668fb12733c0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ff8f62a475c29866967ea30698c737a9f453fd5f07da7ab1391e4b2ffc540859032e723ec6eeae65a165d0e254d4bf6f21e112573e236525096019a3b62fc937
|
7
|
+
data.tar.gz: 3050fb8d113649f92fdff8ac9204b7c33ad4aec5d43620fa491db997e802ac7dd1ec03b498c21954db27ce329e81d641cc7cfbaae78dbf93653c690a0248309a
|
data/.gitignore
CHANGED
data/.travis.yml
CHANGED
data/lib/ostruct/sanitizer.rb
CHANGED
@@ -7,7 +7,7 @@ module OStruct
|
|
7
7
|
#
|
8
8
|
# @example
|
9
9
|
# class Person < OpenStruct
|
10
|
-
# include
|
10
|
+
# include OStruct::Sanitizer
|
11
11
|
#
|
12
12
|
# truncate :name, length: 20
|
13
13
|
# alphanumeric :name
|
@@ -28,32 +28,57 @@ module OStruct
|
|
28
28
|
# Initializes the OpenStruct applying any registered sanitization rules
|
29
29
|
#
|
30
30
|
def initialize(attrs = {})
|
31
|
-
super
|
31
|
+
super nil
|
32
32
|
attrs.each_pair do |field, value|
|
33
|
-
self
|
33
|
+
self[field] = value
|
34
34
|
end
|
35
35
|
end
|
36
36
|
|
37
|
-
#
|
37
|
+
# Creates a setter method for the corresponding field which applies any
|
38
|
+
# existing sanitization rules
|
38
39
|
#
|
39
|
-
# @param [
|
40
|
-
# @
|
40
|
+
# @param [Symbol] method the missing method
|
41
|
+
# @param [Array<Any>] args the method's arguments list
|
41
42
|
#
|
42
|
-
def
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
43
|
+
def method_missing(method, *args)
|
44
|
+
# Give OpenStruct a chance to create getters and setters for the
|
45
|
+
# corresponding field
|
46
|
+
super method, *args
|
47
|
+
|
48
|
+
if field = setter?(method)
|
49
|
+
# override setter logic to apply any existing sanitization rules before
|
50
|
+
# assigning the new value to the field
|
51
|
+
override_setter_for(field) if sanitize?(field)
|
52
|
+
# uses the newly created setter to set the field's value and apply any
|
53
|
+
# existing sanitization rules
|
54
|
+
send(method, args[0])
|
49
55
|
end
|
50
|
-
|
56
|
+
end
|
57
|
+
|
58
|
+
# Set attribute's value via setter so that any existing sanitization rules
|
59
|
+
# may be applied
|
60
|
+
#
|
61
|
+
# @param [Symbol|String] name the attribute's name
|
62
|
+
# @param [Any] value the attribute's value
|
63
|
+
#
|
64
|
+
def []=(name, value)
|
65
|
+
send("#{name}=", value)
|
51
66
|
end
|
52
67
|
|
53
68
|
private
|
54
69
|
|
70
|
+
def setter?(method)
|
71
|
+
method[/.*(?==\z)/m].to_s.to_sym
|
72
|
+
end
|
73
|
+
|
74
|
+
def override_setter_for(field)
|
75
|
+
define_singleton_method("#{field}=") do |value|
|
76
|
+
modifiable[field] = sanitize(field, value)
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
55
80
|
def sanitize(field, value)
|
56
|
-
return value if value.nil?
|
81
|
+
return value if value.nil?
|
57
82
|
self.class.sanitizers[field].reduce(value) do |current_value, sanitizer|
|
58
83
|
sanitizer.call(current_value)
|
59
84
|
end
|
@@ -77,7 +102,7 @@ module OStruct
|
|
77
102
|
def sanitize(*fields, &block)
|
78
103
|
@sanitizers ||= {}
|
79
104
|
fields.each do |field|
|
80
|
-
field_sanitizers = @sanitizers[field] ||= []
|
105
|
+
field_sanitizers = @sanitizers[field.to_sym] ||= []
|
81
106
|
field_sanitizers << block
|
82
107
|
end
|
83
108
|
end
|
data/ostruct-sanitizer.gemspec
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ostruct-sanitizer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.5.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Diego Borges
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2017-01-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -52,6 +52,20 @@ dependencies:
|
|
52
52
|
- - "~>"
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '3.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: byebug
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
55
69
|
description: Provides Rails-like sanitization rules for OpenStruct fields.
|
56
70
|
email:
|
57
71
|
- drborges.cic@gmail.com
|