attr_filters 0.2.1 → 0.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.travis.yml +9 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +1 -1
- data/README.md +49 -2
- data/attr_filters.gemspec +2 -2
- data/gemfiles/rails_4.2.10.gemfile +7 -0
- data/gemfiles/rails_5.2.1.gemfile +7 -0
- data/gemfiles/rails_6.0.0.gemfile +7 -0
- data/lib/active_model/validations/filters_validator.rb +16 -0
- data/lib/attr_filters.rb +1 -0
- data/lib/attr_filters/active_model.rb +22 -0
- data/lib/attr_filters/filters_macro.rb +1 -1
- data/lib/attr_filters/utils.rb +4 -0
- data/lib/attr_filters/version.rb +1 -1
- metadata +11 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cf6cbce34f0df0925b70fc57becf4f3a723abf1d568749a3dcf2d8574cdc8d74
|
4
|
+
data.tar.gz: 5acecff50edcffad8a2198116cfea54f6e5bf57b333a3b9bef7d644f1ca46380
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9e7e3c219382d5c5686a43f75c784414e42d3181806ab3450be30e0a38b4758c13923f3bf57df53c1b0bc1b5e97c3eb4555284b448db206bce9b40a81dfbe915
|
7
|
+
data.tar.gz: 778bafab6a4dae472a05fff08321db18bd1cca4dbe88398333ba59cccfe08516cae3f097f0bad2ec065cb61acaac064ba8fef1fa4b3a7b372e9d06128575941c
|
data/.travis.yml
CHANGED
@@ -2,8 +2,17 @@
|
|
2
2
|
sudo: false
|
3
3
|
language: ruby
|
4
4
|
cache: bundler
|
5
|
+
gemfile:
|
6
|
+
- Gemfile
|
7
|
+
- gemfiles/rails_4.2.10.gemfile
|
8
|
+
- gemfiles/rails_5.2.1.gemfile
|
9
|
+
- gemfiles/rails_6.0.0.gemfile
|
5
10
|
rvm:
|
6
11
|
- 2.4
|
7
12
|
- 2.5
|
8
13
|
- 2.6
|
14
|
+
jobs:
|
15
|
+
exclude:
|
16
|
+
- rvm: 2.4
|
17
|
+
gemfile: gemfiles/rails_6.0.0.gemfile
|
9
18
|
before_install: gem install bundler -v 2.0.1
|
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -1,12 +1,17 @@
|
|
1
1
|
# AttrFilters
|
2
2
|
|
3
3
|
[](https://travis-ci.com/Syndicode/attr-filters)
|
4
|
+
[](https://badge.fury.io/rb/attr_filters)
|
4
5
|
|
5
|
-
Light weight gem for filtering PORO attributes with zero dependencies.
|
6
|
+
Light weight gem for filtering PORO (Plain Old Ruby Objects) attributes with zero dependencies.
|
6
7
|
|
7
8
|
## Description
|
8
9
|
|
9
|
-
AttrFilters brings simple DSL for adding filters to your PORO attributes
|
10
|
+
AttrFilters brings simple DSL for adding filters to your PORO attributes.<br>
|
11
|
+
Simple way to integration with Rails validation.
|
12
|
+
|
13
|
+
## Requirements
|
14
|
+
- Ruby >= 2.4
|
10
15
|
|
11
16
|
## Installation
|
12
17
|
|
@@ -59,6 +64,48 @@ form.last_name # => "Dou"
|
|
59
64
|
form.zip # => "12345"
|
60
65
|
```
|
61
66
|
|
67
|
+
## Integration with Rails
|
68
|
+
### Requirements
|
69
|
+
- ActiveModel >= 4.2.0
|
70
|
+
|
71
|
+
For Rails integration you should include `AttrFilters::ActiveModel`.<br>
|
72
|
+
After that you can add filters using `validates` method.
|
73
|
+
|
74
|
+
```ruby
|
75
|
+
class SingupForm
|
76
|
+
include ActiveModel::Model
|
77
|
+
include AttrFilters::ActiveModel
|
78
|
+
|
79
|
+
attr_accessor :user_name
|
80
|
+
|
81
|
+
validates :user_name, presence: true, filters: { trim: true, squeeze: true }
|
82
|
+
end
|
83
|
+
|
84
|
+
form = SingupForm.new(user_name: " Mike Dou ")
|
85
|
+
form.filter!
|
86
|
+
form.user_name # => "Mike Dou"
|
87
|
+
```
|
88
|
+
|
89
|
+
Also filters can be run automatically before validation. Require `ActiveModel::Validations::Callbacks`
|
90
|
+
|
91
|
+
```ruby
|
92
|
+
class SingupForm
|
93
|
+
include ActiveModel::Model
|
94
|
+
include ActiveModel::Validations::Callbacks
|
95
|
+
include AttrFilters::ActiveModel
|
96
|
+
|
97
|
+
attr_accessor :user_name, :zip
|
98
|
+
|
99
|
+
validates :user_name, presence: true, filters: { trim: true, squeeze: true }
|
100
|
+
filters :zip, trim: true, numbers_only: true
|
101
|
+
end
|
102
|
+
|
103
|
+
form = SingupForm.new(user_name: " Mike Dou ", zip: "12345abc")
|
104
|
+
form.valid?
|
105
|
+
form.user_name # => "Mike Dou"
|
106
|
+
form.zip # => "12345"
|
107
|
+
```
|
108
|
+
|
62
109
|
## Available Filters
|
63
110
|
|
64
111
|
- `trim` - removes leading and trailing whitespaces
|
data/attr_filters.gemspec
CHANGED
@@ -10,8 +10,8 @@ Gem::Specification.new do |spec|
|
|
10
10
|
spec.authors = ["Anton Holovko", "Syndicode LLC"]
|
11
11
|
spec.email = ["anton.holovko.b@gmail.com", "info@syndicode.com"]
|
12
12
|
|
13
|
-
spec.summary = "Light weight gem for filtering PORO attributes with zero dependencies."
|
14
|
-
spec.description = "Light weight gem for filtering PORO attributes with zero dependencies."
|
13
|
+
spec.summary = "Light weight gem for filtering PORO (Plain Old Ruby Objects) attributes with zero dependencies."
|
14
|
+
spec.description = "Light weight gem for filtering PORO (Plain Old Ruby Objects) attributes with zero dependencies."
|
15
15
|
spec.homepage = "https://github.com/Syndicode/attr-filters"
|
16
16
|
spec.license = "MIT"
|
17
17
|
|
@@ -0,0 +1,16 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ActiveModel
|
4
|
+
module Validations
|
5
|
+
class FiltersValidator < ActiveModel::EachValidator
|
6
|
+
def initialize(options)
|
7
|
+
filters = options.slice(*AttrFilters::Filters::LIST.keys)
|
8
|
+
model_class = options[:class]
|
9
|
+
model_class.filters(*options[:attributes], filters)
|
10
|
+
super(options)
|
11
|
+
end
|
12
|
+
|
13
|
+
def validate_each(_record, _attribute, _value); end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
data/lib/attr_filters.rb
CHANGED
@@ -10,6 +10,7 @@ module AttrFilters
|
|
10
10
|
autoload :FiltersMacro, "attr_filters/filters_macro"
|
11
11
|
autoload :InstanceMethods, "attr_filters/instance_methods"
|
12
12
|
autoload :Filters, "attr_filters/filters"
|
13
|
+
autoload :ActiveModel, "attr_filters/active_model"
|
13
14
|
|
14
15
|
def self.included(base)
|
15
16
|
base.extend(FiltersMacro)
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module AttrFilters
|
4
|
+
module ActiveModel
|
5
|
+
if AttrFilters::Utils.satisfied_spec?("activemodel")
|
6
|
+
require "active_model"
|
7
|
+
require "active_model/validations/filters_validator"
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.included(base)
|
11
|
+
base.include AttrFilters
|
12
|
+
base.extend Macro
|
13
|
+
base.init
|
14
|
+
end
|
15
|
+
|
16
|
+
module Macro
|
17
|
+
def init
|
18
|
+
before_validation :filter! if respond_to?(:before_validation)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
data/lib/attr_filters/utils.rb
CHANGED
data/lib/attr_filters/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: attr_filters
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Anton Holovko
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: exe
|
11
11
|
cert_chain: []
|
12
|
-
date: 2019-10-
|
12
|
+
date: 2019-10-26 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|
@@ -67,7 +67,8 @@ dependencies:
|
|
67
67
|
- - "~>"
|
68
68
|
- !ruby/object:Gem::Version
|
69
69
|
version: 0.75.0
|
70
|
-
description: Light weight gem for filtering PORO
|
70
|
+
description: Light weight gem for filtering PORO (Plain Old Ruby Objects) attributes
|
71
|
+
with zero dependencies.
|
71
72
|
email:
|
72
73
|
- anton.holovko.b@gmail.com
|
73
74
|
- info@syndicode.com
|
@@ -89,7 +90,12 @@ files:
|
|
89
90
|
- bin/console
|
90
91
|
- bin/rubocop
|
91
92
|
- bin/setup
|
93
|
+
- gemfiles/rails_4.2.10.gemfile
|
94
|
+
- gemfiles/rails_5.2.1.gemfile
|
95
|
+
- gemfiles/rails_6.0.0.gemfile
|
96
|
+
- lib/active_model/validations/filters_validator.rb
|
92
97
|
- lib/attr_filters.rb
|
98
|
+
- lib/attr_filters/active_model.rb
|
93
99
|
- lib/attr_filters/filters.rb
|
94
100
|
- lib/attr_filters/filters/base.rb
|
95
101
|
- lib/attr_filters/filters/capitalize.rb
|
@@ -129,5 +135,6 @@ rubyforge_project:
|
|
129
135
|
rubygems_version: 2.7.6
|
130
136
|
signing_key:
|
131
137
|
specification_version: 4
|
132
|
-
summary: Light weight gem for filtering PORO attributes with
|
138
|
+
summary: Light weight gem for filtering PORO (Plain Old Ruby Objects) attributes with
|
139
|
+
zero dependencies.
|
133
140
|
test_files: []
|