activerecord-attr-stripper 0.0.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: f38c615421b4d1648505927db79ec0392220dd8e
4
+ data.tar.gz: 67d1e0c74f32a58bacbdc7e29622eaa522ea1f7e
5
+ SHA512:
6
+ metadata.gz: 3ddd5e3bf33a396df3c91cfa93f9111f1a96ba40086b3fc28190caa4b7f90b2b1b674f4b7e8e11e834c2ecb24694c0ee9af007345f1b22af747405febc9321f0
7
+ data.tar.gz: 162ac7a9dfabee427cbec5fdfe5ef87dc408ae5d85e21170b183ca4bdf703cbe5d4cd270b2dde970a7ea71d356d839422a8f08cf2e60d95f170775cb7246dfa2
data/.gitignore ADDED
@@ -0,0 +1,22 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
data/.travis.yml ADDED
@@ -0,0 +1,16 @@
1
+ language: ruby
2
+ before_install:
3
+ - gem install bundler
4
+ gemfile:
5
+ - Gemfile
6
+ rvm:
7
+ - 1.9.3
8
+ - 2.0.0
9
+ - 2.1.1
10
+ - 2.1.2
11
+ - ruby-head
12
+ notifications:
13
+ email:
14
+ - muryoimpl@gmail.com
15
+ script:
16
+ - bundle exec rspec spec
data/Gemfile ADDED
@@ -0,0 +1,11 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in activerecord-attr-stripper.gemspec
4
+ gemspec
5
+
6
+ gem 'database_cleaner'
7
+ gem 'minitest'
8
+ gem 'pry'
9
+ gem 'pry-byebug', platform: [:ruby_20, :ruby_21]
10
+ gem 'rspec', require: false
11
+ gem 'sqlite3', platform: [:ruby, :mswin, :mingw]
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2014 muryoimpl
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
22
+
data/README.md ADDED
@@ -0,0 +1,45 @@
1
+ activerecord-attr-stripper [![Build Status](https://travis-ci.org/muryoimpl/activerecord-attr-stripper.svg?branch=master)](https://travis-ci.org/muryoimpl/activerecord-attr-stripper)
2
+ ==========================
3
+
4
+ A ActiveRecord plugin which removes leading and trailing whitespace(include zenkaku space) of attributes before validation.
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+ ```ruby
10
+ gem 'activerecord-attr-stripper'
11
+ ```
12
+
13
+ And then execute:
14
+ ```ruby
15
+ $ bundle
16
+ ```
17
+ Or install it yourself as:
18
+ ```ruby
19
+ $ gem install activerecord-attr-stripper
20
+ ```
21
+
22
+ ## Usage
23
+
24
+ ```ruby
25
+ class Post < ActiveRecord::Base
26
+ include ActiveRecord::Attr::Stripper
27
+
28
+ strip_attrs :title, :description, blank_to_nil: true
29
+ end
30
+
31
+ post = Post.new(title: '  this is title  ', description: ' ')
32
+ post.valid?
33
+ puts post.title #=> 'this is title'
34
+ puts post.description #=> nil
35
+ ```
36
+
37
+ `blank_to_nil` option is true, attribute's value returns nil if the attribute is '' after removing leading and tailing whitespace.
38
+
39
+ ## Contributing
40
+
41
+ 1. Fork it ( https://github.com/[my-github-username]/activerecord-attr-stripper/fork )
42
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
43
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
44
+ 4. Push to the branch (`git push origin my-new-feature`)
45
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'activerecord/attr/stripper/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "activerecord-attr-stripper"
8
+ spec.version = Activerecord::Attr::Stripper::VERSION
9
+ spec.authors = ["muryoimpl"]
10
+ spec.email = ["muryoimpl@gmail.com"]
11
+ spec.summary = %q{A ActiveRecord plugin which removes leading and trailing whitespace of attributes before validation.}
12
+ spec.description = %q{A ActiveRecord plugin which removes leading and trailing whitespace of attributes before validation.}
13
+ spec.homepage = "https://github.com/muryoimpl/activerecord-attr-stripper"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.required_ruby_version = '>= 1.9.3'
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.6"
24
+ spec.add_development_dependency "rake"
25
+ spec.add_development_dependency "activerecord"
26
+ end
@@ -0,0 +1,2 @@
1
+ # coding: utf-8
2
+ require 'activerecord/attr/stripper/base'
@@ -0,0 +1,42 @@
1
+ # coding: utf-8
2
+ module ActiveRecord
3
+ module Attr
4
+ module Stripper
5
+ class AttrStripper
6
+ class << self
7
+ alias :strip! :new
8
+ end
9
+
10
+ def before_validation(record)
11
+ strip_targets = record.class.class_variable_get :@@strip_targets
12
+
13
+ return if strip_targets.blank?
14
+
15
+ strip_targets.each do |target|
16
+ target.attrs.each do |attr|
17
+ attr_value = record.send(attr)
18
+
19
+ if attr_value.kind_of?(String)
20
+ stripped_value = strip(attr_value, target.opts)
21
+
22
+ record.send("#{attr.to_s}=", stripped_value)
23
+ end
24
+ end
25
+ end
26
+ end
27
+
28
+ private
29
+
30
+ def strip(attr_value, opts)
31
+ stripped_value = attr_value.gsub(/\A(\s| )+|(\s| )+\z/, '')
32
+
33
+ if opts[:blank_to_nil]
34
+ stripped_value.presence
35
+ else
36
+ stripped_value
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ require 'activerecord/attr/stripper/version'
3
+ require 'activerecord/attr/stripper/attr_stripper'
4
+ require 'activerecord/attr/stripper/target'
5
+
6
+ module ActiveRecord
7
+ module Attr
8
+ module Stripper
9
+ extend ActiveSupport::Concern
10
+
11
+ self.included do
12
+ cattr_accessor :strip_targets
13
+
14
+ before_validation ActiveRecord::Attr::Stripper::AttrStripper.strip!
15
+ end
16
+
17
+ module ClassMethods
18
+ def strip_attrs(*attrs)
19
+ return if attrs.blank?
20
+
21
+ self.strip_targets ||= []
22
+ self.strip_targets << Target.new(*attrs)
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,20 @@
1
+ # coding: utf-8
2
+ module ActiveRecord
3
+ module Attr
4
+ module Stripper
5
+ class Target
6
+ attr_reader :attrs, :opts
7
+
8
+ def initialize(*attrs)
9
+ if attrs.last.kind_of?(Hash)
10
+ @attrs = attrs[0...-1]
11
+ @opts = attrs.last
12
+ else
13
+ @attrs = attrs
14
+ @opts = {}
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,8 @@
1
+ # coding: utf-8
2
+ module Activerecord
3
+ module Attr
4
+ module Stripper
5
+ VERSION = "0.0.1"
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,62 @@
1
+ # coding: utf-8
2
+ require 'spec_helper'
3
+ require 'activerecord/attr/stripper'
4
+
5
+ class Post < ActiveRecord::Base
6
+ include ActiveRecord::Attr::Stripper
7
+ end
8
+
9
+ describe ActiveRecord::Attr::Stripper do
10
+ TEST_VALUE = '  test  '
11
+
12
+ let(:post) { Post.new(title: TEST_VALUE, description: TEST_VALUE) }
13
+
14
+ context 'when Post has .strip_attrs(:title)' do
15
+ before do
16
+ Post.class_eval do
17
+ strip_attrs :title
18
+ end
19
+ end
20
+
21
+ context 'without options' do
22
+ context 'when attr includes blank' do
23
+ before do
24
+ post.valid?
25
+ end
26
+
27
+ it { expect(post.title).to eq 'test' }
28
+ it { expect(post.description).to eq TEST_VALUE }
29
+ end
30
+
31
+ context 'when attr includes only blank' do
32
+ before do
33
+ post.title = ' '
34
+ post.valid?
35
+ end
36
+
37
+ it { expect(post.title).to eq '' }
38
+ it { expect(post.description).to eq TEST_VALUE }
39
+ end
40
+ end
41
+
42
+ context 'with options' do
43
+ describe ':blank_to_nil' do
44
+ before do
45
+ Post.class_eval do
46
+ strip_attrs :title, blank_to_nil: true
47
+ end
48
+ end
49
+
50
+ context 'when attr includes only blank' do
51
+ before do
52
+ post.title = '    '
53
+ post.valid?
54
+ end
55
+
56
+ it { expect(post.title).to be_nil }
57
+ it { expect(post.description).to eq TEST_VALUE }
58
+ end
59
+ end
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ require 'active_record'
3
+ require 'database_cleaner'
4
+ require 'pry'
5
+
6
+ ActiveRecord::Base.configurations = { 'test' => { :adapter => 'sqlite3', :database => ':memory:' } }
7
+ ActiveRecord::Base.establish_connection(:test)
8
+
9
+ class CreateTables < ActiveRecord::Migration
10
+ def self.up
11
+ create_table(:posts) {|t| t.string :title; t.text :description; }
12
+ end
13
+ end
14
+
15
+ DatabaseCleaner[:active_record].strategy = :transaction
16
+
17
+ RSpec.configure do |c|
18
+ c.before :all do
19
+ CreateTables.up unless ActiveRecord::Base.connection.table_exists?(:posts)
20
+ end
21
+
22
+ c.before :suite do
23
+ DatabaseCleaner.clean_with :truncation
24
+ end
25
+
26
+ c.after :each do
27
+ DatabaseCleaner.clean
28
+ end
29
+ end
metadata ADDED
@@ -0,0 +1,104 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: activerecord-attr-stripper
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - muryoimpl
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-09-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: activerecord
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: A ActiveRecord plugin which removes leading and trailing whitespace of
56
+ attributes before validation.
57
+ email:
58
+ - muryoimpl@gmail.com
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".gitignore"
64
+ - ".travis.yml"
65
+ - Gemfile
66
+ - LICENSE
67
+ - README.md
68
+ - Rakefile
69
+ - activerecord-attr-stripper.gemspec
70
+ - lib/activerecord/attr/stripper.rb
71
+ - lib/activerecord/attr/stripper/attr_stripper.rb
72
+ - lib/activerecord/attr/stripper/base.rb
73
+ - lib/activerecord/attr/stripper/target.rb
74
+ - lib/activerecord/attr/stripper/version.rb
75
+ - spec/activerecord_attr_strippter_spec.rb
76
+ - spec/spec_helper.rb
77
+ homepage: https://github.com/muryoimpl/activerecord-attr-stripper
78
+ licenses:
79
+ - MIT
80
+ metadata: {}
81
+ post_install_message:
82
+ rdoc_options: []
83
+ require_paths:
84
+ - lib
85
+ required_ruby_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: 1.9.3
90
+ required_rubygems_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ requirements: []
96
+ rubyforge_project:
97
+ rubygems_version: 2.2.2
98
+ signing_key:
99
+ specification_version: 4
100
+ summary: A ActiveRecord plugin which removes leading and trailing whitespace of attributes
101
+ before validation.
102
+ test_files:
103
+ - spec/activerecord_attr_strippter_spec.rb
104
+ - spec/spec_helper.rb