attr_filters 0.2.1 → 0.3.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
  SHA256:
3
- metadata.gz: fce9636df26018c66d0d278ec96421d9a28988d187ae59ce65c412930d86dca4
4
- data.tar.gz: acf1f11c718874e07c95a5d5dfe4332110fabe90936c8a38b2781afaec10d79d
3
+ metadata.gz: cf6cbce34f0df0925b70fc57becf4f3a723abf1d568749a3dcf2d8574cdc8d74
4
+ data.tar.gz: 5acecff50edcffad8a2198116cfea54f6e5bf57b333a3b9bef7d644f1ca46380
5
5
  SHA512:
6
- metadata.gz: f219ec091b56acff2c9b5b69e06f6a0c5d04eb448373ff08607f853091f21be4d861a5348dbd74a5f90efd7299452eba1ae2ce448c26d72f56b968a46617b574
7
- data.tar.gz: 773f5d322b9de0dc55a8289ab695424b11833bf4b6e4d6f8431adbbf939ebe448652da8f3131ad06e5887c9aa7f7ac4498ea0c10acfd581d054a6626a834f724
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
@@ -2,5 +2,9 @@
2
2
 
3
3
  source "https://rubygems.org"
4
4
 
5
+ # gem "activemodel", "4.2.10"
6
+ # gem "activemodel", "5.2.1"
7
+ # gem "activemodel", "6.0.0"
8
+
5
9
  # Specify your gem's dependencies in attr_filters.gemspec
6
10
  gemspec
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- attr_filters (0.2.0)
4
+ attr_filters (0.3.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -1,12 +1,17 @@
1
1
  # AttrFilters
2
2
 
3
3
  [![Build Status](https://travis-ci.com/Syndicode/attr-filters.svg?branch=master)](https://travis-ci.com/Syndicode/attr-filters)
4
+ [![Gem Version](https://badge.fury.io/rb/attr_filters.svg)](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,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "activemodel", "4.2.10"
6
+
7
+ gemspec path: "../"
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "activemodel", "5.2.1"
6
+
7
+ gemspec path: "../"
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "activemodel", "6.0.0"
6
+
7
+ gemspec path: "../"
@@ -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
@@ -14,7 +14,7 @@ module AttrFilters
14
14
  end
15
15
 
16
16
  def _registered_filters
17
- @_registered_filters
17
+ @_registered_filters || {}
18
18
  end
19
19
 
20
20
  private
@@ -7,5 +7,9 @@ module AttrFilters
7
7
  options = last.is_a?(::Hash) ? last : {}
8
8
  [attrs[0..-2], options]
9
9
  end
10
+
11
+ def self.satisfied_spec?(spec_name)
12
+ Gem.loaded_specs.key?(spec_name)
13
+ end
10
14
  end
11
15
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module AttrFilters
4
- VERSION = "0.2.1"
4
+ VERSION = "0.3.1"
5
5
  end
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.2.1
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-24 00:00:00.000000000 Z
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 attributes with zero dependencies.
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 zero dependencies.
138
+ summary: Light weight gem for filtering PORO (Plain Old Ruby Objects) attributes with
139
+ zero dependencies.
133
140
  test_files: []