attr_extras 1.6.2 → 1.6.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: bee10ccc02153b1f71b7aed58cba2ec72b82518a
4
+ data.tar.gz: 5f546c46cbca537eb24cf497b3cfc917cb902f81
5
+ SHA512:
6
+ metadata.gz: e68b64c856a4e8a08f99f47d67afcdb3630ea8cf3765533bc76744c7629fc16ac0bbfeae13f37047ebe72c1ed528bcb2431f3c3b0ea9fc0b18185718e413ae8c
7
+ data.tar.gz: 5c0f5a1879bc6e61611f96d130b2c6f0774660ecd664b07952546fdba6602b5a713c162a791f14d0d4d80185d5b61d0f7b65240f30c8eeddf3f5fc2578e515d2
data/README.md CHANGED
@@ -2,7 +2,34 @@
2
2
 
3
3
  # attr\_extras
4
4
 
5
- Takes some boilerplate out of Ruby and complements `attr_accessor`, `attr_reader` and `attr_writer` nicely by providing:
5
+ Takes some boilerplate out of Ruby, lowering the barrier to extracting small focused classes, without the downsides of using `Struct`.
6
+
7
+ Instead of
8
+
9
+ ```
10
+ class InvoiceBuilder
11
+ def initialize(invoice, employee)
12
+ @invoice, @employee = invoice, employee
13
+ end
14
+
15
+ private
16
+
17
+ attr_reader :invoice, :employee
18
+ end
19
+ ```
20
+
21
+ you can just do
22
+
23
+ ```
24
+ class InvoiceBuilder
25
+ pattr_initialize :invoice, :employee
26
+ end
27
+ ```
28
+
29
+ This nicely complements Ruby's built-in `attr_accessor`, `attr_reader` and `attr_writer`.
30
+
31
+
32
+ ## Usage
6
33
 
7
34
  `attr_initialize :foo, :bar`<br>
8
35
  Defines an initializer that takes two arguments and assigns `@foo` and `@bar`.
data/lib/attr_extras.rb CHANGED
@@ -3,13 +3,21 @@ require "attr_extras/version"
3
3
  module AttrExtras
4
4
  module ClassMethods
5
5
  def attr_initialize(*names)
6
+ min_arity = names.count { |n| not n.is_a?(Array) }
7
+ max_arity = names.length
8
+
6
9
  define_method(:initialize) do |*values|
7
- unless values.length == names.length
8
- raise ArgumentError, "wrong number of arguments (#{values.length} for #{names.length})"
10
+ provided_arity = values.length
11
+
12
+ unless (min_arity..max_arity).include?(provided_arity)
13
+ arity_range = [min_arity, max_arity].uniq.join("..")
14
+ raise ArgumentError, "wrong number of arguments (#{provided_arity} for #{arity_range})"
9
15
  end
10
16
 
11
17
  names.zip(values).each do |name_or_names, value|
12
18
  if name_or_names.is_a?(Array)
19
+ value ||= {}
20
+
13
21
  name_or_names.each do |key|
14
22
  instance_variable_set("@#{key}", value[key])
15
23
  end
@@ -1,3 +1,3 @@
1
1
  module AttrExtras
2
- VERSION = "1.6.2"
2
+ VERSION = "1.6.3"
3
3
  end
@@ -2,63 +2,76 @@ require "minitest/autorun"
2
2
  require "minitest/pride"
3
3
  require "attr_extras"
4
4
 
5
- class Example
6
- attr_initialize :foo, :bar
7
- attr_private :foo, :bar
8
- end
9
-
10
- class PattrExample
11
- pattr_initialize :foo, :bar
12
- end
13
-
14
- class QueryExample
15
- attr_id_query :baz?, :boink?
16
- attr_query :flurp?
17
- attr_accessor :baz_id, :flurp
18
- end
19
-
20
- class MethodObjectExample
21
- method_object :fooable?,
22
- :foo
23
-
24
- def fooable?
25
- foo
5
+ describe Object, ".method_object" do
6
+ let(:klass) do
7
+ Class.new do
8
+ method_object :fooable?,
9
+ :foo
10
+
11
+ def fooable?
12
+ foo
13
+ end
14
+ end
26
15
  end
27
- end
28
-
29
- class ExampleWithHash
30
- pattr_initialize :foo, [:bar, :baz]
31
- end
32
16
 
33
- describe Object, ".method_object" do
34
17
  it "creates a class method that instantiates and runs that instance method" do
35
- assert MethodObjectExample.fooable?(true)
36
- refute MethodObjectExample.fooable?(false)
18
+ assert klass.fooable?(true)
19
+ refute klass.fooable?(false)
37
20
  end
38
21
  end
39
22
 
40
23
  describe Object, ".attr_initialize" do
24
+ let(:klass) do
25
+ Class.new do
26
+ attr_initialize :foo, :bar
27
+ end
28
+ end
29
+
41
30
  it "creates an initializer setting those instance variables" do
42
- example = Example.new("Foo", "Bar")
31
+ example = klass.new("Foo", "Bar")
43
32
  example.instance_variable_get("@foo").must_equal "Foo"
44
33
  example.instance_variable_get("@bar").must_equal "Bar"
45
34
  end
46
35
 
47
36
  it "requires all arguments" do
48
- lambda { Example.new("Foo") }.must_raise ArgumentError
37
+ lambda { klass.new("Foo") }.must_raise ArgumentError
49
38
  end
50
39
 
51
40
  it "can set ivars from a hash" do
