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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: bee10ccc02153b1f71b7aed58cba2ec72b82518a
4
- data.tar.gz: 5f546c46cbca537eb24cf497b3cfc917cb902f81
3
+ metadata.gz: ffd8abe1675a431f0b748464aae94e5e45ff1305
4
+ data.tar.gz: eb248dbd2a6e75dec4e9fe44c9a999087bc9fcb9
5
5
  SHA512:
6
- metadata.gz: e68b64c856a4e8a08f99f47d67afcdb3630ea8cf3765533bc76744c7629fc16ac0bbfeae13f37047ebe72c1ed528bcb2431f3c3b0ea9fc0b18185718e413ae8c
7
- data.tar.gz: 5c0f5a1879bc6e61611f96d130b2c6f0774660ecd664b07952546fdba6602b5a713c162a791f14d0d4d80185d5b61d0f7b65240f30c8eeddf3f5fc2578e515d2
6
+ metadata.gz: cb1c750b44e30aa2f387175e57a259a48c2b1b5bafab3acd937da929cb62fa18519ed2b664d55fd05fbdf8d685d93ac192ca3b56c53cdf63a168cc5477c83e84
7
+ data.tar.gz: 62e1c97fd22b160a960a25532a62a36ad59604a208f45a66be0174490f56c33ff21466815fc6a17b53567bfe683919a2c3f6e0c4bdcf2bf02fc07b4c3b93a8e6
data/.travis.yml CHANGED
@@ -1,6 +1,6 @@
1
1
  language: ruby
2
2
  rvm:
3
- - 2.0.0
3
+ - 2.1.0
4
4
  - 1.9.3
5
- - rbx-19mode
5
+ - rbx
6
6
  - jruby-19mode
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)
@@ -1,3 +1,3 @@
1
1
  module AttrExtras
2
- VERSION = "1.6.3"
2
+ VERSION = "1.7.0"
3
3
  end
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)
@@ -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.6.3
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: