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 ADDED
@@ -0,0 +1 @@
1
+ rvm ruby-1.9.3-p0-patched@attr_extras --create
data/.travis.yml ADDED
@@ -0,0 +1,11 @@
1
+ language: ruby
2
+ rvm:
3
+ # 1.9 rubies
4
+ - 1.9.3
5
+ - rbx-19mode
6
+ - jruby-19mode
7
+
8
+ # 1.8 rubies
9
+ - ree
10
+ - jruby-18mode
11
+ - rbx-18mode
data/README.md CHANGED
@@ -1,3 +1,5 @@
1
+ [![Build status](https://secure.travis-ci.org/barsoom/attr_extras.png)](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 # => "!ooF"
26
- x.foo # NoMethodError: private method `foo' called.
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
@@ -14,6 +14,9 @@ Gem::Specification.new do |gem|
14
14
  gem.require_paths = ["lib"]
15
15
  gem.version = AttrExtras::VERSION
16
16
 
17
+ # For Travis CI.
18
+ gem.add_development_dependency "rake"
19
+
17
20
  if RUBY_VERSION < "1.9.3"
18
21
  gem.add_development_dependency "minitest"
19
22
  end
@@ -1,3 +1,3 @@
1
1
  module AttrExtras
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  end
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
 
@@ -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
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-17 00:00:00.000000000 Z
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.5
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: