attr_delegated 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +22 -0
- data/.pryrc +29 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +29 -0
- data/Rakefile +2 -0
- data/attr_delegated.gemspec +28 -0
- data/lib/attr_delegated.rb +21 -0
- data/lib/attr_delegated/options.rb +150 -0
- data/lib/attr_delegated/version.rb +3 -0
- metadata +118 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 4df2326eae33ba4f3b3fa5ac8c2e6c849171d5d0
|
4
|
+
data.tar.gz: 7df9c505db00ff38cccbaa5197188f245c342b09
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 5a88e90935e2a913bda6c6615f6ebca9f3d31373bde076469e80a1a1520a16d1087f7d40141771730f8587379b64e3df6218148a3bdc2af0b4862b3eda037a5a
|
7
|
+
data.tar.gz: b9a7ced03a76304447ed856210221e31bbf71c3597961e362388e62c4e537e9e8a758ead861334665f209b89bd772f2f8c1854bfebfce49fb15dd6109b0406f2
|
data/.gitignore
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
.bundle
|
4
|
+
.config
|
5
|
+
.yardoc
|
6
|
+
Gemfile.lock
|
7
|
+
InstalledFiles
|
8
|
+
_yardoc
|
9
|
+
coverage
|
10
|
+
doc/
|
11
|
+
lib/bundler/man
|
12
|
+
pkg
|
13
|
+
rdoc
|
14
|
+
spec/reports
|
15
|
+
test/tmp
|
16
|
+
test/version_tmp
|
17
|
+
tmp
|
18
|
+
*.bundle
|
19
|
+
*.so
|
20
|
+
*.o
|
21
|
+
*.a
|
22
|
+
mkmf.log
|
data/.pryrc
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
lib = File.expand_path('../lib', __FILE__)
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
|
4
|
+
require 'attr_delegated'
|
5
|
+
|
6
|
+
class Bar
|
7
|
+
attr_accessor :baz, :quux
|
8
|
+
|
9
|
+
attr_reader :value
|
10
|
+
|
11
|
+
def initialize(value = 9000)
|
12
|
+
@value = value
|
13
|
+
|
14
|
+
@baz = value.to_s * 2
|
15
|
+
@quux = value.to_s * 3
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
class Foo
|
20
|
+
extend AttrDelegated
|
21
|
+
|
22
|
+
attr_delegated :baz, :quux, to: :bar
|
23
|
+
|
24
|
+
attr_reader :bar
|
25
|
+
|
26
|
+
def initialize(new_bar = 9000)
|
27
|
+
@bar = Bar.new new_bar
|
28
|
+
end
|
29
|
+
end
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Alexa Grey
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# AttrDelegated
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'attr_delegated'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install attr_delegated
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
TODO: Write usage instructions here
|
22
|
+
|
23
|
+
## Contributing
|
24
|
+
|
25
|
+
1. Fork it ( https://github.com/[my-github-username]/attr_delegated/fork )
|
26
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
28
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'attr_delegated/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "attr_delegated"
|
8
|
+
spec.version = AttrDelegated::VERSION
|
9
|
+
spec.authors = ["Alexa Grey"]
|
10
|
+
spec.email = ["devel@mouse.vc"]
|
11
|
+
spec.summary = %q{Delegate an ActiveModel attribute and all its meta methods to another model.}
|
12
|
+
spec.description = %q{Delegate an ActiveModel attribute and all its meta methods to another model.}
|
13
|
+
spec.homepage = "https://github.com/scryptmouse/attr_delegated"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.required_ruby_version = '~> 2.0'
|
22
|
+
|
23
|
+
spec.add_dependency "activesupport", ">= 3.2.0", "< 5"
|
24
|
+
|
25
|
+
spec.add_development_dependency "bundler", "~> 1.6"
|
26
|
+
spec.add_development_dependency "rake"
|
27
|
+
spec.add_development_dependency "pry"
|
28
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'active_support/all'
|
2
|
+
require 'attr_delegated/options'
|
3
|
+
require 'attr_delegated/version'
|
4
|
+
|
5
|
+
module AttrDelegated
|
6
|
+
# Delegate an ActiveModel attribute and all its meta methods to another model,
|
7
|
+
# e.g. in addition to the reader: the writer, predicate, and "changed?" methods as well.
|
8
|
+
#
|
9
|
+
# @param [Array] attrs
|
10
|
+
# @param [Hash] options (see AttrDelegated::Options#initialize)
|
11
|
+
# @return [void]
|
12
|
+
def attr_delegated(*attrs, **options)
|
13
|
+
opts = AttrDelegated::Options.new attrs, options
|
14
|
+
|
15
|
+
delegate *opts
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
ActiveSupport.on_load(:active_record) do
|
20
|
+
extend AttrDelegated
|
21
|
+
end
|
@@ -0,0 +1,150 @@
|
|
1
|
+
module AttrDelegated
|
2
|
+
class Options
|
3
|
+
METHODS = {
|
4
|
+
reader: "%s",
|
5
|
+
writer: "%s=",
|
6
|
+
predicate: "%s?",
|
7
|
+
type_cast: "%s_before_type_cast",
|
8
|
+
dirty: %w[
|
9
|
+
%s_change
|
10
|
+
%s_changed?
|
11
|
+
%s_will_change!
|
12
|
+
%s_was
|
13
|
+
reset_%s!
|
14
|
+
]
|
15
|
+
}
|
16
|
+
|
17
|
+
GROUPS = METHODS.keys
|
18
|
+
|
19
|
+
DELEGATE_OPTION_KEYS = %i[to allow_nil prefix]
|
20
|
+
|
21
|
+
DEFAULTS = Hash[GROUPS.zip([true] * GROUPS.length)]
|
22
|
+
|
23
|
+
# @!attribute [r] attributes
|
24
|
+
# @api private
|
25
|
+
# @return [Array<String, Symbol>]
|
26
|
+
attr_reader :attributes
|
27
|
+
|
28
|
+
# @!attribute [r] raw_options
|
29
|
+
# @api private
|
30
|
+
# @return [Hash]
|
31
|
+
attr_reader :raw_options
|
32
|
+
|
33
|
+
UNIQUE_KEYS = %i[only skip]
|
34
|
+
|
35
|
+
# @param [Array<String, Symbol>] attributes
|
36
|
+
# @param [Hash] raw_options
|
37
|
+
# @option raw_options [Symbol, String] :to **Must be set!** Passed to delegate
|
38
|
+
# @option raw_options [Boolean] :allow_nil Passed to `delegate`
|
39
|
+
# @option raw_options [Boolean, String, Symbol] :prefix Passed to `delegate`
|
40
|
+
# @option raw_options [Boolean] :reader (true) Whether to delegate the reader
|
41
|
+
# @option raw_options [Boolean] :writer (true) Whether to delegate the writer
|
42
|
+
# @option raw_options [Boolean] :predicate (true) Whether to delegate the predicate
|
43
|
+
# @option raw_options [Boolean] :type_cast (true) Whether to delegate the `before_type_cast` method
|
44
|
+
# @option raw_options [Boolean] :dirty (true) Whether to delegate the "dirty" methods
|
45
|
+
# @option raw_options [Array, Symbol] :only Can delegate only certain methods, incompatible with `:without`
|
46
|
+
# @option raw_options [Array, Symbol] :skip Can skip delegate only certain methods
|
47
|
+
def initialize(attributes, raw_options = {})
|
48
|
+
@attributes = Array( attributes )
|
49
|
+
@raw_options = raw_options.reverse_merge DEFAULTS
|
50
|
+
|
51
|
+
check_for_attributes!
|
52
|
+
check_for_to!
|
53
|
+
check_for_unique_key_conflict!
|
54
|
+
end
|
55
|
+
|
56
|
+
# @!attribute [r] delegated_methods
|
57
|
+
# @return [Array<Symbol>]
|
58
|
+
def delegated_methods
|
59
|
+
@delegated_methods ||= attributes.flat_map do |attribute|
|
60
|
+
delegated_method_formats.map do |format|
|
61
|
+
format % attribute
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
def to_a
|
67
|
+
[*delegated_methods, options_for_delegate]
|
68
|
+
end
|
69
|
+
|
70
|
+
private
|
71
|
+
# @raise [ArgumentError] unless 1 or more attributes provided
|
72
|
+
# @return [void]
|
73
|
+
def check_for_attributes!
|
74
|
+
raise ArgumentError, "must provide attributes to delegate!" if attributes.none?
|
75
|
+
end
|
76
|
+
|
77
|
+
# @raise [ArgumentError] unless option `:to` is set
|
78
|
+
# @return [void]
|
79
|
+
def check_for_to!
|
80
|
+
raise ArgumentError, "must define `:to` to delegate!" unless option? :to
|
81
|
+
end
|
82
|
+
|
83
|
+
# @raise [ArgumentError] if `:only` and `:skip` are both set
|
84
|
+
# @return [void]
|
85
|
+
def check_for_unique_key_conflict!
|
86
|
+
if unique_keys.many?
|
87
|
+
quoted_unique_keys = unique_keys.map { |k| "`%s`" % k }.to_sentence
|
88
|
+
|
89
|
+
raise ArgumentError, "cannot set #{quoted_unique_keys} at the same time"
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
# @return [Array<String>]
|
94
|
+
def delegated_method_formats
|
95
|
+
@delegated_method_formats ||= METHODS.values_at(*delegates).flatten
|
96
|
+
end
|
97
|
+
|
98
|
+
# @!attribute [r] delegates
|
99
|
+
# @api private
|
100
|
+
# @return [Array<Symbol>]
|
101
|
+
def delegates
|
102
|
+
@delegates ||= detect_delegates!
|
103
|
+
end
|
104
|
+
|
105
|
+
# @return [Array]
|
106
|
+
def detect_delegates!
|
107
|
+
case unique_key
|
108
|
+
when :only then GROUPS & unique_key_options
|
109
|
+
when :skip then GROUPS - unique_key_options
|
110
|
+
else
|
111
|
+
GROUPS.select { |group| option? group }
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
# @return [Boolean]
|
116
|
+
def option?(key)
|
117
|
+
raw_options.key?(key) && raw_options[key].presence
|
118
|
+
end
|
119
|
+
|
120
|
+
# @!attribute [r] options_for_delegate
|
121
|
+
# @api private
|
122
|
+
# @return [Hash]
|
123
|
+
def options_for_delegate
|
124
|
+
raw_options.slice *DELEGATE_OPTION_KEYS
|
125
|
+
end
|
126
|
+
|
127
|
+
# @!attribute [r] unique_key_options
|
128
|
+
# @api private
|
129
|
+
# @return [Array<Symbol>]
|
130
|
+
def unique_key_options
|
131
|
+
@unique_key_options ||= Array(raw_options[unique_key])
|
132
|
+
end
|
133
|
+
|
134
|
+
# @!attribute [r] unique_keys
|
135
|
+
# @api private
|
136
|
+
# @return [Array<Symbol>]
|
137
|
+
def unique_keys
|
138
|
+
@unique_keys ||= raw_options.keys & UNIQUE_KEYS
|
139
|
+
end
|
140
|
+
|
141
|
+
# @!attribute [r] unique_key
|
142
|
+
# @api private
|
143
|
+
# @return [Symbol, nil]
|
144
|
+
def unique_key
|
145
|
+
@unique_key = unique_keys.first unless defined?(@unique_key)
|
146
|
+
|
147
|
+
@unique_key
|
148
|
+
end
|
149
|
+
end
|
150
|
+
end
|
metadata
ADDED
@@ -0,0 +1,118 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: attr_delegated
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Alexa Grey
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-10-05 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activesupport
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - '>='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 3.2.0
|
20
|
+
- - <
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: '5'
|
23
|
+
type: :runtime
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
26
|
+
requirements:
|
27
|
+
- - '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 3.2.0
|
30
|
+
- - <
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '5'
|
33
|
+
- !ruby/object:Gem::Dependency
|
34
|
+
name: bundler
|
35
|
+
requirement: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - ~>
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '1.6'
|
40
|
+
type: :development
|
41
|
+
prerelease: false
|
42
|
+
version_requirements: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - ~>
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '1.6'
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: rake
|
49
|
+
requirement: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - '>='
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: '0'
|
61
|
+
- !ruby/object:Gem::Dependency
|
62
|
+
name: pry
|
63
|
+
requirement: !ruby/object:Gem::Requirement
|
64
|
+
requirements:
|
65
|
+
- - '>='
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: '0'
|
68
|
+
type: :development
|
69
|
+
prerelease: false
|
70
|
+
version_requirements: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - '>='
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: '0'
|
75
|
+
description: Delegate an ActiveModel attribute and all its meta methods to another
|
76
|
+
model.
|
77
|
+
email:
|
78
|
+
- devel@mouse.vc
|
79
|
+
executables: []
|
80
|
+
extensions: []
|
81
|
+
extra_rdoc_files: []
|
82
|
+
files:
|
83
|
+
- .gitignore
|
84
|
+
- .pryrc
|
85
|
+
- Gemfile
|
86
|
+
- LICENSE.txt
|
87
|
+
- README.md
|
88
|
+
- Rakefile
|
89
|
+
- attr_delegated.gemspec
|
90
|
+
- lib/attr_delegated.rb
|
91
|
+
- lib/attr_delegated/options.rb
|
92
|
+
- lib/attr_delegated/version.rb
|
93
|
+
homepage: https://github.com/scryptmouse/attr_delegated
|
94
|
+
licenses:
|
95
|
+
- MIT
|
96
|
+
metadata: {}
|
97
|
+
post_install_message:
|
98
|
+
rdoc_options: []
|
99
|
+
require_paths:
|
100
|
+
- lib
|
101
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
102
|
+
requirements:
|
103
|
+
- - ~>
|
104
|
+
- !ruby/object:Gem::Version
|
105
|
+
version: '2.0'
|
106
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - '>='
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
requirements: []
|
112
|
+
rubyforge_project:
|
113
|
+
rubygems_version: 2.2.2
|
114
|
+
signing_key:
|
115
|
+
specification_version: 4
|
116
|
+
summary: Delegate an ActiveModel attribute and all its meta methods to another model.
|
117
|
+
test_files: []
|
118
|
+
has_rdoc:
|