attr_extras 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.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/README.md +35 -0
- data/Rakefile +9 -0
- data/attr_extras.gemspec +20 -0
- data/lib/attr_extras.rb +24 -0
- data/lib/attr_extras/version.rb +3 -0
- data/spec/attr_extras_spec.rb +28 -0
- metadata +55 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
# attr\_extras
|
2
|
+
|
3
|
+
Takes some of the boilerplate out of some common Ruby patterns and complements `attr` (`attr_accessor`), `attr_reader` and `attr_writer` nicely.
|
4
|
+
|
5
|
+
Lets you do:
|
6
|
+
|
7
|
+
``` ruby
|
8
|
+
class MyClass
|
9
|
+
attr_init :foo, :bar
|
10
|
+
attr_reader_private :foo
|
11
|
+
|
12
|
+
def oof
|
13
|
+
foo.reverse # Uses the private method.
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
x = MyClass.new("Foo!", "Bar!")
|
18
|
+
x.oof # => "!ooF"
|
19
|
+
```
|
20
|
+
|
21
|
+
## Installation
|
22
|
+
|
23
|
+
Add this line to your application's `Gemfile`:
|
24
|
+
|
25
|
+
gem 'attr_extras'
|
26
|
+
|
27
|
+
And then execute:
|
28
|
+
|
29
|
+
bundle
|
30
|
+
|
31
|
+
Or install it yourself as:
|
32
|
+
|
33
|
+
gem install attr_extras
|
34
|
+
|
35
|
+
## License
|
data/Rakefile
ADDED
data/attr_extras.gemspec
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/attr_extras/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Henrik Nyh"]
|
6
|
+
gem.email = ["henrik@nyh.se"]
|
7
|
+
gem.summary = %q{Adds attr_init and attr_reader_private.}
|
8
|
+
gem.homepage = "https://github.com/barsoom/attr_extras"
|
9
|
+
|
10
|
+
gem.files = `git ls-files`.split($\)
|
11
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
12
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
13
|
+
gem.name = "attr_extras"
|
14
|
+
gem.require_paths = ["lib"]
|
15
|
+
gem.version = AttrExtras::VERSION
|
16
|
+
|
17
|
+
if RUBY_VERSION < "1.9.3"
|
18
|
+
gem.add_development_dependency "minitest"
|
19
|
+
end
|
20
|
+
end
|
data/lib/attr_extras.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
require "attr_extras/version"
|
2
|
+
|
3
|
+
module AttrExtras
|
4
|
+
module ClassMethods
|
5
|
+
def attr_init(*keys)
|
6
|
+
define_method(:initialize) do |*values|
|
7
|
+
unless values.length == keys.length
|
8
|
+
raise ArgumentError, "wrong number of arguments (#{values.length} for #{keys.length})"
|
9
|
+
end
|
10
|
+
|
11
|
+
keys.zip(values).each do |k, v|
|
12
|
+
instance_variable_set("@#{k}", v)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def attr_reader_private(*keys)
|
18
|
+
attr_reader *keys
|
19
|
+
private *keys
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
Object.extend AttrExtras::ClassMethods
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require "minitest/autorun"
|
2
|
+
require "attr_extras"
|
3
|
+
|
4
|
+
class Example
|
5
|
+
attr_init :foo, :bar
|
6
|
+
attr_reader_private :foo, :bar
|
7
|
+
end
|
8
|
+
|
9
|
+
describe Object, ".attr_init" do
|
10
|
+
it "creates an initializer setting those instance variables" do
|
11
|
+
example = Example.new("Foo", "Bar")
|
12
|
+
example.instance_variable_get("@foo").must_equal "Foo"
|
13
|
+
example.instance_variable_get("@bar").must_equal "Bar"
|
14
|
+
end
|
15
|
+
|
16
|
+
it "requires all arguments" do
|
17
|
+
lambda { Example.new("Foo") }.must_raise ArgumentError
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
describe Object, ".attr_reader_private" do
|
22
|
+
it "creates private readers" do
|
23
|
+
example = Example.new("Foo", "Bar")
|
24
|
+
example.send(:foo).must_equal "Foo"
|
25
|
+
example.send(:bar).must_equal "Bar"
|
26
|
+
lambda { example.foo }.must_raise NoMethodError
|
27
|
+
end
|
28
|
+
end
|
metadata
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: attr_extras
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Henrik Nyh
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-10-16 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description:
|
15
|
+
email:
|
16
|
+
- henrik@nyh.se
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- .gitignore
|
22
|
+
- Gemfile
|
23
|
+
- README.md
|
24
|
+
- Rakefile
|
25
|
+
- attr_extras.gemspec
|
26
|
+
- lib/attr_extras.rb
|
27
|
+
- lib/attr_extras/version.rb
|
28
|
+
- spec/attr_extras_spec.rb
|
29
|
+
homepage: https://github.com/barsoom/attr_extras
|
30
|
+
licenses: []
|
31
|
+
post_install_message:
|
32
|
+
rdoc_options: []
|
33
|
+
require_paths:
|
34
|
+
- lib
|
35
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
36
|
+
none: false
|
37
|
+
requirements:
|
38
|
+
- - ! '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
42
|
+
none: false
|
43
|
+
requirements:
|
44
|
+
- - ! '>='
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '0'
|
47
|
+
requirements: []
|
48
|
+
rubyforge_project:
|
49
|
+
rubygems_version: 1.8.5
|
50
|
+
signing_key:
|
51
|
+
specification_version: 3
|
52
|
+
summary: Adds attr_init and attr_reader_private.
|
53
|
+
test_files:
|
54
|
+
- spec/attr_extras_spec.rb
|
55
|
+
has_rdoc:
|