attr_extras 1.1.0 → 1.2.0
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/README.md +14 -0
- data/lib/attr_extras.rb +12 -1
- data/lib/attr_extras/version.rb +1 -1
- data/spec/attr_extras_spec.rb +15 -0
- metadata +6 -6
data/README.md
CHANGED
|
@@ -13,6 +13,9 @@ Defines private readers for `@foo` and `@bar`.
|
|
|
13
13
|
`pattr_initialize :foo, :bar`<br>
|
|
14
14
|
Defines both initializer and private readers.
|
|
15
15
|
|
|
16
|
+
`method_object :fooable?, :foo`<br>
|
|
17
|
+
Defines a `.fooable?` class method that delegates to an instance method.
|
|
18
|
+
|
|
16
19
|
`attr_id_query :foo?, :bar?`<br>
|
|
17
20
|
Defines query methods like `foo?`, which is true iff `foo_id` is truthy. Goes well with Active Record.
|
|
18
21
|
|
|
@@ -42,6 +45,17 @@ x = MyClass.new("Foo!", "Bar!")
|
|
|
42
45
|
x.oof # => "!ooF"
|
|
43
46
|
x.foo # NoMethodError: private method `foo' called.
|
|
44
47
|
x.item? # => true
|
|
48
|
+
|
|
49
|
+
class MyMethodObject
|
|
50
|
+
method_object :fooable?, :foo
|
|
51
|
+
|
|
52
|
+
def fooable?
|
|
53
|
+
foo == :some_value
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
MyMethodObject.fooable?(:some_value) # => true
|
|
58
|
+
MyMethodObject.fooable?(:another_value) # => false
|
|
45
59
|
```
|
|
46
60
|
|
|
47
61
|
|
data/lib/attr_extras.rb
CHANGED
|
@@ -24,6 +24,15 @@ module AttrExtras
|
|
|
24
24
|
attr_private *names
|
|
25
25
|
end
|
|
26
26
|
|
|
27
|
+
def method_object(method_name, *names)
|
|
28
|
+
metaclass = (class << self; self; end)
|
|
29
|
+
metaclass.send(:define_method, method_name) do |*values|
|
|
30
|
+
new(*values).send(method_name)
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
pattr_initialize *names
|
|
34
|
+
end
|
|
35
|
+
|
|
27
36
|
def attr_id_query(*names)
|
|
28
37
|
names.each do |name|
|
|
29
38
|
name = name.to_s
|
|
@@ -38,4 +47,6 @@ module AttrExtras
|
|
|
38
47
|
end
|
|
39
48
|
end
|
|
40
49
|
|
|
41
|
-
|
|
50
|
+
class Class
|
|
51
|
+
include AttrExtras::ClassMethods
|
|
52
|
+
end
|
data/lib/attr_extras/version.rb
CHANGED
data/spec/attr_extras_spec.rb
CHANGED
|
@@ -15,6 +15,21 @@ class QueryExample
|
|
|
15
15
|
attr_accessor :baz_id
|
|
16
16
|
end
|
|
17
17
|
|
|
18
|
+
class MethodObjectExample
|
|
19
|
+
method_object :fooable?, :foo
|
|
20
|
+
|
|
21
|
+
def fooable?
|
|
22
|
+
foo
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
describe Object, ".method_object" do
|
|
27
|
+
it "creates a class method that instantiates and runs that instance method" do
|
|
28
|
+
assert MethodObjectExample.fooable?(true)
|
|
29
|
+
refute MethodObjectExample.fooable?(false)
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
|
|
18
33
|
describe Object, ".attr_initialize" do
|
|
19
34
|
it "creates an initializer setting those instance variables" do
|
|
20
35
|
example = Example.new("Foo", "Bar")
|
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: 1.
|
|
4
|
+
version: 1.2.0
|
|
5
5
|
prerelease:
|
|
6
6
|
platform: ruby
|
|
7
7
|
authors:
|
|
@@ -9,11 +9,11 @@ authors:
|
|
|
9
9
|
autorequire:
|
|
10
10
|
bindir: bin
|
|
11
11
|
cert_chain: []
|
|
12
|
-
date: 2013-01-
|
|
12
|
+
date: 2013-01-29 00:00:00.000000000 Z
|
|
13
13
|
dependencies:
|
|
14
14
|
- !ruby/object:Gem::Dependency
|
|
15
15
|
name: rake
|
|
16
|
-
requirement: &
|
|
16
|
+
requirement: &70346338947260 !ruby/object:Gem::Requirement
|
|
17
17
|
none: false
|
|
18
18
|
requirements:
|
|
19
19
|
- - ! '>='
|
|
@@ -21,7 +21,7 @@ dependencies:
|
|
|
21
21
|
version: '0'
|
|
22
22
|
type: :development
|
|
23
23
|
prerelease: false
|
|
24
|
-
version_requirements: *
|
|
24
|
+
version_requirements: *70346338947260
|
|
25
25
|
description:
|
|
26
26
|
email:
|
|
27
27
|
- henrik@nyh.se
|
|
@@ -53,7 +53,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
|
53
53
|
version: '0'
|
|
54
54
|
segments:
|
|
55
55
|
- 0
|
|
56
|
-
hash:
|
|
56
|
+
hash: 1416484778478969945
|
|
57
57
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
58
58
|
none: false
|
|
59
59
|
requirements:
|
|
@@ -62,7 +62,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
62
62
|
version: '0'
|
|
63
63
|
segments:
|
|
64
64
|
- 0
|
|
65
|
-
hash:
|
|
65
|
+
hash: 1416484778478969945
|
|
66
66
|
requirements: []
|
|
67
67
|
rubyforge_project:
|
|
68
68
|
rubygems_version: 1.8.10
|