attr_combined_accessor 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/README.md +75 -0
- data/lib/attr_combined_accessor.rb +30 -0
- metadata +130 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: f9b183dfe79b863da890b5c857135b080f1de490
|
4
|
+
data.tar.gz: da180374d5f84c694fe5b51873808b017bed703d
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: a9fbf605657f4d3eeb78a17941a32f2afdba7782ecbeed543df8e7334fb5481535a26bd8bb6be9feb9f91b4704669b0ffa851c0361b57bfa425b83bbd46ef08c
|
7
|
+
data.tar.gz: 33a33294a1e3039651fdfd85d87eeb5ec8860e02ca5cedf9baa629aaa03dca7e7b98c1c9311608f78a4a24ea907af7650a4244a72748136612731d0f0ad3a80b
|
data/README.md
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
# attr_combined_accessor [![Build Status](https://travis-ci.org/nathankleyn/attr_combined_accessor.svg?branch=master)](https://travis-ci.org/nathankleyn/attr_combined_accessor) [![Coverage Status](https://coveralls.io/repos/nathankleyn/attr_combined_accessor/badge.png?branch=master)](https://coveralls.io/r/nathankleyn/attr_combined_accessor?branch=master)
|
2
|
+
|
3
|
+
Combined reader/writer accessors in Ruby, ideal for chaining or DSLs.
|
4
|
+
|
5
|
+
## Installing
|
6
|
+
|
7
|
+
You can install this gem via RubyGems:
|
8
|
+
|
9
|
+
```sh
|
10
|
+
gem install attr_combined_accessor
|
11
|
+
```
|
12
|
+
|
13
|
+
## Using
|
14
|
+
|
15
|
+
Sometimes you really need to be able to set and get without the limitations of Ruby's default accessors and the equals sign. This is where attr_combined_accessor comes in!
|
16
|
+
|
17
|
+
```ruby
|
18
|
+
require 'attr_combined_accessor'
|
19
|
+
|
20
|
+
class MyAmazingClass
|
21
|
+
attr_combined_accessor :foo, :bar
|
22
|
+
end
|
23
|
+
```
|
24
|
+
|
25
|
+
And now you can use them:
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
my_amazing_class = MyAmazingClass.new
|
29
|
+
my_amazing_class.foo # => nil
|
30
|
+
my_amazing_class.foo('foo')
|
31
|
+
my_amazing_class.foo # => 'foo'
|
32
|
+
```
|
33
|
+
|
34
|
+
The attr_combined_accessor gem returns `self` from the accessor, which makes chaining setters possible:
|
35
|
+
|
36
|
+
```ruby
|
37
|
+
my_amazing_class = MyAmazingClass.new
|
38
|
+
my_amazing_class.foo('foo').bar('bar')
|
39
|
+
my_amazing_class.foo # => 'foo'
|
40
|
+
my_amazing_class.bar # => 'bar'
|
41
|
+
```
|
42
|
+
|
43
|
+
It also creates the standard `attr_writer` for the places where you want readability and don't need the DSL or chaining style:
|
44
|
+
|
45
|
+
```ruby
|
46
|
+
my_amazing_class = MyAmazingClass.new
|
47
|
+
my_amazing_class.foo = 'foo'
|
48
|
+
my_amazing_class.foo # => 'foo'
|
49
|
+
my_amazing_class.foo('notfoo!')
|
50
|
+
my_amazing_class.foo # => 'notfoo!'
|
51
|
+
```
|
52
|
+
|
53
|
+
## License
|
54
|
+
|
55
|
+
The MIT License (MIT)
|
56
|
+
|
57
|
+
Copyright (c) 2015 Nathan Kleyn
|
58
|
+
|
59
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
60
|
+
of this software and associated documentation files (the "Software"), to deal
|
61
|
+
in the Software without restriction, including without limitation the rights
|
62
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
63
|
+
copies of the Software, and to permit persons to whom the Software is
|
64
|
+
furnished to do so, subject to the following conditions:
|
65
|
+
|
66
|
+
The above copyright notice and this permission notice shall be included in
|
67
|
+
all copies or substantial portions of the Software.
|
68
|
+
|
69
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
70
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
71
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
72
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
73
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
74
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
75
|
+
THE SOFTWARE.
|
@@ -0,0 +1,30 @@
|
|
1
|
+
# Extend module with the additional accessor utility.
|
2
|
+
class Module
|
3
|
+
# Creates a combined accessor that allows getting and setting from the
|
4
|
+
# same endpoint. It will operate in getter mode if you don't pass any
|
5
|
+
# arguments when calling it, otherwise it will work in setter mode. The
|
6
|
+
# setter mode returns self. This is really super useful when needing to
|
7
|
+
# chain methods (you can't chain standard attr_writer methods because
|
8
|
+
# of the `= something` part) or when trying to create a nice looking DSL.
|
9
|
+
#
|
10
|
+
# We also create the usual writer though so you can use both.
|
11
|
+
def attr_combined_accessor(*syms)
|
12
|
+
syms.each do |sym|
|
13
|
+
add_attr_combined_accessor(sym)
|
14
|
+
attr_writer(sym)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
def add_attr_combined_accessor(sym)
|
21
|
+
define_method(sym) do |*args|
|
22
|
+
if args.empty?
|
23
|
+
instance_variable_get(:"@#{sym}")
|
24
|
+
else
|
25
|
+
instance_variable_set(:"@#{sym}", *args)
|
26
|
+
self
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
metadata
ADDED
@@ -0,0 +1,130 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: attr_combined_accessor
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Nathan Kleyn
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-02-03 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: coveralls
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0.7'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0.7'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: filewatcher
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0.3'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0.3'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: pry-byebug
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '2.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '2.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '3.1'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '3.1'
|
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.28'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0.28'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rubocop-rspec
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '1.2'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '1.2'
|
97
|
+
description: See https://github.com/nathankleyn/attr_combined_accessor for more information!
|
98
|
+
email:
|
99
|
+
- nathan@nathankleyn.com
|
100
|
+
executables: []
|
101
|
+
extensions: []
|
102
|
+
extra_rdoc_files: []
|
103
|
+
files:
|
104
|
+
- README.md
|
105
|
+
- lib/attr_combined_accessor.rb
|
106
|
+
homepage: https://github.com/nathankleyn/attr_combined_accessor
|
107
|
+
licenses:
|
108
|
+
- MIT
|
109
|
+
metadata: {}
|
110
|
+
post_install_message:
|
111
|
+
rdoc_options: []
|
112
|
+
require_paths:
|
113
|
+
- lib
|
114
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
115
|
+
requirements:
|
116
|
+
- - ">="
|
117
|
+
- !ruby/object:Gem::Version
|
118
|
+
version: '0'
|
119
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
120
|
+
requirements:
|
121
|
+
- - ">="
|
122
|
+
- !ruby/object:Gem::Version
|
123
|
+
version: '0'
|
124
|
+
requirements: []
|
125
|
+
rubyforge_project:
|
126
|
+
rubygems_version: 2.4.5
|
127
|
+
signing_key:
|
128
|
+
specification_version: 4
|
129
|
+
summary: Combined reader/writer accessors in Ruby, ideal for chaining or DSLs.
|
130
|
+
test_files: []
|