52
- example = ExampleWithHash.new("Foo", :bar => "Bar", :baz => "Baz")
41
+ klass = Class.new do
42
+ pattr_initialize :foo, [:bar, :baz]
43
+ end
44
+
45
+ example = klass.new("Foo", :bar => "Bar", :baz => "Baz")
53
46
  example.instance_variable_get("@foo").must_equal "Foo"
54
47
  example.instance_variable_get("@bar").must_equal "Bar"
55
48
  example.instance_variable_get("@baz").must_equal "Baz"
56
49
  end
50
+
51
+ it "treats hash values as optional" do
52
+ klass = Class.new do
53
+ pattr_initialize :foo, [:bar, :baz]
54
+ end
55
+
56
+ example = klass.new("Foo", :bar => "Bar")
57
+ example.instance_variable_get("@baz").must_equal nil
58
+
59
+ example = klass.new("Foo")
60
+ example.instance_variable_get("@bar").must_equal nil
61
+ end
57
62
  end
58
63
 
59
64
  describe Object, ".attr_private" do
65
+ let(:klass) do
66
+ Class.new do
67
+ attr_private :foo, :bar
68
+ end
69
+ end
70
+
60
71
  it "creates private readers" do
61
- example = Example.new("Foo", "Bar")
72
+ example = klass.new
73
+ example.instance_variable_set("@foo", "Foo")
74
+ example.instance_variable_set("@bar", "Bar")
62
75
  example.send(:foo).must_equal "Foo"
63
76
  example.send(:bar).must_equal "Bar"
64
77
  lambda { example.foo }.must_raise NoMethodError
@@ -67,20 +80,34 @@ end
67
80
 
68
81
  describe Object, ".pattr_initialize" do
69
82
  it "creates both initializer and private readers" do
70
- example = PattrExample.new("Foo", "Bar")
83
+ klass = Class.new do
84
+ pattr_initialize :foo, :bar
85
+ end
86
+
87
+ example = klass.new("Foo", "Bar")
71
88
  example.send(:foo).must_equal "Foo"
72
89
  end
73
90
 
74
91
  it "works with hash ivars" do
75
- example = ExampleWithHash.new("Foo", :bar => "Bar")
92
+ klass = Class.new do
93
+ pattr_initialize :foo, [:bar, :baz]
94
+ end
95
+
96
+ example = klass.new("Foo", :bar => "Bar")
76
97
  example.send(:bar).must_equal "Bar"
77
98
  end
78
99
  end
79
100
 
80
101
  describe Object, ".attr_id_query" do
81
102
  it "creates id query methods" do
82
- example = QueryExample.new
103
+ klass = Class.new do
104
+ attr_id_query :baz?, :boink?
105
+ attr_accessor :baz_id
106
+ end
107
+
108
+ example = klass.new
83
109
  refute example.baz?
110
+
84
111
  example.baz_id = 123
85
112
  assert example.baz?
86
113
  end
@@ -92,7 +119,12 @@ end
92
119
 
93
120
  describe Object, ".attr_query" do
94
121
  it "creates attribute query methods" do
95
- example = QueryExample.new
122
+ klass = Class.new do
123
+ attr_query :flurp?
124
+ attr_accessor :flurp
125
+ end
126
+
127
+ example = klass.new
96
128
  refute example.flurp?
97
129
  example.flurp = "!"
98
130
  assert example.flurp?
metadata CHANGED
@@ -1,27 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: attr_extras
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.6.2
5
- prerelease:
4
+ version: 1.6.3
6
5
  platform: ruby
7
6
  authors:
8
7
  - Henrik Nyh
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2013-08-30 00:00:00.000000000 Z
11
+ date: 2014-03-09 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: rake
16
- requirement: &70165595375620 !ruby/object:Gem::Requirement
17
- none: false
15
+ requirement: !ruby/object:Gem::Requirement
18
16
  requirements:
19
- - - ! '>='
17
+ - - '>='
20
18
  - !ruby/object:Gem::Version
21
19
  version: '0'
22
20
  type: :development
23
21
  prerelease: false
24
- version_requirements: *70165595375620
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
25
27
  description:
26
28
  email:
27
29
  - henrik@nyh.se
@@ -42,33 +44,26 @@ files:
42
44
  homepage: https://github.com/barsoom/attr_extras
43
45
  licenses:
44
46
  - MIT
47
+ metadata: {}
45
48
  post_install_message:
46
49
  rdoc_options: []
47
50
  require_paths:
48
51
  - lib
49
52
  required_ruby_version: !ruby/object:Gem::Requirement
50
- none: false
51
53
  requirements:
52
- - - ! '>='
54
+ - - '>='
53
55
  - !ruby/object:Gem::Version
54
56
  version: '0'
55
- segments:
56
- - 0
57
- hash: 4094106113878864252
58
57
  required_rubygems_version: !ruby/object:Gem::Requirement
59
- none: false
60
58
  requirements:
61
- - - ! '>='
59
+ - - '>='
62
60
  - !ruby/object:Gem::Version
63
61
  version: '0'
64
- segments:
65
- - 0
66
- hash: 4094106113878864252
67
62
  requirements: []
68
63
  rubyforge_project:
69
- rubygems_version: 1.8.10
64
+ rubygems_version: 2.2.0
70
65
  signing_key:
71
- specification_version: 3
66
+ specification_version: 4
72
67
  summary: Takes some boilerplate out of Ruby with methods like attr_initialize.
73
68
  test_files:
74
69
  - spec/attr_extras_spec.rb