attribute_extras 0.1.6 → 1.0.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: 3d6f41939b95f9077e1d709d128a1a32bcb67f14479f2bb8f75365db675dc8a5
4
+ data.tar.gz: aab6a1d29cc6aeb19a5e3c098b15c60c7ca20601f2c1e73dfbdf8945e429ee7e
5
+ SHA512:
6
+ metadata.gz: 78b61939069af036335b4c07fc700725777ef2fa536673f546b0faadea49ebb80b5bae4e9658968e389137cfb495209562ed3178cc5fc6b53978d8620849a4c6
7
+ data.tar.gz: cc998b6d64c6efc0cbbe3ab8e1dc3cc53e7eb6bbd49af52f7c8d9b9a5278bd4bf677f70834d71af4fb47f006faa116aa3ef02da97f5f13d04e72acafebf67f5b
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
@@ -0,0 +1,9 @@
1
+ AllCops:
2
+ DisplayCopNames: true
3
+ DisplayStyleGuide: true
4
+ TargetRubyVersion: 2.5
5
+ Exclude:
6
+ - 'vendor/**/*'
7
+
8
+ Style/PerlBackrefs:
9
+ Enabled: false
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ cache: bundler
3
+ rvm: 2.5.3
@@ -0,0 +1,18 @@
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.0] - 2018-12-05
10
+ ### Changed
11
+ - Instead of overwriting the writer, adds a `before_validation` callback so that the extras can be composed.
12
+ - From `set_*_attributes` to just `*_attributes`, .e.g, `set_nullified_attributes` became `nullify_attributes`.
13
+
14
+ ### Removed
15
+ - All of the introspection methods.
16
+
17
+ [Unreleased]: https://github.com/CultureHQ/components/compare/v1.0.0...HEAD
18
+ [1.0.0]: https://github.com/CultureHQ/components/compare/v0.1.6...v0.0.1
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ gemspec
@@ -0,0 +1,69 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ attribute_extras (1.0.0)
5
+ activerecord (> 3)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ activemodel (5.2.2)
11
+ activesupport (= 5.2.2)
12
+ activerecord (5.2.2)
13
+ activemodel (= 5.2.2)
14
+ activesupport (= 5.2.2)
15
+ arel (>= 9.0)
16
+ activesupport (5.2.2)
17
+ concurrent-ruby (~> 1.0, >= 1.0.2)
18
+ i18n (>= 0.7, < 2)
19
+ minitest (~> 5.1)
20
+ tzinfo (~> 1.1)
21
+ arel (9.0.0)
22
+ ast (2.4.0)
23
+ concurrent-ruby (1.1.3)
24
+ docile (1.3.1)
25
+ i18n (1.1.1)
26
+ concurrent-ruby (~> 1.0)
27
+ jaro_winkler (1.5.1)
28
+ json (2.1.0)
29
+ minitest (5.11.3)
30
+ parallel (1.12.1)
31
+ parser (2.5.3.0)
32
+ ast (~> 2.4.0)
33
+ powerpack (0.1.2)
34
+ rainbow (3.0.0)
35
+ rake (12.3.1)
36
+ rubocop (0.60.0)
37
+ jaro_winkler (~> 1.5.1)
38
+ parallel (~> 1.10)
39
+ parser (>= 2.5, != 2.5.1.1)
40
+ powerpack (~> 0.1)
41
+ rainbow (>= 2.2.2, < 4.0)
42
+ ruby-progressbar (~> 1.7)
43
+ unicode-display_width (~> 1.4.0)
44
+ ruby-progressbar (1.10.0)
45
+ simplecov (0.16.1)
46
+ docile (~> 1.1)
47
+ json (>= 1.8, < 3)
48
+ simplecov-html (~> 0.10.0)
49
+ simplecov-html (0.10.2)
50
+ sqlite3 (1.3.13)
51
+ thread_safe (0.3.6)
52
+ tzinfo (1.2.5)
53
+ thread_safe (~> 0.1)
54
+ unicode-display_width (1.4.0)
55
+
56
+ PLATFORMS
57
+ ruby
58
+
59
+ DEPENDENCIES
60
+ attribute_extras!
61
+ bundler (~> 1.17)
62
+ minitest (~> 5.0)
63
+ rake (~> 12.3)
64
+ rubocop (~> 0.60)
65
+ simplecov (~> 0.16)
66
+ sqlite3 (~> 1.3)
67
+
68
+ BUNDLED WITH
69
+ 1.17.1
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Kevin Deisz
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://travis-ci.org/kddeisz/attribute_extras.svg?branch=master)](https://travis-ci.org/kddeisz/attribute_extras)
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 macros 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_macro`
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 macros by using the `define_macro` method on the `AttributeExtras` module. `define_macro` a name for the macro 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_macro :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/kddeisz/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,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path('lib', __dir__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'attribute_extras/version'
6
+
7
+ Gem::Specification.new do |spec| # rubocop:disable Metrics/BlockLength
8
+ spec.name = 'attribute_extras'
9
+ spec.version = AttributeExtras::VERSION
10
+ spec.authors = ['Kevin Deisz']
11
+ spec.email = ['kevin.deisz@gmail.com']
12
+
13
+ spec.summary = 'Extra macros for auto attribute manipulation.'
14
+ spec.description = <<~DOC
15
+ Builds macros to automatically manipulate your models' attributes.
16
+ DOC
17
+ spec.homepage = 'https://github.com/kddeisz/attribute_extras'
18
+ spec.license = 'MIT'
19
+
20
+ spec.files =
21
+ Dir.chdir(__dir__) do
22
+ `git ls-files -z`.split("\x0").reject do |f|
23
+ f.match(%r{^(test|spec|features)/})
24
+ end
25
+ end
26
+
27
+ spec.bindir = 'exe'
28
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
29
+ spec.require_paths = ['lib']
30
+
31
+ spec.add_dependency 'activerecord', '> 3'
32
+
33
+ spec.add_development_dependency 'bundler', '~> 1.17'
34
+ spec.add_development_dependency 'minitest', '~> 5.0'
35
+ spec.add_development_dependency 'rake', '~> 12.3'
36
+ spec.add_development_dependency 'rubocop', '~> 0.60'
37
+ spec.add_development_dependency 'simplecov', '~> 0.16'
38
+ spec.add_development_dependency 'sqlite3', '~> 1.3'
39
+ end
@@ -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__)
@@ -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,50 +1,54 @@
1
- require 'attribute_extras/extra_builder'
2
- require 'attribute_extras/hook_builder'
3
- require 'attribute_extras/modifier'
4
- require 'logger'
1
+ # frozen_string_literal: true
5
2
 
6
- # Used for automatic attribute manipulation
3
+ require 'attribute_extras/version'
4
+
5
+ # Extra macros for auto attribute manipulation.
7
6
  module AttributeExtras
7
+ # Parent class of the various extras.
8
+ class AttributeExtra < Module
9
+ attr_reader :name
10
+
11
+ def initialize(name, attributes, perform)
12
+ @name = name
13
+ define_extra(name, attributes, perform)
14
+ end
15
+
16
+ def included(clazz)
17
+ clazz.before_validation(name)
18
+ end
19
+
20
+ private
21
+
22
+ def define_extra(name, attributes, perform)
23
+ define_method(name) do
24
+ attributes.each do |attribute|
25
+ value = public_send(attribute)
26
+ public_send(:"#{attribute}=", perform[self, attribute, value])
27
+ end
28
+ end
29
+ end
30
+ end
8
31
 
9
- # the registered extras
10
- mattr_accessor :extras
11
- self.extras = []
32
+ def self.define_extra(name, &perform)
33
+ extra = Class.new(AttributeExtra)
34
+ extra_name = name.to_s.gsub(/(?:\A|_)([a-z])/i) { $1.upcase }.to_sym
12
35
 
13
- # wrap up configuration options into one block
14
- def self.configure(&block)
15
- yield self
36
+ AttributeExtras.const_set(extra_name, extra)
37
+ ActiveRecord::Base.define_singleton_method(name) do |*attributes|
38
+ include extra.new(name, attributes, perform)
39
+ end
16
40
  end
17
41
 
18
- # the logger for attribute extras
19
- def self.logger
20
- @logger ||= Logger.new($stderr)
42
+ define_extra :nullify_attributes do |*, value|
43
+ value.presence
21
44
  end
22
45
 
23
- # register the extra and build the functions
24
- def self.register_extra(verb, function, past:, validator:, options: nil)
25
- past ||= verb
26
- compiled_validator = validator.is_a?(Proc) ? validator : ->(options){ validator }
27
- options ||= ->(attribute){ {} }
28
-
29
- extra = ExtraBuilder.new(verb, past, function, compiled_validator, options).build
30
- hook = HookBuilder.new(verb, past).build
31
-
32
- self.const_set(:"#{verb.capitalize}Attributes", extra)
33
- self.extras << extra
34
- ActiveRecord::Base.extend(hook)
46
+ define_extra :strip_attributes do |*, value|
47
+ value.is_a?(String) ? value.strip : value
35
48
  end
36
49
 
37
- self.register_extra :nullify, ->(value, options){ value.presence },
38
- past: :nullified,
39
- validator: { format: { allow_nil: true, without: /\A\s*\z/ } }
40
-
41
- self.register_extra :strip, ->(value, options){ value.is_a?(String) ? value.strip : value },
42
- past: :stripped,
43
- validator: { format: { without: /\A\s+|\s+\z/ } }
44
-
45
- self.register_extra :truncate, ->(value, options){ value.is_a?(String) ? value[0...options[:limit]] : value },
46
- past: :truncated,
47
- validator: ->(options){ { length: { maximum: options[:limit] } } },
48
- options: ->(attribute){ { limit: self.columns_hash[attribute.to_s].limit } }
49
-
50
+ define_extra :truncate_attributes do |record, attribute, value|
51
+ limit = record.class.columns_hash[attribute.to_s].limit
52
+ value.is_a?(String) ? value[0...limit] : value
53
+ end
50
54
  end
@@ -1,6 +1,5 @@
1
- module AttributeExtras
2
-
3
- # current version of the gem
4
- VERSION = '0.1.6'
1
+ # frozen_string_literal: true
5
2
 
3
+ module AttributeExtras
4
+ VERSION = '1.0.0'
6
5
  end
metadata CHANGED
@@ -1,116 +1,158 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: attribute_extras
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.6
5
- prerelease:
4
+ version: 1.0.0
6
5
  platform: ruby
7
6
  authors:
8
7
  - Kevin Deisz
9
8
  autorequire:
10
- bindir: bin
9
+ bindir: exe
11
10
  cert_chain: []
12
- date: 2016-02-10 00:00:00.000000000 Z
11
+ date: 2018-12-05 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: activerecord
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
- - - ! '>'
17
+ - - ">"
20
18
  - !ruby/object:Gem::Version
21
19
  version: '3'
22
20
  type: :runtime
23
21
  prerelease: false
24
22
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
23
  requirements:
27
- - - ! '>'
24
+ - - ">"
28
25
  - !ruby/object:Gem::Version
29
26
  version: '3'
30
27
  - !ruby/object:Gem::Dependency
31
- name: activesupport
28
+ name: bundler
32
29
  requirement: !ruby/object:Gem::Requirement
33
- none: false
34
30
  requirements:
35
- - - ! '>'
31
+ - - "~>"
36
32
  - !ruby/object:Gem::Version
37
- version: '3'
38
- type: :runtime
33
+ version: '1.17'
34
+ type: :development
39
35
  prerelease: false
40
36
  version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
37
  requirements:
43
- - - ! '>'
38
+ - - "~>"
44
39
  - !ruby/object:Gem::Version
45
- version: '3'
40
+ version: '1.17'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '12.3'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '12.3'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rubocop
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '0.60'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '0.60'
83
+ - !ruby/object:Gem::Dependency
84
+ name: simplecov
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '0.16'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '0.16'
46
97
  - !ruby/object:Gem::Dependency
47
98
  name: sqlite3
48
99
  requirement: !ruby/object:Gem::Requirement
49
- none: false
50
100
  requirements:
51
- - - ! '>='
101
+ - - "~>"
52
102
  - !ruby/object:Gem::Version
53
- version: '0'
103
+ version: '1.3'
54
104
  type: :development
55
105
  prerelease: false
56
106
  version_requirements: !ruby/object:Gem::Requirement
57
- none: false
58
107
  requirements:
59
- - - ! '>='
108
+ - - "~>"
60
109
  - !ruby/object:Gem::Version
61
- version: '0'
62
- description: Builds macros to automatically manipulate your models' attributes.
110
+ version: '1.3'
111
+ description: 'Builds macros to automatically manipulate your models'' attributes.
112
+
113
+ '
63
114
  email:
64
- - info@trialnetworks.com
115
+ - kevin.deisz@gmail.com
65
116
  executables: []
66
117
  extensions: []
67
118
  extra_rdoc_files: []
68
119
  files:
69
- - lib/attribute_extras/extra_builder.rb
70
- - lib/attribute_extras/hook_builder.rb
71
- - lib/attribute_extras/modifier.rb
72
- - lib/attribute_extras/version.rb
73
- - lib/attribute_extras.rb
74
- - MIT-LICENSE
75
- - Rakefile
120
+ - ".gitignore"
121
+ - ".rubocop.yml"
122
+ - ".travis.yml"
123
+ - CHANGELOG.md
124
+ - Gemfile
125
+ - Gemfile.lock
126
+ - LICENSE
76
127
  - README.md
77
- - test/base_extensions_test.rb
78
- - test/modifier_test.rb
79
- - test/nullify_attributes_test.rb
80
- - test/strip_attributes_test.rb
81
- - test/test_classes.rb
82
- - test/test_helper.rb
83
- - test/truncate_attributes_test.rb
84
- homepage: https://github.com/drugdev/attribute_extras
128
+ - Rakefile
129
+ - attribute_extras.gemspec
130
+ - bin/console
131
+ - bin/setup
132
+ - lib/attribute_extras.rb
133
+ - lib/attribute_extras/version.rb
134
+ homepage: https://github.com/kddeisz/attribute_extras
85
135
  licenses:
86
136
  - MIT
137
+ metadata: {}
87
138
  post_install_message:
88
139
  rdoc_options: []
89
140
  require_paths:
90
141
  - lib
91
142
  required_ruby_version: !ruby/object:Gem::Requirement
92
- none: false
93
143
  requirements:
94
- - - ! '>='
144
+ - - ">="
95
145
  - !ruby/object:Gem::Version
96
146
  version: '0'
97
147
  required_rubygems_version: !ruby/object:Gem::Requirement
98
- none: false
99
148
  requirements:
100
- - - ! '>='
149
+ - - ">="
101
150
  - !ruby/object:Gem::Version
102
151
  version: '0'
103
152
  requirements: []
104
153
  rubyforge_project:
105
- rubygems_version: 1.8.23
154
+ rubygems_version: 2.7.6
106
155
  signing_key:
107
- specification_version: 3
156
+ specification_version: 4
108
157
  summary: Extra macros for auto attribute manipulation.
109
- test_files:
110
- - test/base_extensions_test.rb
111
- - test/modifier_test.rb
112
- - test/nullify_attributes_test.rb
113
- - test/strip_attributes_test.rb
114
- - test/test_classes.rb
115
- - test/test_helper.rb
116
- - test/truncate_attributes_test.rb
158
+ test_files: []
@@ -1,20 +0,0 @@
1
- Copyright 2015 DrugDev, Inc.
2
-
3
- Permission is hereby granted, free of charge, to any person obtaining
4
- a copy of this software and associated documentation files (the
5
- "Software"), to deal in the Software without restriction, including
6
- without limitation the rights to use, copy, modify, merge, publish,
7
- distribute, sublicense, and/or sell copies of the Software, and to
8
- permit persons to whom the Software is furnished to do so, subject to
9
- the following conditions:
10
-
11
- The above copyright notice and this permission notice shall be
12
- included in all copies or substantial portions of the Software.
13
-
14
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
- NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
- LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
- OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
- WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -1,83 +0,0 @@
1
- module AttributeExtras
2
-
3
- # Builds a concern that will be included into classes when they call
4
- # the macro corresponding to the given verb. For instance, for the verb
5
- # :nullify it will build out AttributeExtras::NullifyAttributes, which
6
- # can be included into classes to get all of the utility functions.
7
- class ExtraBuilder
8
-
9
- # store the given options
10
- def initialize(verb, past, function, validator, options)
11
- @verb = verb
12
- @past = past
13
- @function = function
14
- @validator = validator
15
- @options = options
16
- end
17
-
18
- # build the extra
19
- def build
20
- concern = Module.new
21
- concern.module_eval(concern_definition)
22
- concern::ClassMethods.module_eval(&utilities_definition)
23
- concern
24
- end
25
-
26
- private
27
-
28
- # the module definition for the concern
29
- def concern_definition
30
- <<-RUBY
31
- extend ActiveSupport::Concern
32
-
33
- module ClassMethods
34
- def inherited_#{@past}_attributes
35
- @inherited_#{@past}_attributes ||= begin
36
- modifiers = []
37
- self.ancestors.each do |ancestor|
38
- break if ancestor == ActiveRecord::Base
39
- if ancestor.respond_to?(:#{@past}_attributes)
40
- modifiers += ancestor.#{@past}_attributes
41
- end
42
- end
43
- modifiers
44
- end
45
- end
46
-
47
- def #{@past}_attributes
48
- @#{@past}_attributes ||= []
49
- end
50
- end
51
-
52
- def #{@verb}_attributes
53
- set_#{@past}_attributes
54
- self.changed? ? self.save : true
55
- end
56
-
57
- def #{@verb}_attributes!
58
- set_#{@past}_attributes
59
- self.changed? ? self.save : true
60
- end
61
-
62
- private
63
-
64
- def set_#{@past}_attributes
65
- self.class.inherited_#{@past}_attributes.each do |modifier|
66
- attribute = modifier.attribute
67
- self.send(:\"\#{attribute}=\", self.class.#{@verb}_attribute_extra(self.send(attribute), modifier.options))
68
- end
69
- end
70
- RUBY
71
- end
72
-
73
- # the module definition for the utilities
74
- def utilities_definition
75
- verb, function, validator, options = @verb, @function, @validator, @options
76
- proc do
77
- define_method(:"#{verb}_attribute_extra", function)
78
- define_method(:"#{verb}_validator_for", validator)
79
- define_method(:"#{verb}_options_for", options)
80
- end
81
- end
82
- end
83
- end
@@ -1,57 +0,0 @@
1
- module AttributeExtras
2
-
3
- # Builds a module that extends ActiveRecord::Base to build the macro
4
- # corresponding to the given verb. For instance, for the verb :nullify,
5
- # it will build a module that extends AR::Base with the method
6
- # :nullify_attributes, which is then used to include the concern from
7
- # the ExtraBuilder.
8
- class HookBuilder
9
-
10
- # store the given options
11
- def initialize(verb, past)
12
- @verb = verb
13
- @past = past
14
- end
15
-
16
- # build the hook
17
- def build
18
- hook = Module.new
19
- hook.module_eval(module_definition)
20
- hook
21
- end
22
-
23
- private
24
-
25
- # the module definition for the extra
26
- def module_definition
27
- <<-RUBY
28
- def #{@verb}_attributes(*attributes, validator: true, writer: true)
29
- return unless self.table_exists?
30
- if (non_attributes = attributes.map(&:to_s) - self.column_names).any?
31
- AttributeExtras.logger.warn("Invalid attributes passed to #{@verb}_attributes: \#{non_attributes.join(', ')}")
32
- return
33
- end
34
-
35
- include ::AttributeExtras::#{@verb.capitalize}Attributes
36
-
37
- attributes.each do |attribute|
38
- options = self.#{@verb}_options_for(attribute)
39
- modifier = Modifier.new(attribute, options)
40
-
41
- if validator
42
- validates attribute, self.#{@verb}_validator_for(modifier.options)
43
- end
44
-
45
- if writer
46
- define_method("\#{attribute}=") do |value|
47
- write_attribute(attribute, self.class.#{@verb}_attribute_extra(value, modifier.options))
48
- end
49
- end
50
-
51
- #{@past}_attributes << modifier
52
- end
53
- end
54
- RUBY
55
- end
56
- end
57
- end
@@ -1,19 +0,0 @@
1
- module AttributeExtras
2
-
3
- # a container for an attribute that has been modified
4
- class Modifier
5
-
6
- # the attribute this modifier represents
7
- attr_reader :attribute
8
-
9
- # the set of options generated for this attribute
10
- attr_reader :options
11
-
12
- # store the given options
13
- def initialize(attribute, options = {})
14
- @attribute = attribute
15
- @options = options
16
- end
17
-
18
- end
19
- end
@@ -1,52 +0,0 @@
1
- require 'test_helper'
2
-
3
- class BaseExtensionsTest < ActiveSupport::TestCase
4
-
5
- def test_nullify_attributes_warns
6
- assert_output_matches 'Invalid attributes' do
7
- address_class(:nullify_attributes, :first_line, :second_line, :third_line)
8
- end
9
- end
10
-
11
- def test_nullify_attributes_success
12
- klass = address_class(:nullify_attributes, :first_line, :second_line)
13
- assert_equal klass.nullified_attributes.map(&:attribute), [:first_line, :second_line]
14
- end
15
-
16
- def test_strip_attributes_warns
17
- assert_output_matches 'Invalid attributes' do
18
- address_class(:strip_attributes, :first_line, :second_line, :third_line)
19
- end
20
- end
21
-
22
- def test_strip_attributes_success
23
- klass = address_class(:strip_attributes, :first_line, :second_line)
24
- assert_equal klass.stripped_attributes.map(&:attribute), [:first_line, :second_line]
25
- end
26
-
27
- def test_truncate_attributes_success
28
- klass = address_class(:truncate_attributes, :first_line, :second_line)
29
- assert_equal klass.truncated_attributes.map(&:attribute), [:first_line, :second_line]
30
- end
31
-
32
- private
33
-
34
- # returns a newly created address class
35
- def address_class(macro, *arguments)
36
- Class.new(ActiveRecord::Base) do
37
- self.table_name = 'addresses'
38
- send(macro, *arguments)
39
- end
40
- end
41
-
42
- # assert that the output to $stderr matches the expected
43
- def assert_output_matches(expected)
44
- stderr = $stderr
45
- $stderr = StringIO.new
46
- AttributeExtras.instance_variable_set(:@logger, Logger.new($stderr))
47
- yield
48
- assert_match expected, $stderr.string
49
- ensure
50
- $stderr = stderr
51
- end
52
- end
@@ -1,9 +0,0 @@
1
- require 'test_helper'
2
-
3
- class ModifierTest < ActiveSupport::TestCase
4
- def test_modifiers
5
- modifier = AttributeExtras::Modifier.new(:name, limit: 255)
6
- assert_equal modifier.attribute, :name
7
- assert_equal modifier.options, { limit: 255 }
8
- end
9
- end
@@ -1,57 +0,0 @@
1
- require 'test_helper'
2
-
3
- class NullifyAttributesTest < ActiveSupport::TestCase
4
- def test_nullify_attributes
5
- person = Person.new
6
- person.set_blank_attributes
7
-
8
- assert person.nullify_attributes
9
- person_attributes.each do |attribute|
10
- assert_nil person.send(attribute)
11
- end
12
- end
13
-
14
- def test_nullify_attributes!
15
- person = Person.new
16
- person.set_blank_attributes
17
-
18
- assert person.nullify_attributes!
19
- person_attributes.each do |attribute|
20
- assert_nil person.send(attribute)
21
- end
22
- end
23
-
24
- def test_nullified_attributes_inheritance
25
- architect = Architect.new
26
- architect.set_blank_attributes
27
-
28
- assert architect.nullify_attributes
29
- architect_attributes.each do |attribute|
30
- assert_nil architect.send(attribute)
31
- end
32
- end
33
-
34
- def test_nullified_attributes
35
- assert_equal person_attributes, Person.nullified_attributes.map(&:attribute)
36
- assert_empty Developer.nullified_attributes.map(&:attribute)
37
- assert_equal [:architect_nullified], Architect.nullified_attributes.map(&:attribute)
38
- end
39
-
40
- def test_inherited_nullified_attributes
41
- assert_equal person_attributes, Person.inherited_nullified_attributes.map(&:attribute)
42
- assert_equal person_attributes, Developer.inherited_nullified_attributes.map(&:attribute)
43
- assert_equal architect_attributes, Architect.inherited_nullified_attributes.map(&:attribute)
44
- end
45
-
46
- private
47
-
48
- # a list of the attributes that are nullified on the architect class
49
- def architect_attributes
50
- @architect_attributes ||= ([:architect_nullified] + person_attributes)
51
- end
52
-
53
- # a list of the attributes that are nullified on the person class
54
- def person_attributes
55
- @person_attributes ||= [:person_nullified_one, :person_nullified_two, :person_nullified_three, :person_nullified_four]
56
- end
57
- end
@@ -1,57 +0,0 @@
1
- require 'test_helper'
2
-
3
- class StripAttributesTest < ActiveSupport::TestCase
4
- def test_strip_attributes
5
- person = Person.new
6
- person.set_padded_attributes
7
-
8
- assert person.strip_attributes
9
- person_attributes.each do |attribute|
10
- assert_equal Person.stripped_value, person.send(attribute)
11
- end
12
- end
13
-
14
- def test_strip_attributes!
15
- person = Person.new
16
- person.set_padded_attributes
17
-
18
- assert person.strip_attributes!
19
- person_attributes.each do |attribute|
20
- assert_equal Person.stripped_value, person.send(attribute)
21
- end
22
- end
23
-
24
- def test_stripped_attributes_inheritance
25
- architect = Architect.new
26
- architect.set_padded_attributes
27
-
28
- assert architect.strip_attributes
29
- architect_attributes.each do |attribute|
30
- assert_equal Architect.stripped_value, architect.send(attribute)
31
- end
32
- end
33
-
34
- def test_stripped_attributes
35
- assert_equal person_attributes, Person.stripped_attributes.map(&:attribute)
36
- assert_empty Developer.stripped_attributes.map(&:attribute)
37
- assert_equal [:architect_stripped], Architect.stripped_attributes.map(&:attribute)
38
- end
39
-
40
- def test_inherited_stripped_attributes
41
- assert_equal person_attributes, Person.inherited_stripped_attributes.map(&:attribute)
42
- assert_equal person_attributes, Developer.inherited_stripped_attributes.map(&:attribute)
43
- assert_equal architect_attributes, Architect.inherited_stripped_attributes.map(&:attribute)
44
- end
45
-
46
- private
47
-
48
- # a list of the attributes that are stripped on the architect class
49
- def architect_attributes
50
- @architect_attributes ||= ([:architect_stripped] + person_attributes)
51
- end
52
-
53
- # a list of the attributes that are stripped on the person class
54
- def person_attributes
55
- @person_attributes ||= [:person_stripped_one, :person_stripped_two, :person_stripped_three, :person_stripped_four]
56
- end
57
- end
@@ -1,86 +0,0 @@
1
- COLUMN_LIMIT = 255
2
- NUMBERS = %w[one two three four]
3
-
4
- ActiveRecord::Schema.define do
5
- create_table :people, force: true do |t|
6
- %w[nullified stripped truncated].each do |prefix|
7
- options = (prefix == 'truncated') ? { limit: COLUMN_LIMIT } : {}
8
- NUMBERS.each do |number|
9
- t.string "person_#{prefix}_#{number}", options
10
- end
11
- t.string "architect_#{prefix}", options
12
- end
13
- end
14
-
15
- create_table :addresses, force: true do |t|
16
- t.string :first_line, limit: COLUMN_LIMIT
17
- t.string :second_line, limit: COLUMN_LIMIT
18
- end
19
- end
20
-
21
- class Person < ActiveRecord::Base
22
-
23
- { nullified: :nullify_attributes, stripped: :strip_attributes, truncated: :truncate_attributes }.each do |prefix, macro|
24
- NUMBERS.each_with_index do |number, index|
25
- send(macro, "person_#{prefix}_#{number}".to_sym, validator: (index / 2).even?, writer: (index % 2))
26
- end
27
- end
28
-
29
- def set_blank_attributes
30
- NUMBERS.each do |number|
31
- write_attribute("person_nullified_#{number}", ' ')
32
- end
33
- save(validate: false)
34
- end
35
-
36
- def set_long_attributes
37
- NUMBERS.each do |number|
38
- write_attribute("person_truncated_#{number}", self.class.long_value)
39
- end
40
- save(validate: false)
41
- end
42
-
43
- def set_padded_attributes
44
- NUMBERS.each do |number|
45
- write_attribute("person_stripped_#{number}", ' test value ')
46
- end
47
- save(validate: false)
48
- end
49
-
50
- class << self
51
- def short_value
52
- @short_value ||= ('a' * COLUMN_LIMIT)
53
- end
54
-
55
- def stripped_value
56
- @stripped_value ||= 'test value'
57
- end
58
-
59
- def long_value
60
- @long_value ||= ('a' * 500)
61
- end
62
- end
63
- end
64
-
65
- class Developer < Person; end
66
-
67
- class Architect < Developer
68
- nullify_attributes :architect_nullified
69
- strip_attributes :architect_stripped
70
- truncate_attributes :architect_truncated
71
-
72
- def set_blank_attributes
73
- write_attribute(:architect_nullified, ' ')
74
- super
75
- end
76
-
77
- def set_long_attributes
78
- write_attribute(:architect_truncated, self.class.long_value)
79
- super
80
- end
81
-
82
- def set_padded_attributes
83
- write_attribute(:architect_stripped, ' test value ')
84
- super
85
- end
86
- end
@@ -1,10 +0,0 @@
1
- $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
-
3
- require 'active_record'
4
- require 'minitest/autorun'
5
- require 'attribute_extras'
6
-
7
- ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
8
- ActiveSupport.test_order = :sorted
9
-
10
- require 'test_classes'
@@ -1,57 +0,0 @@
1
- require 'test_helper'
2
-
3
- class TruncateAttributesTest < ActiveSupport::TestCase
4
- def test_truncate_attributes
5
- person = Person.new
6
- person.set_long_attributes
7
-
8
- assert person.truncate_attributes
9
- person_attributes.each do |attribute|
10
- assert_equal Person.short_value, person.send(attribute)
11
- end
12
- end
13
-
14
- def test_truncate_attributes!
15
- person = Person.new
16
- person.set_long_attributes
17
-
18
- assert person.truncate_attributes!
19
- person_attributes.each do |attribute|
20
- assert_equal Person.short_value, person.send(attribute)
21
- end
22
- end
23
-
24
- def test_truncated_attributes_inheritance
25
- architect = Architect.new
26
- architect.set_long_attributes
27
-
28
- assert architect.truncate_attributes
29
- architect_attributes.each do |attribute|
30
- assert_equal Architect.short_value, architect.send(attribute)
31
- end
32
- end
33
-
34
- def test_truncated_attributes
35
- assert_equal person_attributes, Person.truncated_attributes.map(&:attribute)
36
- assert_empty Developer.truncated_attributes.map(&:attribute)
37
- assert_equal [:architect_truncated], Architect.truncated_attributes.map(&:attribute)
38
- end
39
-
40
- def test_inherited_truncated_attributes
41
- assert_equal person_attributes, Person.inherited_truncated_attributes.map(&:attribute)
42
- assert_equal person_attributes, Developer.inherited_truncated_attributes.map(&:attribute)
43
- assert_equal architect_attributes, Architect.inherited_truncated_attributes.map(&:attribute)
44
- end
45
-
46
- private
47
-
48
- # a list of the attributes that are truncated on the architect class
49
- def architect_attributes
50
- @architect_attributes ||= ([:architect_truncated] + person_attributes)
51
- end
52
-
53
- # a list of the attributes that are truncated on the person class
54
- def person_attributes
55
- @person_attributes ||= [:person_truncated_one, :person_truncated_two, :person_truncated_three, :person_truncated_four]
56
- end
57
- end