attr_extras 1.2.0 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -7,6 +7,9 @@ Takes some boilerplate out of Ruby and complements `attr_accessor`, `attr_reader
7
7
  `attr_initialize :foo, :bar`<br>
8
8
  Defines an initializer that takes two arguments and assigns `@foo` and `@bar`.
9
9
 
10
+ `attr_initialize :foo, [:bar, :baz]`<br>
11
+ Defines an initializer that takes one regular argument, assigning `@foo`, and one hash argument, assigning `@bar` and `@baz`.
12
+
10
13
  `attr_private :foo, :bar`<br>
11
14
  Defines private readers for `@foo` and `@bar`.
12
15
 
@@ -47,7 +50,8 @@ x.foo # NoMethodError: private method `foo' called.
47
50
  x.item? # => true
48
51
 
49
52
  class MyMethodObject
50
- method_object :fooable?, :foo
53
+ method_object :fooable?,
54
+ :foo
51
55
 
52
56
  def fooable?
53
57
  foo == :some_value
@@ -56,6 +60,14 @@ end
56
60
 
57
61
  MyMethodObject.fooable?(:some_value) # => true
58
62
  MyMethodObject.fooable?(:another_value) # => false
63
+
64
+ class MyHashyObject
65
+ attr_initialize :foo, [:bar, :baz]
66
+ attr_reader :bar
67
+ end
68
+
69
+ x = MyHashyObject.new("Foo!", bar: "Bar!", baz: "Baz!")
70
+ x.bar # => "Bar!"
59
71
  ```
60
72
 
61
73
 
data/lib/attr_extras.rb CHANGED
@@ -8,8 +8,14 @@ module AttrExtras
8
8
  raise ArgumentError, "wrong number of arguments (#{values.length} for #{names.length})"
9
9
  end
10
10
 
11
- names.zip(values).each do |name, value|
12
- instance_variable_set("@#{name}", value)
11
+ names.zip(values).each do |name_or_names, value|
12
+ if name_or_names.is_a?(Array)
13
+ name_or_names.each do |key|
14
+ instance_variable_set("@#{key}", value[key])
15
+ end
16
+ else
17
+ instance_variable_set("@#{name_or_names}", value)
18
+ end
13
19
  end
14
20
  end
15
21
  end
@@ -21,7 +27,7 @@ module AttrExtras
21
27
 
22
28
  def pattr_initialize(*names)
23
29
  attr_initialize *names
24
- attr_private *names
30
+ attr_private *names.flatten
25
31
  end
26
32
 
27
33
  def method_object(method_name, *names)
@@ -1,3 +1,3 @@
1
1
  module AttrExtras
2
- VERSION = "1.2.0"
2
+ VERSION = "1.3.0"
3
3
  end
@@ -1,4 +1,5 @@
1
1
  require "minitest/autorun"
2
+ require "minitest/pride"
2
3
  require "attr_extras"
3
4
 
4
5
  class Example
@@ -16,13 +17,18 @@ class QueryExample
16
17
  end
17
18
 
18
19
  class MethodObjectExample
19
- method_object :fooable?, :foo
20
+ method_object :fooable?,
21
+ :foo
20
22
 
21
23
  def fooable?
22
24
  foo
23
25
  end
24
26
  end
25
27
 
28
+ class ExampleWithHash
29
+ pattr_initialize :foo, [:bar, :baz]
30
+ end
31
+
26
32
  describe Object, ".method_object" do
27
33
  it "creates a class method that instantiates and runs that instance method" do
28
34
  assert MethodObjectExample.fooable?(true)
@@ -40,6 +46,13 @@ describe Object, ".attr_initialize" do
40
46
  it "requires all arguments" do
41
47
  lambda { Example.new("Foo") }.must_raise ArgumentError
42
48
  end
49
+
50
+ it "can set ivars from a hash" do
51
+ example = ExampleWithHash.new("Foo", bar: "Bar", baz: "Baz")
52
+ example.instance_variable_get("@foo").must_equal "Foo"
53
+ example.instance_variable_get("@bar").must_equal "Bar"
54
+ example.instance_variable_get("@baz").must_equal "Baz"
55
+ end
43
56
  end
44
57
 
45
58
  describe Object, ".attr_private" do
@@ -56,6 +69,11 @@ describe Object, ".pattr_initialize" do
56
69
  example = PattrExample.new("Foo", "Bar")
57
70
  example.send(:foo).must_equal "Foo"
58
71
  end
72
+
73
+ it "works with hash ivars" do
74
+ example = ExampleWithHash.new("Foo", bar: "Bar")
75
+ example.send(:bar).must_equal "Bar"
76
+ end
59
77
  end
60
78
 
61
79
  describe Object, ".attr_id_query" 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.2.0
4
+ version: 1.3.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-29 00:00:00.000000000 Z
12
+ date: 2013-02-21 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
16
- requirement: &70346338947260 !ruby/object:Gem::Requirement
16
+ requirement: &70350078109880 !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: *70346338947260
24
+ version_requirements: *70350078109880
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: 1416484778478969945
56
+ hash: -4277147738060101807
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: 1416484778478969945
65
+ hash: -4277147738060101807
66
66
  requirements: []
67
67
  rubyforge_project:
68
68
  rubygems_version: 1.8.10