attr_extras 3.0.0 → 3.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 984c86a8a64ba56792a8e78ce89452ca66c32b55
4
- data.tar.gz: 3957fdf50be3ffee0ffec904176d6e3918676294
3
+ metadata.gz: 52ef09869d3ebdb635964d77234b8bf806b85370
4
+ data.tar.gz: 3ae234b7c52e1e766cb1399d44c09f5cfc7ee82a
5
5
  SHA512:
6
- metadata.gz: 1857358b5507acbbbd0aefe5266f1b0e5626ecb5415ffd5070a7f4bd327fcdf4e615917df393cbefe58b6b34d6c9ae869a32751549d119e1d2fc37aa8a4f2e46
7
- data.tar.gz: 2f7c2155bfd5a96f9b31f88bf2153ef11048ce1117c03b402cb5ac90249bfe66240177e741ce4544c88e3933566ad6abafb7fe80082c6344172159c05bafff9e
6
+ metadata.gz: 926fdc5d339f40c481cfec0fbb0a20bb7c11ffcc3fe4206fde1c25e3d6183834e04c4bec247c3c89672f22f6d48a0adb1454c93a98b734b17029503c9fffdb98
7
+ data.tar.gz: d6040789a8f309c42a5fa15f46c7168ca0a65975d4a15989bd2b0ad723c8d0de9636b7101ca05e510dd869d9a94855c5e3740a2bee72d4e055fe33679a066eba
data/.travis.yml CHANGED
@@ -1,6 +1,5 @@
1
1
  language: ruby
2
2
  rvm:
3
- - 2.1.0
3
+ - 2.1.2
4
4
  - 1.9.3
5
- - rbx
6
5
  - jruby-19mode
data/README.md CHANGED
@@ -30,7 +30,7 @@ This nicely complements Ruby's built-in `attr_accessor`, `attr_reader` and `attr
30
30
 
31
31
  Supports positional arguments as well as optional and required hash arguments.
32
32
 
33
- Also provides conveniences for creating value objects, method objects and query methods.
33
+ Also provides conveniences for creating value objects, method objects, query methods and abstract methods.
34
34
 
35
35
 
36
36
  ## Usage
@@ -45,6 +45,8 @@ Defines an initializer that takes two arguments and assigns `@foo` and `@bar`.
45
45
  `attr_initialize [:bar, :baz!]` defines an initializer that takes one hash argument, assigning `@bar` (optional) and `@baz` (required).
46
46
 
47
47
 
48
+ `attr_initialize` can also accept a block which will be invoked after initialization. This is useful for calling `super` appropriately in subclasses or initializing private data as necessary.
49
+
48
50
  ### `attr_private :foo, :bar`
49
51
 
50
52
  Defines private readers for `@foo` and `@bar`.
@@ -204,6 +206,7 @@ Or to see warnings (try not to have any):
204
206
  * [Victor Arias](https://github.com/victorarias)
205
207
  * [Teo Ljungberg](https://github.com/teoljungberg)
206
208
  * [Kim Persson](https://github.com/lavinia)
209
+ * [Joe Ferris](https://github.com/jferris)
207
210
 
208
211
 
209
212
  ## License
data/lib/attr_extras.rb CHANGED
@@ -5,8 +5,8 @@ require "attr_extras/utils"
5
5
 
6
6
  module AttrExtras
7
7
  module ModuleMethods
8
- def attr_initialize(*names)
9
- AttrInitialize.new(self, names).apply
8
+ def attr_initialize(*names, &block)
9
+ AttrInitialize.new(self, names, block).apply
10
10
  end
11
11
 
12
12
  def attr_private(*names)
@@ -36,13 +36,13 @@ module AttrExtras
36
36
  end
37
37
  end
38
38
 
39
- def pattr_initialize(*names)
40
- attr_initialize(*names)
39
+ def pattr_initialize(*names, &block)
40
+ attr_initialize(*names, &block)
41
41
  attr_private(*Utils.flat_names(names))
42
42
  end
43
43
 
44
- def vattr_initialize(*names)
45
- attr_initialize(*names)
44
+ def vattr_initialize(*names, &block)
45
+ attr_initialize(*names, &block)
46
46
  attr_value(*Utils.flat_names(names))
47
47
  end
48
48
 
@@ -1,6 +1,6 @@
1
1
  class AttrExtras::AttrInitialize
2
- def initialize(klass, names)
3
- @klass, @names = klass, names
2
+ def initialize(klass, names, block)
3
+ @klass, @names, @block = klass, names, block
4
4
  end
5
5
 
6
6
  attr_reader :klass, :names
@@ -10,6 +10,7 @@ class AttrExtras::AttrInitialize
10
10
  # The define_method block can't call our methods, so we need to make
11
11
  # things available via local variables.
12
12
  names = @names
13
+ block = @block
13
14
  validate_arity = method(:validate_arity)
14
15
  set_ivar_from_hash = method(:set_ivar_from_hash)
15
16
 
@@ -28,6 +29,10 @@ class AttrExtras::AttrInitialize
28
29
  instance_variable_set("@#{name}", value)
29
30
  end
30
31
  end
32
+
33
+ if block
34
+ instance_eval(&block)
35
+ end
31
36
  end
32
37
  end
33
38
 
@@ -1,3 +1,3 @@
1
1
  module AttrExtras
2
- VERSION = "3.0.0"
2
+ VERSION = "3.1.0"
3
3
  end
@@ -50,4 +50,18 @@ describe Object, ".attr_initialize" do
50
50
 
51
51
  lambda { klass.new(:optional => "X") }.must_raise KeyError
52
52
  end
53
+
54
+ it "can accept a block for initialization" do
55
+ klass = Class.new do
56
+ attr_initialize :value do
57
+ @copy = @value
58
+ end
59
+
60
+ attr_reader :copy
61
+ end
62
+
63
+ example = klass.new("expected")
64
+
65
+ example.copy.must_equal "expected"
66
+ end
53
67
  end
@@ -18,4 +18,18 @@ describe Object, ".pattr_initialize" do
18
18
  example = klass.new("Foo", :bar => "Bar", :baz => "Baz")
19
19
  example.send(:baz).must_equal "Baz"
20
20
  end
21
+
22
+ it "can reference private initializer methods in an initializer block" do
23
+ klass = Class.new do
24
+ pattr_initialize :value do
25
+ @copy = value
26
+ end
27
+
28
+ attr_reader :copy
29
+ end
30
+
31
+ example = klass.new("expected")
32
+
33
+ example.copy.must_equal "expected"
34
+ end
21
35
  end
@@ -23,4 +23,17 @@ describe Object, ".vattr_initialize" do
23
23
  example1.baz.must_equal "Baz"
24
24
  example1.must_equal example2
25
25
  end
26
+
27
+ it "can accept an initializer block" do
28
+ called = false
29
+ klass = Class.new do
30
+ vattr_initialize :value do
31
+ called = true
32
+ end
33
+ end
34
+
35
+ klass.new("expected")
36
+
37
+ called.must_equal true
38
+ end
26
39
  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: 3.0.0
4
+ version: 3.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Henrik Nyh
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2014-05-15 00:00:00.000000000 Z
13
+ date: 2014-09-12 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rake