extensible_attr_accessor 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.rspec +2 -0
- data/.travis.yml +3 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/LICENSE.txt +21 -0
- data/README.md +72 -0
- data/Rakefile +1 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/extensible_attr_accessor.gemspec +25 -0
- data/lib/extensible_attr_accessor.rb +3 -0
- data/lib/extensible_attr_accessor/extension/module.rb +4 -0
- data/lib/extensible_attr_accessor/extension/module/attr_accessor.rb +28 -0
- data/lib/extensible_attr_accessor/extension/module/attr_reader.rb +20 -0
- data/lib/extensible_attr_accessor/extension/module/attr_writer.rb +23 -0
- data/lib/extensible_attr_accessor/version.rb +3 -0
- metadata +104 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: b49dfda02581e3da16e1911c725f38f42b9ecf01
|
4
|
+
data.tar.gz: 22684353b246d3b36f136a8fd0069e00973b2de2
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 4395fc875910958d0530491fe5198b279336afa3c3196b640773f1391b1b6f7bfe65b9a652664c5089095097960849565a60a579e8c0d254b220a81e15f4a60e
|
7
|
+
data.tar.gz: f6d1159558f28de22236ce98c9461302324391623c9d2c3cc20c98c0fea5a79dfbb89c160279263885c090b904f337dd35f8964aeb02374ae766c26e33f2537d
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 jkr2255
|
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 all
|
13
|
+
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 THE
|
21
|
+
SOFTWARE.
|
22
|
+
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Jkr2255
|
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
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
# ExtensibleAttrAccessor
|
2
|
+
|
3
|
+
Allow `attr_accessor`, `attr_reader`, `attr_writer` to accept blocks.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'extensible_attr_accessor'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install extensible_attr_accessor
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
`attr_xxx` can make multiple accessor at one line, but they cannot be easily modified.
|
24
|
+
|
25
|
+
With this gem, `attr_accessor`, `attr_reader`, `attr_writer` can have a block, and common modification can be applied to multiple accessors.
|
26
|
+
|
27
|
+
Without a block, all metamethods call original ones, so this gem causes no impact for existing code.
|
28
|
+
|
29
|
+
### Module#attr_reader
|
30
|
+
When `attr_reader` is used with a block, the block is called when generated methods are called with one parameter (raw value of corresonding instance variable).
|
31
|
+
|
32
|
+
The return values of generated methods are the ones from block.
|
33
|
+
|
34
|
+
```ruby
|
35
|
+
#define getters converting to string
|
36
|
+
attr_reader(:foo, :bar){|val| val.to_s }
|
37
|
+
|
38
|
+
#shorthand
|
39
|
+
attr_reader :foo, :bar, &:to_s
|
40
|
+
```
|
41
|
+
|
42
|
+
### Module#attr_writer
|
43
|
+
When `attr_writer` is used with a block, the block is called when generated methods are called with one parameter (the input parameter of setter method).
|
44
|
+
|
45
|
+
The return value of the block is stored by the setter.
|
46
|
+
|
47
|
+
```ruby
|
48
|
+
#define setters always capitalizing
|
49
|
+
attr_writer(:foo, :bar){|val| val.capitalize }
|
50
|
+
|
51
|
+
#shorthand
|
52
|
+
attr_writer :foo, :bar, &:capitalize
|
53
|
+
```
|
54
|
+
|
55
|
+
### Module#attr_accessor
|
56
|
+
Doing above two functionalities in one `attr_accessor`. The block is called with **two** arguments, the first one is value, next one is operation mode (`:get` or `:set`).
|
57
|
+
|
58
|
+
I wonder where this method is useful, but made this for completeness.
|
59
|
+
|
60
|
+
## Development
|
61
|
+
|
62
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
|
63
|
+
|
64
|
+
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` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
65
|
+
|
66
|
+
## Contributing
|
67
|
+
|
68
|
+
1. Fork it ( https://github.com/jkr2255/extensible_attr_accessor/fork )
|
69
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
70
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
71
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
72
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "extensible_attr_accessor"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'extensible_attr_accessor/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "extensible_attr_accessor"
|
8
|
+
spec.version = ExtensibleAttrAccessor::VERSION
|
9
|
+
spec.authors = ["Jkr2255"]
|
10
|
+
spec.email = ["magnesium.oxide.play@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{attr_xxx extension accepting blocks.}
|
13
|
+
spec.homepage = "https://github.com/jkr2255/extensible_attr_accessor"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
18
|
+
spec.bindir = "exe"
|
19
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
|
+
spec.require_paths = ["lib"]
|
21
|
+
|
22
|
+
spec.add_development_dependency "bundler", "~> 1.9"
|
23
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
24
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
25
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
#encoding:utf-8
|
2
|
+
|
3
|
+
class Module
|
4
|
+
private
|
5
|
+
alias_method :inextensible_attr_accessor, :attr_accessor
|
6
|
+
def attr_accessor(*args, &block)
|
7
|
+
unless block_given?
|
8
|
+
inextensible_attr_accessor *args
|
9
|
+
return
|
10
|
+
end
|
11
|
+
#with block
|
12
|
+
args.each do |name|
|
13
|
+
define_method name do
|
14
|
+
raw_val = instance_variable_get "@#{name}"
|
15
|
+
block.call raw_val, :get
|
16
|
+
end
|
17
|
+
define_method "#{name}=" do |val|
|
18
|
+
parsed_val = block.call val, :set
|
19
|
+
|
20
|
+
instance_variable_set "@#{name}", parsed_val
|
21
|
+
|
22
|
+
val
|
23
|
+
|
24
|
+
end
|
25
|
+
end
|
26
|
+
nil
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
#encoding:utf-8
|
2
|
+
|
3
|
+
class Module
|
4
|
+
private
|
5
|
+
alias_method :inextensible_attr_reader, :attr_reader
|
6
|
+
def attr_reader(*args, &block)
|
7
|
+
unless block_given?
|
8
|
+
inextensible_attr_reader *args
|
9
|
+
return
|
10
|
+
end
|
11
|
+
#with block
|
12
|
+
args.each do |name|
|
13
|
+
define_method name do
|
14
|
+
raw_val = instance_variable_get "@#{name}"
|
15
|
+
block.call raw_val
|
16
|
+
end
|
17
|
+
end
|
18
|
+
nil
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
#encoding:utf-8
|
2
|
+
|
3
|
+
class Module
|
4
|
+
private
|
5
|
+
alias_method :inextensible_attr_writer, :attr_writer
|
6
|
+
def attr_writer(*args, &block)
|
7
|
+
unless block_given?
|
8
|
+
inextensible_attr_writer *args
|
9
|
+
return
|
10
|
+
end
|
11
|
+
#with block
|
12
|
+
args.each do |name|
|
13
|
+
define_method "#{name}=" do |val|
|
14
|
+
parsed_val = block.call val
|
15
|
+
|
16
|
+
instance_variable_set "@#{name}", parsed_val
|
17
|
+
|
18
|
+
val
|
19
|
+
end
|
20
|
+
end
|
21
|
+
nil
|
22
|
+
end
|
23
|
+
end
|
metadata
ADDED
@@ -0,0 +1,104 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: extensible_attr_accessor
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jkr2255
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-05-24 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.9'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.9'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ~>
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ~>
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ~>
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
description:
|
56
|
+
email:
|
57
|
+
- magnesium.oxide.play@gmail.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- .gitignore
|
63
|
+
- .rspec
|
64
|
+
- .travis.yml
|
65
|
+
- Gemfile
|
66
|
+
- LICENSE
|
67
|
+
- LICENSE.txt
|
68
|
+
- README.md
|
69
|
+
- Rakefile
|
70
|
+
- bin/console
|
71
|
+
- bin/setup
|
72
|
+
- extensible_attr_accessor.gemspec
|
73
|
+
- lib/extensible_attr_accessor.rb
|
74
|
+
- lib/extensible_attr_accessor/extension/module.rb
|
75
|
+
- lib/extensible_attr_accessor/extension/module/attr_accessor.rb
|
76
|
+
- lib/extensible_attr_accessor/extension/module/attr_reader.rb
|
77
|
+
- lib/extensible_attr_accessor/extension/module/attr_writer.rb
|
78
|
+
- lib/extensible_attr_accessor/version.rb
|
79
|
+
homepage: https://github.com/jkr2255/extensible_attr_accessor
|
80
|
+
licenses:
|
81
|
+
- MIT
|
82
|
+
metadata: {}
|
83
|
+
post_install_message:
|
84
|
+
rdoc_options: []
|
85
|
+
require_paths:
|
86
|
+
- lib
|
87
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
88
|
+
requirements:
|
89
|
+
- - '>='
|
90
|
+
- !ruby/object:Gem::Version
|
91
|
+
version: '0'
|
92
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - '>='
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
requirements: []
|
98
|
+
rubyforge_project:
|
99
|
+
rubygems_version: 2.4.6
|
100
|
+
signing_key:
|
101
|
+
specification_version: 4
|
102
|
+
summary: attr_xxx extension accepting blocks.
|
103
|
+
test_files: []
|
104
|
+
has_rdoc:
|