ostruct-sanitizer 0.3.1 → 0.6.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
- SHA1:
3
- metadata.gz: c2195eea12fb39af3afff6c4b6fe806ec04bca30
4
- data.tar.gz: 76c44ec8ff0b923e93adbc976a59a6683ff3892a
2
+ SHA256:
3
+ metadata.gz: ec72d23523602ba268e7d80512e70dbf9b0009832ae82183ec543318113bad65
4
+ data.tar.gz: '049578c417a53dc08f904c186f5fd394692a9717f2e78441df7f65ef7192c763'
5
5
  SHA512:
6
- metadata.gz: c66bb4d5415a969ee8e986c34b9363ef1971fa1e5de273617069dd69fc10384ba78567a56301bdd392fa8311404c24c8079840372174e9699dc30bad85589b67
7
- data.tar.gz: 76b3e51b2ede7292b4e065c4125c0c2ff990650445297d705690071e19c4e59b89ee82db8748fd1119bf43fbd493305cec83b6beac90c6c5bdb1828897e28e37
6
+ metadata.gz: 42000f20cab9e7cf22da5138392f6c9228ed5482ecb836f99ff27053ad885ca267660d9b240fac18d93900641976ef3f33578df326c2f5eeb2169ab2cb0e0de1
7
+ data.tar.gz: 834702166f3e337f6c34b0c975649c4cf2b5e35e4645649f0ed254f1393a423b122db10eb7131f9a10cd6ba22a46bd084a9305594bd1303f4a053f1dceaafc2b
@@ -0,0 +1,24 @@
1
+ name: CI
2
+
3
+ on: push
4
+
5
+ jobs:
6
+ test:
7
+ name: Tests
8
+ runs-on: ubuntu-latest
9
+ strategy:
10
+ fail-fast: false
11
+ matrix:
12
+ ruby:
13
+ - "2.5"
14
+ - "2.6"
15
+ - "2.7"
16
+ steps:
17
+ - uses: actions/checkout@v2
18
+ - uses: ruby/setup-ruby@v1
19
+ with:
20
+ ruby-version: ${{ matrix.ruby }}
21
+ bundler: "2"
22
+ bundler-cache: true
23
+ - name: Run tests
24
+ run: bundle exec rake spec
@@ -0,0 +1,20 @@
1
+ name: Publish Gem
2
+
3
+ on:
4
+ push:
5
+ branches:
6
+ - main
7
+ tags:
8
+ - v*
9
+ jobs:
10
+ build:
11
+ runs-on: ubuntu-latest
12
+ steps:
13
+ - uses: actions/checkout@v1
14
+ - name: Release Gem
15
+ uses: cadwallion/publish-rubygems-action@master
16
+ if: contains(github.ref, 'refs/tags/v')
17
+ env:
18
+ GITHUB_TOKEN: ${{ secrets.github_token }}
19
+ RUBYGEMS_API_KEY: ${{ secrets.rubygems_api_key }}
20
+ RELEASE_COMMAND: rake release
data/.gitignore CHANGED
@@ -7,3 +7,4 @@
7
7
  /pkg/
8
8
  /spec/reports/
9
9
  /tmp/
10
+ .byebug_history
data/.travis.yml CHANGED
@@ -2,4 +2,7 @@ sudo: false
2
2
  language: ruby
3
3
  rvm:
4
4
  - 2.2.4
5
- before_install: gem install bundler -v 1.13.6
5
+ - 2.3.3
6
+ - 2.7.3
7
+ - ruby-head
8
+ before_install: gem install bundler -v 2.2.25
data/README.md CHANGED
@@ -34,7 +34,7 @@ class User < OpenStruct
34
34
  include OStruct::Sanitizer
35
35
 
36
36
  truncate :first_name, :last_name, length: 10
37
- drop_punctuation :city, :country
37
+ alphanumeric :city, :country
38
38
  strip :email, :phone
39
39
 
40
40
  sanitize :age do |value|
@@ -1,5 +1,5 @@
1
1
  module OStruct
