capitalize_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: 3a89cd7c8d70e2337f2f025ad4f0f0807e9c4ed884fbf7a02ad9912ae1f4097d
4
+ data.tar.gz: 2cec25cab661fcacd636133252da6720f54fa726337364b9df1bd99b71d52e87
5
+ SHA512:
6
+ metadata.gz: de90462c0e36b85e600e68982e2ec5cbe602cc3544ac6c805c6c22d102e151ddb67774d0076584fef9cbb11bb30b6c533d32c59fe06100317ed4bb51158bce11
7
+ data.tar.gz: 7cd8809904377811111bbd1ae500c5a7a60ca90672a79ea451b5b99a57a8cd74e792d2b7e2a79c009823e869919b5fc4d389064c6f78bf807b18607554b84704
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,6 @@
1
+ ---
2
+ language: ruby
3
+ cache: bundler
4
+ rvm:
5
+ - 2.7.0
6
+ before_install: gem install bundler -v 2.1.4
@@ -0,0 +1,11 @@
1
+ # Changelog
2
+ All notable changes to this project will be documented in this file.
3
+
4
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
5
+ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
6
+
7
+ ## [Unreleased]
8
+
9
+ ## [[0.1.0]] - 2020-10-21
10
+ ### Added
11
+ - Initial capitalization logic and spec
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in capitalize_attributes.gemspec
4
+ gemspec
5
+
6
+ gem "rake", "~> 12.0"
7
+ gem "rspec", "~> 3.0"
@@ -0,0 +1,49 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ capitalize_attributes (0.1.0)
5
+ activemodel (>= 5.0, < 7.0)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ activemodel (5.2.3)
11
+ activesupport (= 5.2.3)
12
+ activesupport (5.2.3)
13
+ concurrent-ruby (~> 1.0, >= 1.0.2)
14
+ i18n (>= 0.7, < 2)
15
+ minitest (~> 5.1)
16
+ tzinfo (~> 1.1)
17
+ concurrent-ruby (1.1.5)
18
+ diff-lcs (1.3)
19
+ i18n (1.6.0)
20
+ concurrent-ruby (~> 1.0)
21
+ minitest (5.11.3)
22
+ rake (12.3.3)
23
+ rspec (3.8.0)
24
+ rspec-core (~> 3.8.0)
25
+ rspec-expectations (~> 3.8.0)
26
+ rspec-mocks (~> 3.8.0)
27
+ rspec-core (3.8.2)
28
+ rspec-support (~> 3.8.0)
29
+ rspec-expectations (3.8.4)
30
+ diff-lcs (>= 1.2.0, < 2.0)
31
+ rspec-support (~> 3.8.0)
32
+ rspec-mocks (3.8.1)
33
+ diff-lcs (>= 1.2.0, < 2.0)
34
+ rspec-support (~> 3.8.0)
35
+ rspec-support (3.8.2)
36
+ thread_safe (0.3.6)
37
+ tzinfo (1.2.5)
38
+ thread_safe (~> 0.1)
39
+
40
+ PLATFORMS
41
+ ruby
42
+
43
+ DEPENDENCIES
44
+ capitalize_attributes!
45
+ rake (~> 12.0)
46
+ rspec (~> 3.0)
47
+
48
+ BUNDLED WITH
49
+ 2.0.2
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Mark Oveson
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
13
+ all 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
21
+ THE SOFTWARE.
@@ -0,0 +1,60 @@
1
+ # Capitalize Attributes
2
+
3
+ Capitalize Attributes is an ActiveModel extension that can automatically capitalize names and place-names that were entered as all-lowercase or all-uppercase text. This is often the case when a user is asked to provide personal information such as name, state, city, or country in an input form.
4
+
5
+ Capitalize Attributes does not attempt to work magic on mixed-case text. If someone goes to the trouble of using the shift key at some point during input, we assume they know what they are doing. This gem only affects values that are entirely uppercase or entirely lowercase.
6
+
7
+ Capitalize Attributes works with ActiveRecord and ActiveModel models.
8
+
9
+ This gem was inspired by the workhorse `strip_attributes` gem: https://github.com/rmm5t/strip_attributes
10
+
11
+ ## Installation
12
+
13
+ Add this line to your application's Gemfile:
14
+
15
+ ```ruby
16
+ gem 'capitalize_attributes'
17
+ ```
18
+
19
+ And then execute:
20
+
21
+ $ bundle install
22
+
23
+ Or install it yourself as:
24
+
25
+ $ gem install capitalize_attributes
26
+
27
+ ## Usage
28
+
29
+ To use this gem, include the gem and then list the attributes that should be capitalized, like this:
30
+
31
+ ```ruby
32
+ class MyModel < ::ApplicationRecord
33
+ include CapitalizeAttributes
34
+
35
+ capitalize_attributes :first_name, :last_name, :city
36
+ end
37
+
38
+ => record = MyModel.new(first_name: "whisper", last_name: "SHOUT", city: "McCall")
39
+ => record.validate
40
+ => record.first_name
41
+ #> "Whisper"
42
+ => record.last_name
43
+ #> "Shout"
44
+ => record.city
45
+ #> "McCall"
46
+ ```
47
+
48
+ ## Development
49
+
50
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
51
+
52
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
53
+
54
+ ## Contributing
55
+
56
+ Bug reports and pull requests are welcome on GitHub at https://github.com/moveson/capitalize_attributes.
57
+
58
+ ## License
59
+
60
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "capitalize_attributes"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,30 @@
1
+ require_relative 'lib/capitalize_attributes/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "capitalize_attributes"
5
+ spec.version = CapitalizeAttributes::VERSION
6
+ spec.authors = ["Mark Oveson"]
7
+ spec.email = ["mark.oveson@gmail.com"]
8
+
9
+ spec.summary = %q{Automatically capitalizes names and place-names.}
10
+ spec.description = %q{An ActiveModel extension that provides automatic capitalization for name and place-name attributes.}
11
+ spec.homepage = "https://www.github.com/moveson/capitalize_attributes"
12
+ spec.license = "MIT"
13
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.4.0")
14
+
15
+ spec.metadata["homepage_uri"] = spec.homepage
16
+ spec.metadata["source_code_uri"] = spec.homepage
17
+ spec.metadata["changelog_uri"] = "https://github.com/moveson/capitalize_attributes/blob/master/CHANGELOG.md"
18
+
19
+ # Specify which files should be added to the gem when it is released.
20
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
21
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
22
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
23
+ end
24
+ spec.bindir = "exe"
25
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
26
+ spec.require_paths = ["lib"]
27
+
28
+ spec.add_dependency "activemodel", ">= 5.0", "< 7.0"
29
+ spec.add_development_dependency "rspec", "~> 3.9"
30
+ end
@@ -0,0 +1,38 @@
1
+ require "active_model"
2
+ require "capitalize_attributes/selective_capitalizer"
3
+
4
+ module CapitalizeAttributes
5
+ extend ::ActiveSupport::Concern
6
+
7
+ included do
8
+ before_validation :capitalize_record
9
+ class_attribute :capitalizable_attribute_names
10
+ self.capitalizable_attribute_names = []
11
+ end
12
+
13
+ module ClassMethods
14
+ def capitalize_attributes(*attributes)
15
+ attributes.each(&method(:capitalize_attribute))
16
+ end
17
+
18
+ def capitalize_attribute(attribute)
19
+ self.capitalizable_attribute_names << attribute.to_s
20
+ end
21
+ end
22
+
23
+ private
24
+
25
+ def capitalize_record
26
+ capitalizable_attributes = attributes.slice(*self.capitalizable_attribute_names)
27
+
28
+ capitalizable_attributes.each do |attr, value|
29
+ if value.present?
30
+ new_value = ::CapitalizeAttributes::SelectiveCapitalizer.perform(value)
31
+ self.send("#{attr}=", new_value) if new_value != value
32
+ end
33
+ end
34
+
35
+ # Don't cancel later callbacks
36
+ true
37
+ end
38
+ end
@@ -0,0 +1,49 @@
1
+ module CapitalizeAttributes
2
+ module Matchers
3
+
4
+ # RSpec Examples:
5
+ #
6
+ # it { is_expected.to capitalize_attribute(:first_name) }
7
+ # it { is_expected.to capitalize_attributes(:first_name, :last_name) }
8
+ # it { is_expected.not_to capitalize_attribute(:password) }
9
+ # it { is_expected.not_to capitalize_attributes(:password, :encrypted_password) }
10
+ def capitalize_attribute(*attributes)
11
+ CapitalizeAttributeMatcher.new(attributes)
12
+ end
13
+
14
+ alias_method :capitalize_attributes, :capitalize_attribute
15
+
16
+ class CapitalizeAttributeMatcher
17
+ def initialize(attributes)
18
+ @attributes = attributes
19
+ @options = {}
20
+ end
21
+
22
+ def matches?(subject)
23
+ @attributes.all? do |attribute|
24
+ @attribute = attribute
25
+ subject.send("#{@attribute}=", "string")
26
+ subject.validate
27
+ subject.send(@attribute) == "String"
28
+ end
29
+ end
30
+
31
+ def failure_message # RSpec 3.x
32
+ "Expected #{@attribute} to be capitalized, but it was not"
33
+ end
34
+
35
+ alias_method :failure_message_for_should, :failure_message # RSpec 1.2, 2.x, and minitest-matchers
36
+
37
+ def failure_message_when_negated # RSpec 3.x
38
+ "Expected #{@attribute} not to be capitalized, but it was"
39
+ end
40
+
41
+ alias_method :failure_message_for_should_not, :failure_message_when_negated # RSpec 1.2, 2.x, and minitest-matchers
42
+ alias_method :negative_failure_message, :failure_message_when_negated # RSpec 1.1
43
+
44
+ def description
45
+ "capitalize #{@attributes.map { |el| "##{el}" }.to_sentence}"
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,58 @@
1
+ require "active_support/core_ext/string"
2
+
3
+ module CapitalizeAttributes
4
+ class SelectiveCapitalizer
5
+ ABBREVIATION_MAX_LENGTH = 4
6
+ ABBREVIATION_VOWELS = %w(a e i o u y).freeze
7
+ ROMAN_NUMERALS = %w(i ii iii iv v vi vii viii ix x).to_set.freeze
8
+ ALWAYS_DOWNCASED_WORDS = [
9
+ "de",
10
+ "of",
11
+ "on",
12
+ "the",
13
+ ].to_set.freeze
14
+
15
+ def self.perform(value)
16
+ words = value.split
17
+ words.map { |word| capitalize(word) }.join(" ")
18
+ end
19
+
20
+ def self.capitalize(word)
21
+ # Modify the word only if it is all lowercase or all uppercase.
22
+ # This avoids inadvertently capitalizing names intended to have mixed
23
+ # case, like "McDonald".
24
+ #
25
+ # If a word is mixed case, we assume the person entering it
26
+ # intends the case to be as entered.
27
+ return word unless word.downcase == word || word.upcase == word
28
+
29
+ downcased_word = word.downcase
30
+
31
+ case
32
+ when always_downcased?(downcased_word)
33
+ downcased_word
34
+ when abbreviation?(downcased_word)
35
+ word.upcase
36
+ when roman_numeral?(downcased_word)
37
+ word.upcase
38
+ else
39
+ word.titleize
40
+ end
41
+ end
42
+
43
+ def self.abbreviation?(downcased_word)
44
+ return false if downcased_word.length > ABBREVIATION_MAX_LENGTH
45
+
46
+ chars = downcased_word.chars
47
+ (chars - ABBREVIATION_VOWELS) == chars
48
+ end
49
+
50
+ def self.roman_numeral?(downcased_word)
51
+ ROMAN_NUMERALS.include?(downcased_word)
52
+ end
53
+
54
+ def self.always_downcased?(downcased_word)
55
+ ALWAYS_DOWNCASED_WORDS.include?(downcased_word)
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,3 @@
1
+ module CapitalizeAttributes
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,97 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: capitalize_attributes
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Mark Oveson
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-10-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activemodel
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '5.0'
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '7.0'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: '5.0'
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '7.0'
33
+ - !ruby/object:Gem::Dependency
34
+ name: rspec
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '3.9'
40
+ type: :development
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: '3.9'
47
+ description: An ActiveModel extension that provides automatic capitalization for name
48
+ and place-name attributes.
49
+ email:
50
+ - mark.oveson@gmail.com
51
+ executables: []
52
+ extensions: []
53
+ extra_rdoc_files: []
54
+ files:
55
+ - ".gitignore"
56
+ - ".rspec"
57
+ - ".travis.yml"
58
+ - CHANGELOG.md
59
+ - Gemfile
60
+ - Gemfile.lock
61
+ - LICENSE.txt
62
+ - README.md
63
+ - Rakefile
64
+ - bin/console
65
+ - bin/setup
66
+ - capitalize_attributes.gemspec
67
+ - lib/capitalize_attributes.rb
68
+ - lib/capitalize_attributes/matchers.rb
69
+ - lib/capitalize_attributes/selective_capitalizer.rb
70
+ - lib/capitalize_attributes/version.rb
71
+ homepage: https://www.github.com/moveson/capitalize_attributes
72
+ licenses:
73
+ - MIT
74
+ metadata:
75
+ homepage_uri: https://www.github.com/moveson/capitalize_attributes
76
+ source_code_uri: https://www.github.com/moveson/capitalize_attributes
77
+ changelog_uri: https://github.com/moveson/capitalize_attributes/blob/master/CHANGELOG.md
78
+ post_install_message:
79
+ rdoc_options: []
80
+ require_paths:
81
+ - lib
82
+ required_ruby_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: 2.4.0
87
+ required_rubygems_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ requirements: []
93
+ rubygems_version: 3.1.4
94
+ signing_key:
95
+ specification_version: 4
96
+ summary: Automatically capitalizes names and place-names.
97
+ test_files: []