attribute_extras 0.1.4 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: b20d50f3986dfb10a5e3c8d5afcb9c7b6f3acd3c4ed8ec77e7f2f0bdd4cb6b41
4
+ data.tar.gz: 7f05f0982053f65ed2bc96993a2e50d99c7f53257e8d941e5a5adae735cd90dc
5
+ SHA512:
6
+ metadata.gz: 52c7e755eab89794cb6e8fd424ad390534a02c26fd4082a896fe93c42f6c0b107b2f493b253b09d29df0bcb71c7ab5afa4cdf7c44cc12129928322b11e586d53
7
+ data.tar.gz: c17ab44ea5736d792e6288f14d9bf014ec32889fabbdefee0b503ff9e29ce1f7cd0dba95cfa452f397ff3014056687a540388163e913b725c7a0c842e5f6b115
@@ -0,0 +1,6 @@
1
+ version: 2
2
+ updates:
3
+ - package-ecosystem: "bundler"
4
+ directory: "/"
5
+ schedule:
6
+ interval: "daily"
@@ -0,0 +1,35 @@
1
+ name: Main
2
+ on:
3
+ - push
4
+ - pull_request_target
5
+ jobs:
6
+ ci:
7
+ name: CI
8
+ runs-on: ubuntu-latest
9
+ env:
10
+ CI: true
11
+ steps:
12
+ - run: sudo apt-get install libsqlite3-dev
13
+ - uses: actions/checkout@master
14
+ - uses: ruby/setup-ruby@v1
15
+ with:
16
+ ruby-version: 3.0
17
+ bundler-cache: true
18
+ - name: Lint and test
19
+ run: |
20
+ bundle exec rubocop --parallel
21
+ bundle exec rake test
22
+ automerge:
23
+ name: AutoMerge
24
+ needs: ci
25
+ runs-on: ubuntu-latest
26
+ if: github.event_name == 'pull_request_target' && (github.actor == github.repository_owner || github.actor == 'dependabot[bot]')
27
+ steps:
28
+ - uses: actions/github-script@v3
29
+ with:
30
+ script: |
31
+ github.pulls.merge({
32
+ owner: context.payload.repository.owner.login,
33
+ repo: context.payload.repository.name,
34
+ pull_number: context.payload.pull_request.number
35
+ })
data/.gitignore ADDED
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
data/.rubocop.yml ADDED
@@ -0,0 +1,34 @@
1
+ AllCops:
2
+ DisplayCopNames: true
3
+ DisplayStyleGuide: true
4
+ TargetRubyVersion: 2.7
5
+ Exclude:
6
+ - 'vendor/**/*'
7
+ - 'yard/**/*'
8
+
9
+ Gemspec/RequiredRubyVersion:
10
+ Enabled: false
11
+
12
+ Layout/LineLength:
13
+ Max: 80
14
+
15
+ Lint/AmbiguousBlockAssociation:
16
+ Enabled: false
17
+
18
+ Lint/MissingSuper:
19
+ Enabled: false
20
+
21
+ Naming/RescuedExceptionsVariableName:
22
+ Enabled: false
23
+
24
+ Style/Documentation:
25
+ Enabled: false
26
+
27
+ Style/FormatString:
28
+ EnforcedStyle: percent
29
+
30
+ Style/FormatStringToken:
31
+ Enabled: false
32
+
33
+ Style/PerlBackrefs:
34
+ Enabled: false
data/CHANGELOG.md ADDED
@@ -0,0 +1,27 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project will be documented in this file.
4
+
5
+ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
6
+
7
+ ## [Unreleased]
8
+
9
+ ## [1.0.1] - 2021-11-17
10
+
11
+ ### Changed
12
+
13
+ - Require MFA for releasing.
14
+
15
+ ## [1.0.0] - 2018-12-05
16
+
17
+ ### Changed
18
+
19
+ - Instead of overwriting the writer, adds a `before_validation` callback so that the extras can be composed.
20
+ - From `set_*_attributes` to just `*_attributes`, .e.g, `set_nullified_attributes` became `nullify_attributes`.
21
+
22
+ ### Removed
23
+
24
+ - All of the introspection methods.
25
+
26
+ [Unreleased]: https://github.com/kddnewton/attribute_extras/compare/v1.0.0...HEAD
27
+ [1.0.0]: https://github.com/kddnewton/attribute_extras/compare/ccda61...v1.0.0
@@ -0,0 +1,76 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, sex characteristics, gender identity and expression,
9
+ level of experience, education, socio-economic status, nationality, personal
10
+ appearance, race, religion, or sexual identity and orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ - Using welcoming and inclusive language
18
+ - Being respectful of differing viewpoints and experiences
19
+ - Gracefully accepting constructive criticism
20
+ - Focusing on what is best for the community
21
+ - Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ - The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ - Trolling, insulting/derogatory comments, and personal or political attacks
28
+ - Public or private harassment
29
+ - Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ - Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at kddnewton@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at https://www.contributor-covenant.org/version/1/4/code-of-conduct.html
72
+
73
+ [homepage]: https://www.contributor-covenant.org
74
+
75
+ For answers to common questions about this code of conduct, see
76
+ https://www.contributor-covenant.org/faq
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ gemspec
6
+
7
+ gem 'activerecord', '>= 6.0.0.beta1'
data/Gemfile.lock ADDED
@@ -0,0 +1,70 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ attribute_extras (1.0.1)
5
+ activerecord (> 3)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ activemodel (7.0.0.alpha2)
11
+ activesupport (= 7.0.0.alpha2)
12
+ activerecord (7.0.0.alpha2)
13
+ activemodel (= 7.0.0.alpha2)
14
+ activesupport (= 7.0.0.alpha2)
15
+ activesupport (7.0.0.alpha2)
16
+ concurrent-ruby (~> 1.0, >= 1.0.2)
17
+ i18n (>= 1.6, < 2)
18
+ minitest (>= 5.1)
19
+ tzinfo (~> 2.0)
20
+ ast (2.4.2)
21
+ concurrent-ruby (1.1.9)
22
+ docile (1.3.5)
23
+ i18n (1.8.10)
24
+ concurrent-ruby (~> 1.0)
25
+ minitest (5.14.4)
26
+ parallel (1.21.0)
27
+ parser (3.0.2.0)
28
+ ast (~> 2.4.1)
29
+ rainbow (3.0.0)
30
+ rake (13.0.6)
31
+ regexp_parser (2.1.1)
32
+ rexml (3.2.5)
33
+ rubocop (1.23.0)
34
+ parallel (~> 1.10)
35
+ parser (>= 3.0.0.0)
36
+ rainbow (>= 2.2.2, < 4.0)
37
+ regexp_parser (>= 1.8, < 3.0)
38
+ rexml
39
+ rubocop-ast (>= 1.12.0, < 2.0)
40
+ ruby-progressbar (~> 1.7)
41
+ unicode-display_width (>= 1.4.0, < 3.0)
42
+ rubocop-ast (1.13.0)
43
+ parser (>= 3.0.1.1)
44
+ ruby-progressbar (1.11.0)
45
+ simplecov (0.21.2)
46
+ docile (~> 1.1)
47
+ simplecov-html (~> 0.11)
48
+ simplecov_json_formatter (~> 0.1)
49
+ simplecov-html (0.12.3)
50
+ simplecov_json_formatter (0.1.2)
51
+ sqlite3 (1.4.2)
52
+ tzinfo (2.0.4)
53
+ concurrent-ruby (~> 1.0)
54
+ unicode-display_width (2.1.0)
55
+
56
+ PLATFORMS
57
+ ruby
58
+
59
+ DEPENDENCIES
60
+ activerecord (>= 6.0.0.beta1)
61
+ attribute_extras!
62
+ bundler (~> 2.0)
63
+ minitest (~> 5.0)
64
+ rake (~> 13.0)
65
+ rubocop (~> 1.12)
66
+ simplecov (~> 0.17)
67
+ sqlite3 (~> 1.4)
68
+
69
+ BUNDLED WITH
70
+ 2.2.3
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015-present Kevin Newton
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.
data/README.md CHANGED
@@ -1,105 +1,112 @@
1
- ## AttributeExtras
1
+ # AttributeExtras
2
2
 
