auto_format_attributes 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 642fc614e0853c02f78be04ce56ce28f163e653d4aab87fcc3af97dc2d5217c9
4
+ data.tar.gz: 1e25982bc65232a1a6715fec1de25d429a9659dd0976fd7c845e56fd6e7822da
5
+ SHA512:
6
+ metadata.gz: 9f9404754da7ab28ec0af6714de3ef602e5cb7f0f6600bdd7e876997b3781afe8014bbd3aa38722ccb88a177f8dc22f2c0fd06f5443845224e32c7523bb7b4ed
7
+ data.tar.gz: 0ca6eb471d65ce0b4a190fd8721370e1c69ca26fa37bea4307a8b5b9eed47d31741e9dd7b56ccf9f94b1e30872a1e1dd3b7ca5530f6f0805b108e600d537fd1d
@@ -0,0 +1,39 @@
1
+ # AutoFormatAttributes
2
+ AutoFormatAttributes helps to format string attributes from ActiveRecord or ActiveModel.
3
+
4
+ It works by adding a before_validation hook to the record.
5
+ ## Usage
6
+
7
+ ```
8
+ class User < ActiveRecord::Base
9
+
10
+ # the last name will be in UPPERCASE
11
+ auto_upcase_attributes :last_name
12
+
13
+ auto_capitalize_attributes :name, :first_name
14
+
15
+ auto_downcase_attributes :profile_url
16
+
17
+ auto_titleize_attributes :title
18
+ end
19
+ ```
20
+
21
+ ## Installation
22
+ Add this line to your application's Gemfile:
23
+
24
+ ```ruby
25
+ gem 'auto_format_attributes'
26
+ ```
27
+
28
+ And then execute:
29
+ ```bash
30
+ $ bundle
31
+ ```
32
+
33
+ Or install it yourself as:
34
+ ```bash
35
+ $ gem install auto_format_attributes
36
+ ```
37
+
38
+ ## License
39
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,27 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'AutoFormatAttributes'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.md')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+ require 'bundler/gem_tasks'
18
+
19
+ require 'rake/testtask'
20
+
21
+ Rake::TestTask.new(:test) do |t|
22
+ t.libs << 'test'
23
+ t.pattern = 'test/**/*_test.rb'
24
+ t.verbose = false
25
+ end
26
+
27
+ task default: :test
@@ -0,0 +1,36 @@
1
+ require "auto_format_attributes/version"
2
+
3
+ module AutoFormatAttributes
4
+ def auto_titleize_attributes(*attributes)
5
+ auto_modify_attributes(attributes, :titleize)
6
+ end
7
+
8
+ def auto_upcase_attributes(*attributes)
9
+ auto_modify_attributes(attributes, :upcase)
10
+ end
11
+
12
+ def auto_downcase_attributes(*attributes)
13
+ auto_modify_attributes(attributes, :downcase)
14
+ end
15
+
16
+ def auto_capitalize_attributes(*attributes)
17
+ auto_modify_attributes(attributes, :capitalize)
18
+ end
19
+
20
+ private
21
+
22
+ def auto_modify_attributes(attributes, method)
23
+ attributes.each do |attribute|
24
+ before_validation do |record|
25
+ if record[attribute].respond_to? method
26
+ record[attribute] = record[attribute].method(method).call
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
32
+
33
+ #ActiveRecord::Base.send(:extend, AutoStripAttributes) if defined? ActiveRecord
34
+ ActiveSupport.on_load(:active_record) do
35
+ extend AutoFormatAttributes
36
+ end
@@ -0,0 +1,3 @@
1
+ module AutoFormatAttributes
2
+ VERSION = '0.1.0'
3
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :auto_format_attributes do
3
+ # # Task goes here
4
+ # end
metadata ADDED
@@ -0,0 +1,63 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: auto_format_attributes
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - youtous
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-05-18 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activerecord
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '4.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '4.0'
27
+ description: Help to format strings in Rails before validation of the Model.
28
+ email:
29
+ - contact@youtous.me
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - README.md
35
+ - Rakefile
36
+ - lib/auto_format_attributes.rb
37
+ - lib/auto_format_attributes/version.rb
38
+ - lib/tasks/auto_format_attributes_tasks.rake
39
+ homepage: https://youtous.me
40
+ licenses:
41
+ - MIT
42
+ metadata: {}
43
+ post_install_message:
44
+ rdoc_options: []
45
+ require_paths:
46
+ - lib
47
+ required_ruby_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ required_rubygems_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: '0'
57
+ requirements: []
58
+ rubyforge_project:
59
+ rubygems_version: 2.7.6
60
+ signing_key:
61
+ specification_version: 4
62
+ summary: AutoFormatAttributes Rails model.
63
+ test_files: []