attr_extras 1.6.3 → 1.7.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.
- checksums.yaml +4 -4
- data/.travis.yml +2 -2
- data/README.md +12 -0
- data/lib/attr_extras.rb +5 -0
- data/lib/attr_extras/version.rb +1 -1
- data/script/test +8 -0
- data/spec/attr_extras_spec.rb +21 -0
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ffd8abe1675a431f0b748464aae94e5e45ff1305
|
4
|
+
data.tar.gz: eb248dbd2a6e75dec4e9fe44c9a999087bc9fcb9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cb1c750b44e30aa2f387175e57a259a48c2b1b5bafab3acd937da929cb62fa18519ed2b664d55fd05fbdf8d685d93ac192ca3b56c53cdf63a168cc5477c83e84
|
7
|
+
data.tar.gz: 62e1c97fd22b160a960a25532a62a36ad59604a208f45a66be0174490f56c33ff21466815fc6a17b53567bfe683919a2c3f6e0c4bdcf2bf02fc07b4c3b93a8e6
|
data/.travis.yml
CHANGED
data/README.md
CHANGED
@@ -43,6 +43,9 @@ Defines private readers for `@foo` and `@bar`.
|
|
43
43
|
`pattr_initialize :foo, :bar`<br>
|
44
44
|
Defines both initializer and private readers.
|
45
45
|
|
46
|
+
`attr_value :foo, :bar`<br>
|
47
|
+
Defines both initializer and public readers.
|
48
|
+
|
46
49
|
`method_object :fooable?, :foo`<br>
|
47
50
|
Defines a `.fooable?` class method that delegates to an instance method.
|
48
51
|
|
@@ -100,6 +103,15 @@ end
|
|
100
103
|
|
101
104
|
x = MyHashyObject.new("Foo!", bar: "Bar!", baz: "Baz!")
|
102
105
|
x.bar # => "Bar!"
|
106
|
+
|
107
|
+
class MyValueObject
|
108
|
+
attr_value :foo, :bar
|
109
|
+
end
|
110
|
+
|
111
|
+
x = MyValueObject.new(5, 10)
|
112
|
+
x.foo # => 5
|
113
|
+
x.bar # => 10
|
114
|
+
x.foo = 20 # NoMethodError: undefined method `foo=''`
|
103
115
|
```
|
104
116
|
|
105
117
|
## Why not use `Struct`?
|
data/lib/attr_extras.rb
CHANGED
@@ -38,6 +38,11 @@ module AttrExtras
|
|
38
38
|
attr_private(*names.flatten)
|
39
39
|
end
|
40
40
|
|
41
|
+
def attr_value(*names)
|
42
|
+
attr_initialize(*names)
|
43
|
+
attr_reader(*names.flatten)
|
44
|
+
end
|
45
|
+
|
41
46
|
def method_object(method_name, *names)
|
42
47
|
singleton_class.send(:define_method, method_name) do |*values|
|
43
48
|
new(*values).send(method_name)
|
data/lib/attr_extras/version.rb
CHANGED
data/script/test
ADDED
@@ -0,0 +1,8 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
# Script used to have one way to run single tests in any project,
|
4
|
+
# in this case we need to work around test unit a bit.
|
5
|
+
|
6
|
+
# Test unit does not support line numbers :(
|
7
|
+
file = ARGV.first.split(":").first
|
8
|
+
system("bundle exec ruby #{file}") || exit(1)
|
data/spec/attr_extras_spec.rb
CHANGED
@@ -98,6 +98,27 @@ describe Object, ".pattr_initialize" do
|
|
98
98
|
end
|
99
99
|
end
|
100
100
|
|
101
|
+
describe Object, ".attr_value" do
|
102
|
+
it "creates both initializer and public readers" do
|
103
|
+
klass = Class.new do
|
104
|
+
attr_value :foo, :bar
|
105
|
+
end
|
106
|
+
|
107
|
+
example = klass.new("Foo", "Bar")
|
108
|
+
example.foo.must_equal "Foo"
|
109
|
+
lambda { example.foo = "new value" }.must_raise NoMethodError
|
110
|
+
end
|
111
|
+
|
112
|
+
it "works with hash ivars" do
|
113
|
+
klass = Class.new do
|
114
|
+
attr_value :foo, [:bar, :baz]
|
115
|
+
end
|
116
|
+
|
117
|
+
example = klass.new("Foo", :bar => "Bar")
|
118
|
+
example.bar.must_equal "Bar"
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
101
122
|
describe Object, ".attr_id_query" do
|
102
123
|
it "creates id query methods" do
|
103
124
|
klass = Class.new do
|
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.7.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Henrik Nyh
|
@@ -40,6 +40,7 @@ files:
|
|
40
40
|
- attr_extras.gemspec
|
41
41
|
- lib/attr_extras.rb
|
42
42
|
- lib/attr_extras/version.rb
|
43
|
+
- script/test
|
43
44
|
- spec/attr_extras_spec.rb
|
44
45
|
homepage: https://github.com/barsoom/attr_extras
|
45
46
|
licenses:
|