2
2
  module Sanitizer
3
- VERSION = "0.3.1"
3
+ VERSION = "0.6.1"
4
4
  end
5
5
  end
@@ -7,10 +7,10 @@ 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
- # drop_punctuation :name
13
+ # alphanumeric :name
14
14
  # sanitize :middle_name do |value|
15
15
  # # Perform a more complex sanitization process
16
16
  # end
@@ -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
@@ -94,12 +119,12 @@ module OStruct
94
119
  strip(*fields) if strip_whitespaces
95
120
  end
96
121
 
97
- # Drops any punctuation character from the field's value
122
+ # Remove any non-alphanumeric character from the field's value
98
123
  #
99
124
  # @param [Array<Symbol>] a list of field names to be sanitized
100
125
  #
101
- def drop_punctuation(*fields)
102
- sanitize(*fields) { |value| value.gsub(/[^\w\s]/, '') }
126
+ def alphanumeric(*fields)
127
+ sanitize(*fields) { |value| value.gsub(/[^A-Za-z0-9\s]/, '') }
103
128
  end
104
129
 
105
130
  # Strips out leading and trailing spaces from the values of the given fields
@@ -21,7 +21,10 @@ Gem::Specification.new do |spec|
21
21
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
22
  spec.require_paths = ["lib"]
23
23
 
24
- spec.add_development_dependency "bundler", "~> 1.13"
24
+ spec.required_ruby_version = '>= 2.5'
25
+
26
+ spec.add_development_dependency "bundler", "~> 2.2.11"
25
27
  spec.add_development_dependency "rake", "~> 10.0"
26
28
  spec.add_development_dependency "rspec", "~> 3.0"
29
+ spec.add_development_dependency "byebug"
27
30
  end
data/test_all.sh ADDED
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env bash
2
+
3
+ set -e
4
+
5
+ rubies=("ruby-2.5.9" "ruby-2.6.8" "ruby-2.7.4")
6
+ for i in "${rubies[@]}"
7
+ do
8
+ echo "====================================================="
9
+ echo "$i: Start Test"
10
+ echo "====================================================="
11
+ rvm $i exec bundle
12
+ rvm $i exec bundle exec rake
13
+ echo "====================================================="
14
+ echo "$i: End Test"
15
+ echo "====================================================="
16
+ done
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.3.1
4
+ version: 0.6.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-20 00:00:00.000000000 Z
11
+ date: 2021-08-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '1.13'
19
+ version: 2.2.11
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '1.13'
26
+ version: 2.2.11
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rake
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -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
@@ -59,6 +73,8 @@ executables: []
59
73
  extensions: []
60
74
  extra_rdoc_files: []
61
75
  files:
76
+ - ".github/workflows/ci.yml"
77
+ - ".github/workflows/release.yml"
62
78
  - ".gitignore"
63
79
  - ".rspec"
64
80
  - ".travis.yml"
@@ -72,6 +88,7 @@ files:
72
88
  - lib/ostruct/sanitizer.rb
73
89
  - lib/ostruct/sanitizer/version.rb
74
90
  - ostruct-sanitizer.gemspec
91
+ - test_all.sh
75
92
  homepage: https://github.com/powerhome/ostruct-sanitizer
76
93
  licenses:
77
94
  - MIT
@@ -84,15 +101,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
84
101
  requirements:
85
102
  - - ">="
86
103
  - !ruby/object:Gem::Version
87
- version: '0'
104
+ version: '2.5'
88
105
  required_rubygems_version: !ruby/object:Gem::Requirement
89
106
  requirements:
90
107
  - - ">="
91
108
  - !ruby/object:Gem::Version
92
109
  version: '0'
93
110
  requirements: []
94
- rubyforge_project:
95
- rubygems_version: 2.4.8
111
+ rubygems_version: 3.0.3
96
112
  signing_key:
97
113
  specification_version: 4
98
114
  summary: Provides Rails-like sanitization rules for OpenStruct fields.