attr_extras 0.0.4 → 0.0.5
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/.rvmrc +1 -0
- data/.travis.yml +11 -0
- data/README.md +13 -2
- data/attr_extras.gemspec +3 -0
- data/lib/attr_extras/version.rb +1 -1
- data/lib/attr_extras.rb +12 -0
- data/spec/attr_extras_spec.rb +18 -0
- metadata +23 -5
data/.rvmrc
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
rvm ruby-1.9.3-p0-patched@attr_extras --create
|
data/.travis.yml
ADDED
data/README.md
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
[](https://travis-ci.org/#!/barsoom/attr_extras/builds)
|
2
|
+
|
1
3
|
# attr\_extras
|
2
4
|
|
3
5
|
Takes some boilerplate out of Ruby and complements `attr_accessor`, `attr_reader` and `attr_writer` nicely by providing:
|
@@ -8,6 +10,9 @@ Defines an initializer that takes two arguments and assigns `@foo` and `@bar`.
|
|
8
10
|
`attr_private :foo, :bar`<br>
|
9
11
|
Defines private readers for `@foo` and `@bar`.
|
10
12
|
|
13
|
+
`attr_id_query :foo?, :bar?`<br>
|
14
|
+
Defines query methods like `foo?` which is true iff `foo_id` is truthy. Goes well with Active Record.
|
15
|
+
|
11
16
|
|
12
17
|
## Example
|
13
18
|
|
@@ -15,15 +20,21 @@ Defines private readers for `@foo` and `@bar`.
|
|
15
20
|
class MyClass
|
16
21
|
attr_initialize :foo, :bar
|
17
22
|
attr_private :foo
|
23
|
+
attr_id_query :item?
|
18
24
|
|
19
25
|
def oof
|
20
26
|
foo.reverse
|
21
27
|
end
|
28
|
+
|
29
|
+
def item_id
|
30
|
+
123
|
31
|
+
end
|
22
32
|
end
|
23
33
|
|
24
34
|
x = MyClass.new("Foo!", "Bar!")
|
25
|
-
x.oof
|
26
|
-
x.foo
|
35
|
+
x.oof # => "!ooF"
|
36
|
+
x.foo # NoMethodError: private method `foo' called.
|
37
|
+
x.item? # => true
|
27
38
|
```
|
28
39
|
|
29
40
|
|
data/attr_extras.gemspec
CHANGED
data/lib/attr_extras/version.rb
CHANGED
data/lib/attr_extras.rb
CHANGED
@@ -18,6 +18,18 @@ module AttrExtras
|
|
18
18
|
attr_reader *names
|
19
19
|
private *names
|
20
20
|
end
|
21
|
+
|
22
|
+
def attr_id_query(*names)
|
23
|
+
names.each do |name|
|
24
|
+
unless name.to_s.end_with?("?")
|
25
|
+
raise "#{__method__} wants `#{name}?`, not `#{name}`."
|
26
|
+
end
|
27
|
+
|
28
|
+
define_method(name) do # def foo?
|
29
|
+
!!send("#{name.to_s.chop}_id") # !!send("foo_id")
|
30
|
+
end # end
|
31
|
+
end
|
32
|
+
end
|
21
33
|
end
|
22
34
|
end
|
23
35
|
|
data/spec/attr_extras_spec.rb
CHANGED
@@ -6,6 +6,11 @@ class Example
|
|
6
6
|
attr_private :foo, :bar
|
7
7
|
end
|
8
8
|
|
9
|
+
class QueryExample
|
10
|
+
attr_id_query :baz?, :boink?
|
11
|
+
attr_accessor :baz_id
|
12
|
+
end
|
13
|
+
|
9
14
|
describe Object, ".attr_init" do
|
10
15
|
it "creates an initializer setting those instance variables" do
|
11
16
|
example = Example.new("Foo", "Bar")
|
@@ -26,3 +31,16 @@ describe Object, ".attr_private" do
|
|
26
31
|
lambda { example.foo }.must_raise NoMethodError
|
27
32
|
end
|
28
33
|
end
|
34
|
+
|
35
|
+
describe Object, ".attr_id_query" do
|
36
|
+
it "creates id query methods" do
|
37
|
+
example = QueryExample.new
|
38
|
+
refute example.baz?
|
39
|
+
example.baz_id = 123
|
40
|
+
assert example.baz?
|
41
|
+
end
|
42
|
+
|
43
|
+
it "requires a trailing questionmark" do
|
44
|
+
lambda { Object.attr_id_query(:foo) }.must_raise RuntimeError
|
45
|
+
end
|
46
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: attr_extras
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.5
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,8 +9,19 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-10-
|
13
|
-
dependencies:
|
12
|
+
date: 2012-10-24 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rake
|
16
|
+
requirement: &70296485671660 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70296485671660
|
14
25
|
description:
|
15
26
|
email:
|
16
27
|
- henrik@nyh.se
|
@@ -19,6 +30,8 @@ extensions: []
|
|
19
30
|
extra_rdoc_files: []
|
20
31
|
files:
|
21
32
|
- .gitignore
|
33
|
+
- .rvmrc
|
34
|
+
- .travis.yml
|
22
35
|
- Gemfile
|
23
36
|
- README.md
|
24
37
|
- Rakefile
|
@@ -38,18 +51,23 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
38
51
|
- - ! '>='
|
39
52
|
- !ruby/object:Gem::Version
|
40
53
|
version: '0'
|
54
|
+
segments:
|
55
|
+
- 0
|
56
|
+
hash: 2608832839125048119
|
41
57
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
42
58
|
none: false
|
43
59
|
requirements:
|
44
60
|
- - ! '>='
|
45
61
|
- !ruby/object:Gem::Version
|
46
62
|
version: '0'
|
63
|
+
segments:
|
64
|
+
- 0
|
65
|
+
hash: 2608832839125048119
|
47
66
|
requirements: []
|
48
67
|
rubyforge_project:
|
49
|
-
rubygems_version: 1.8.
|
68
|
+
rubygems_version: 1.8.10
|
50
69
|
signing_key:
|
51
70
|
specification_version: 3
|
52
71
|
summary: Adds attr_initialize and attr_private methods.
|
53
72
|
test_files:
|
54
73
|
- spec/attr_extras_spec.rb
|
55
|
-
has_rdoc:
|