attribute_extras 0.1.2 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Rakefile +2 -9
- data/lib/attribute_extras.rb +7 -0
- data/lib/attribute_extras/extra_builder.rb +5 -0
- data/lib/attribute_extras/hook_builder.rb +7 -1
- data/lib/attribute_extras/modifier.rb +6 -1
- data/lib/attribute_extras/version.rb +4 -1
- data/test/base_extensions_test.rb +16 -15
- metadata +17 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 10a90bb4e6bf56b13a54edf33e931fbfab2bed8a
|
4
|
+
data.tar.gz: 2358a40e44937cd3c03d085be35ff494d2de4dde
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1a56ab58683db265c247d3f32ccff17e02dab15bc26888e19396e51d60132760f63387d8a4ea4d3886d55a120c079d5d96cb2d8450886aac253281ba0c63cb5b
|
7
|
+
data.tar.gz: 2f246e1be37f16968fe3e1b905e4aa44191eedacfc5e68f69a2a5f44823826f1227d5c44d27e1180633c2918b3d4fed0d4976df91a5479c3e32807f09333ffbf
|
data/Rakefile
CHANGED
@@ -5,24 +5,18 @@ rescue LoadError
|
|
5
5
|
end
|
6
6
|
|
7
7
|
require 'rdoc/task'
|
8
|
+
require 'rake/testtask'
|
8
9
|
|
9
10
|
RDoc::Task.new(:rdoc) do |rdoc|
|
10
11
|
rdoc.rdoc_dir = 'rdoc'
|
11
12
|
rdoc.title = 'AttributeExtras'
|
12
13
|
rdoc.options << '--line-numbers'
|
13
|
-
rdoc.rdoc_files.include('README.
|
14
|
+
rdoc.rdoc_files.include('README.md')
|
14
15
|
rdoc.rdoc_files.include('lib/**/*.rb')
|
15
16
|
end
|
16
17
|
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
18
|
Bundler::GemHelper.install_tasks
|
23
19
|
|
24
|
-
require 'rake/testtask'
|
25
|
-
|
26
20
|
Rake::TestTask.new(:test) do |t|
|
27
21
|
t.libs << 'lib'
|
28
22
|
t.libs << 'test'
|
@@ -30,5 +24,4 @@ Rake::TestTask.new(:test) do |t|
|
|
30
24
|
t.verbose = false
|
31
25
|
end
|
32
26
|
|
33
|
-
|
34
27
|
task default: :test
|
data/lib/attribute_extras.rb
CHANGED
@@ -1,7 +1,9 @@
|
|
1
1
|
require 'attribute_extras/extra_builder'
|
2
2
|
require 'attribute_extras/hook_builder'
|
3
3
|
require 'attribute_extras/modifier'
|
4
|
+
require 'logger'
|
4
5
|
|
6
|
+
# Used for automatic attribute manipulation
|
5
7
|
module AttributeExtras
|
6
8
|
|
7
9
|
# the registered extras
|
@@ -13,6 +15,11 @@ module AttributeExtras
|
|
13
15
|
yield self
|
14
16
|
end
|
15
17
|
|
18
|
+
# the logger for attribute extras
|
19
|
+
def self.logger
|
20
|
+
@logger ||= Logger.new($stderr)
|
21
|
+
end
|
22
|
+
|
16
23
|
# register the extra and build the functions
|
17
24
|
def self.register_extra(verb, function, past:, validator:, options: nil)
|
18
25
|
past ||= verb
|
@@ -1,4 +1,9 @@
|
|
1
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.
|
2
7
|
class ExtraBuilder
|
3
8
|
|
4
9
|
# store the given options
|
@@ -1,4 +1,10 @@
|
|
1
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.
|
2
8
|
class HookBuilder
|
3
9
|
|
4
10
|
# store the given options
|
@@ -21,7 +27,7 @@ module AttributeExtras
|
|
21
27
|
<<-RUBY
|
22
28
|
def #{@verb}_attributes(*attributes, validator: true, writer: true)
|
23
29
|
if self.table_exists? && (non_attributes = attributes.map(&:to_s) - self.column_names).any?
|
24
|
-
|
30
|
+
AttributeExtras.logger.warn("Invalid attributes passed to #{@verb}_attributes: \#{non_attributes.join(', ')}")
|
25
31
|
end
|
26
32
|
|
27
33
|
include ::AttributeExtras::#{@verb.capitalize}Attributes
|
@@ -3,8 +3,13 @@ module AttributeExtras
|
|
3
3
|
# a container for an attribute that has been modified
|
4
4
|
class Modifier
|
5
5
|
|
6
|
-
|
6
|
+
# the attribute this modifier represents
|
7
|
+
attr_reader :attribute
|
7
8
|
|
9
|
+
# the set of options generated for this attribute
|
10
|
+
attr_reader :options
|
11
|
+
|
12
|
+
# store the given options
|
8
13
|
def initialize(attribute, options = {})
|
9
14
|
@attribute = attribute
|
10
15
|
@options = options
|
@@ -2,10 +2,9 @@ require 'test_helper'
|
|
2
2
|
|
3
3
|
class BaseExtensionsTest < ActiveSupport::TestCase
|
4
4
|
|
5
|
-
def
|
6
|
-
|
7
|
-
|
8
|
-
assert_not klass.respond_to?(:nullified_attributes)
|
5
|
+
def test_nullify_attributes_warns
|
6
|
+
assert_output_matches 'Invalid attributes' do
|
7
|
+
address_class(:nullify_attributes, :first_line, :second_line, :third_line)
|
9
8
|
end
|
10
9
|
end
|
11
10
|
|
@@ -14,10 +13,9 @@ class BaseExtensionsTest < ActiveSupport::TestCase
|
|
14
13
|
assert_equal klass.nullified_attributes.map(&:attribute), [:first_line, :second_line]
|
15
14
|
end
|
16
15
|
|
17
|
-
def
|
18
|
-
|
19
|
-
|
20
|
-
assert_not klass.respond_to?(:stripped_attributes)
|
16
|
+
def test_strip_attributes_warns
|
17
|
+
assert_output_matches 'Invalid attributes' do
|
18
|
+
address_class(:strip_attributes, :first_line, :second_line, :third_line)
|
21
19
|
end
|
22
20
|
end
|
23
21
|
|
@@ -26,13 +24,6 @@ class BaseExtensionsTest < ActiveSupport::TestCase
|
|
26
24
|
assert_equal klass.stripped_attributes.map(&:attribute), [:first_line, :second_line]
|
27
25
|
end
|
28
26
|
|
29
|
-
def test_truncate_attributes_failure
|
30
|
-
assert_raises ArgumentError do
|
31
|
-
klass = address_class(:truncate_attributes, :first_line, :second_line, :third_line)
|
32
|
-
assert_not klass.respond_to?(:truncated_attributes)
|
33
|
-
end
|
34
|
-
end
|
35
|
-
|
36
27
|
def test_truncate_attributes_success
|
37
28
|
klass = address_class(:truncate_attributes, :first_line, :second_line)
|
38
29
|
assert_equal klass.truncated_attributes.map(&:attribute), [:first_line, :second_line]
|
@@ -48,4 +39,14 @@ class BaseExtensionsTest < ActiveSupport::TestCase
|
|
48
39
|
end
|
49
40
|
end
|
50
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
|
51
52
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: attribute_extras
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Kevin Deisz
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-09-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -24,6 +24,20 @@ dependencies:
|
|
24
24
|
- - ">"
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '3'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: activesupport
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '3'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '3'
|
27
41
|
- !ruby/object:Gem::Dependency
|
28
42
|
name: sqlite3
|
29
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -92,3 +106,4 @@ test_files:
|
|
92
106
|
- test/test_classes.rb
|
93
107
|
- test/test_helper.rb
|
94
108
|
- test/truncate_attributes_test.rb
|
109
|
+
has_rdoc:
|