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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 394b3fad1a9d90995cb53fe82963aac941605eff
4
- data.tar.gz: 0af510883d0da26ff1e1ce4d86143e38e3452a55
3
+ metadata.gz: 34d10f9b80fcf1d992ed9f72333b6c40802ad242
4
+ data.tar.gz: 1d2deb49aa89ab53a36b713410c7668fb12733c0
5
5
  SHA512:
6
- metadata.gz: 41dfe532cbcdc441b3da61b127568edd5d6dfd6817f281482f51a36e2f0ef0ee89676c1c78425d5caba561d73fd4b135ab3e7cd648cbd26d5ebd774faf817e19
7
- data.tar.gz: 504c8ddc4d3a9e53775fdbbc6bd828580f4911fcfbb7ec54a49888663560bbb012fc57be0b56265f40986f9878a6f343e5c6894746dc94512fde50a42032fc78
6
+ metadata.gz: ff8f62a475c29866967ea30698c737a9f453fd5f07da7ab1391e4b2ffc540859032e723ec6eeae65a165d0e254d4bf6f21e112573e236525096019a3b62fc937
7
+ data.tar.gz: 3050fb8d113649f92fdff8ac9204b7c33ad4aec5d43620fa491db997e802ac7dd1ec03b498c21954db27ce329e81d641cc7cfbaae78dbf93653c690a0248309a
data/.gitignore CHANGED
@@ -7,3 +7,4 @@
7
7
  /pkg/
8
8
  /spec/reports/
9
9
  /tmp/
10
+ .byebug_history
@@ -2,4 +2,6 @@ sudo: false
2
2
  language: ruby
3
3
  rvm:
4
4
  - 2.2.4
5
+ - 2.3.3
6
+ - ruby-head
5
7
  before_install: gem install bundler -v 1.13.6
@@ -7,7 +7,7 @@ module OStruct
7
7
  #
8
8
  # @example
9
9
  # class Person < OpenStruct
10
- # include WellsFargoRetail::Sanitizer
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.send("#{field}=", value)
33
+ self[field] = value
34
34
  end
35
35
  end
36
36
 
37
- # Overrides ostruct member definition applying sanitization rules when needed
37
+ # Creates a setter method for the corresponding field which applies any
38
+ # existing sanitization rules
38
39
  #
39
- # @param [#to_sym] field the name of the field being defined
40
- # @return [Symbol] the name of the defined field
40
+ # @param [Symbol] method the missing method
41
+ # @param [Array<Any>] args the method's arguments list
41
42
  #
42
- def new_ostruct_member(field)
43
- field = field.to_sym
44
- unless respond_to?(field)
45
- define_singleton_method(field) { modifiable[field] }
46
- define_singleton_method("#{field}=") do |value|
47
- modifiable[field] = sanitize(field, value)
48
- end
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
- field
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? || !sanitize?(field)
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
@@ -1,5 +1,5 @@
1
1
  module OStruct
2
2
  module Sanitizer
3
- VERSION = "0.4.1"
3
+ VERSION = "0.5.1"
4
4
  end
5
5
  end
@@ -24,4 +24,5 @@ Gem::Specification.new do |spec|
24
24
  spec.add_development_dependency "bundler", "~> 1.13"
25
25
  spec.add_development_dependency "rake", "~> 10.0"
26
26
  spec.add_development_dependency "rspec", "~> 3.0"
27
+ spec.add_development_dependency "byebug"
27
28
  end
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.1
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: 2016-12-21 00:00:00.000000000 Z
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