3
- Use this gem for automatic behavior on attributes. It by default provides three class macros that can be used for managing attributes. You can also build your own macros that will automatically become available to you within your ActiveRecord models. By default each macro gets the options `:validator` and `:writer` and both are set to true. If you do not want the macros to validate or overwrite the attribute writers, you can pass false to those options.
3
+ [![Build Status](https://github.com/kddnewton/attribute_extras/workflows/Main/badge.svg)](https://github.com/kddnewton/attribute_extras/actions)
4
+ [![Gem](https://img.shields.io/gem/v/attribute_extras.svg)](https://rubygems.org/gems/attribute_extras)
4
5
 
5
- ### nullify_attributes
6
+ Use this gem for automatic behavior on attributes performed before validation. You can use the predefined extras or define your own.
6
7
 
7
- Causes attribute assignment to check for presence of the given value, and set the value accordingly.
8
+ ## Installation
8
9
 
9
- ```ruby
10
- class Person < ActiveRecord::Base
11
- nullify_attributes :name
12
- end
10
+ Add this line to your application's Gemfile:
13
11
 
14
- p = Person.new(name: ' ')
15
- p.name # => nil
12
+ ```ruby
13
+ gem 'attribute_extras'
16
14
  ```
17
15
 
18
- ### strip_attributes
16
+ And then execute:
17
+
18
+ $ bundle
19
+
20
+ Or install it yourself as:
21
+
22
+ $ gem install attribute_extras
19
23
 
20
- Causes string attribute assignment to strip the given value, and set the value accordingly.
24
+ ## Usage
25
+
26
+ `AttributeExtras` provides three extras that are predefined: `nullify_attributes`, `strip_attributes`, and `truncate_attributes`. You can use these methods to tell `AttributeExtras` to perform mutations before validation. Additionally, you can call these methods at any time to perform the mutation programmatically. Examples are below:
27
+
28
+ ### `ActiveRecord::Base::nullify_attributes`
29
+
30
+ Sets the value to `nil` if the value is blank.
21
31
 
22
32
  ```ruby
23
33
  class Person < ActiveRecord::Base
24
- strip_attributes :name
34
+ nullify_attributes :name
25
35
  end
26
36
 
27
- p = Person.new(name: ' value ')
28
- p.name # => 'value'
37
+ person = Person.create(name: ' ')
38
+ person.name # => nil
39
+
40
+ person = Person.new(name: ' ')
41
+ person.nullify_attributes
42
+ person.name # => nil
29
43
  ```
30
44
 
31
- ### truncate_attributes
45
+ ### `ActiveRecord::Base::strip_attributes`
32
46
 
33
- Causes string attribute assignment to be truncated down to the maximum allowed value for that column.
47
+ Strips the value.
34
48
 
35
49
  ```ruby
36
50
  class Person < ActiveRecord::Base
37
- truncate_attributes :name
51
+ strip_attributes :name
38
52
  end
39
53
 
40
- p = Person.new(name: 'a' * 500)
41
- p.name # => 'a' * limit
42
- ```
43
-
44
- ### Inheritance
54
+ person = Person.create(name: ' value ')
55
+ person.name # => 'value'
45
56
 
46
- By default, attributes that you manipulate with any of the above macros will be inherited into the subclasses.
47
-
48
- ### Other methods
57
+ person = Person.new(name: ' value ')
58
+ person.strip_attributes
59
+ person.name # => 'value'
60
+ ```
49
61
 
50
- For migrating values to the correct format specified, there are two methods for each macro that will enforce the format. For example, for the `nullify_attributes` macro there is the `nullify_attributes` instance method and the `nullify_attributes!` instance method. Both will set the correct values and then call their respective `save`.
62
+ ### `ActiveRecord::Base::truncate_attributes`
51
63
 
52
- For introspection, there are two methods for each macro supplied. For example, for the `nullify_attributes` macro there is the `nullified_attributes` method and the `inherited_nullified_attributes` method. Examples below:
64
+ Truncates the value to the maximum length allowed by the column.
53
65
 
54
66
  ```ruby
55
67
  class Person < ActiveRecord::Base
56
- nullify_attributes :name
57
- end
58
-
59
- class Developer < Person
60
- nullify_attributes :email
68
+ truncate_attributes :name
61
69
  end
62
70
 
63
- Person.nullified_attributes.map(&:attribute) # => [:name]
64
- Person.inherited_nullified_attributes.map(&:attribute) # => [:name]
71
+ person = Person.create(name: 'a' * 500)
72
+ person.name # => 'a' * limit
65
73
 
66
- Developer.nullified_attributes.map(&:attribute) # => [:email]
67
- Developer.inherited_nullified_attributes.map(&:attribute) # => [:email, :name]
74
+ person = Person.new(name: 'a' * 500)
75
+ person.truncate_attributes
76
+ person.name # => 'a' * limit
68
77
  ```
69
78
 
70
- ## Internals
79
+ ### `AttributeExtras::define_extra`
71
80
 
72
- If you want to register your own macros, you can do so with `AttributeExtras.register_extra`. Internally `attribute_extras` does this for the three macros listed above.
73
-
74
- ### `:nullify` Example
81
+ You can define your own extras by using the `define_extra` method on the `AttributeExtras` module. `define_extra` takes a name for the extra and a block which itself accepts three arguments (the record being modified, the attribute being modified, and the value of the attribute). The block should return the modified value. An example would be:
75
82
 
76
83
  ```ruby
77
- AttributeExtras.configure do |config|
78
- config.register_extra :nullify, ->(value, options){ value.presence },
79
- past: :nullified,
80
- validator: { format: { allow_nil: true, without: /\A\s*\z/ } }
84
+ AttributeExtras.define_extra :double_attributes do |_record, _attribute, value|
85
+ value * 2
81
86
  end
82
- ```
83
87
 
84
- ### `:truncate` Example
85
-
86
- ```ruby
87
- AttributeExtras.configure do |config|
88
- config.register_extra :truncate, ->(value, options){ value.is_a?(String) ? value[0...options[:limit]] : value },
89
- past: :truncated,
90
- validator: ->(options){ { length: { maximum: options[:limit] } } },
91
- options: ->(attribute){ { limit: self.columns_hash[attribute.to_s].limit } }
88
+ class Person < ActiveRecord::Base
89
+ double_attributes :age
92
90
  end
91
+
92
+ person = Person.create(age: 5)
93
+ person.age # => 10
94
+
95
+ person = Person.new(age: 5)
96
+ person.double_attributes
97
+ person.age # => 10
93
98
  ```
94
99
 
95
- In this case the options is needed to build a hash of metadata about the attribute in question so that the validator option can change. The options hash is also passed in to the `truncate_attribute_extra` method so that you have access to that metadata.
100
+ ## Development
101
+
102
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
96
103
 
97
- ## Additional information
104
+ 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).
98
105
 
99
- ### Contributing
106
+ ## Contributing
100
107
 
101
- Contributions welcome! Please submit a pull request with tests.
108
+ Bug reports and pull requests are welcome on GitHub at https://github.com/kddnewton/attribute_extras.
102
109
 
103
- ### License
110
+ ## License
104
111
 
105
- MIT License.
112
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile CHANGED
@@ -1,27 +1,12 @@
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
1
+ # frozen_string_literal: true
6
2
 
7
- require 'rdoc/task'
3
+ require 'bundler/gem_tasks'
8
4
  require 'rake/testtask'
9
5
 
10
- RDoc::Task.new(:rdoc) do |rdoc|
11
- rdoc.rdoc_dir = 'rdoc'
12
- rdoc.title = 'AttributeExtras'
13
- rdoc.options << '--line-numbers'
14
- rdoc.rdoc_files.include('README.md')
15
- rdoc.rdoc_files.include('lib/**/*.rb')
16
- end
17
-
18
- Bundler::GemHelper.install_tasks
19
-
20
6
  Rake::TestTask.new(:test) do |t|
21
- t.libs << 'lib'
22
7
  t.libs << 'test'
23
- t.pattern = 'test/**/*_test.rb'
24
- t.verbose = false
8
+ t.libs << 'lib'
9
+ t.test_files = FileList['test/**/*_test.rb']
25
10
  end
26
11
 
27
12
  task default: :test
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'lib/attribute_extras/version'
4
+
5
+ version = AttributeExtras::VERSION
6
+ repository = 'https://github.com/kddnewton/attribute_extras'
7
+
8
+ Gem::Specification.new do |spec|
9
+ spec.name = 'attribute_extras'
10
+ spec.version = version
11
+ spec.authors = ['Kevin Newton']
12
+ spec.email = ['kddnewton@gmail.com']
13
+
14
+ spec.summary = 'Extra macros for auto attribute manipulation.'
15
+ spec.description = <<~DOC
16
+ Builds macros to automatically manipulate your models' attributes.
17
+ DOC
18
+ spec.homepage = repository
19
+ spec.license = 'MIT'
20
+
21
+ spec.metadata = {
22
+ 'bug_tracker_uri' => "#{repository}/issues",
23
+ 'changelog_uri' => "#{repository}/blob/v#{version}/CHANGELOG.md",
24
+ 'source_code_uri' => repository,
25
+ 'rubygems_mfa_required' => 'true'
26
+ }
27
+
28
+ spec.files =
29
+ Dir.chdir(__dir__) do
30
+ `git ls-files -z`.split("\x0").reject do |f|
31
+ f.match(%r{^(test|spec|features)/})
32
+ end
33
+ end
34
+
35
+ spec.bindir = 'exe'
36
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
37
+ spec.require_paths = ['lib']
38
+
39
+ spec.add_dependency 'activerecord', '> 3'
40
+
41
+ spec.add_development_dependency 'bundler', '~> 2.0'
42
+ spec.add_development_dependency 'minitest', '~> 5.0'
43
+ spec.add_development_dependency 'rake', '~> 13.0'
44
+ spec.add_development_dependency 'rubocop', '~> 1.12'
45
+ spec.add_development_dependency 'simplecov', '~> 0.17'
46
+ spec.add_development_dependency 'sqlite3', '~> 1.4'
47
+ end
data/bin/console ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'attribute_extras'
6
+
7
+ require 'irb'
8
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
@@ -1,6 +1,5 @@
1
- module AttributeExtras
2
-
3
- # current version of the gem
4
- VERSION = '0.1.4'
1
+ # frozen_string_literal: true
5
2
 
3
+ module AttributeExtras
4
+ VERSION = '1.0.1'
6
5
  end