smtlaissezfaire-lazy_attr 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,43 @@
1
+
2
+ LazyAttr
3
+ ========
4
+
5
+ Extend your class with LazyAttr:
6
+
7
+ require "rubygems"
8
+ require "lazy_attr"
9
+
10
+ class MyClass
11
+ extend LazyAttr
12
+
13
+ lazy_attr_reader :foo, lambda { 17 }
14
+ attr_writer :foo
15
+
16
+ lazy_attr_accessor :bar, lambda { Time.now }
17
+ end
18
+
19
+
20
+ Use it like a typical `attr_reader` or `attr_accessor`:
21
+
22
+ >> obj = MyClass.new
23
+ => #<MyClass:0x260818>
24
+
25
+ >> obj.foo
26
+ => 17
27
+ >> obj.foo = 18
28
+ => 18
29
+ >> obj.foo
30
+ => 18
31
+
32
+ >> obj.bar
33
+ => Mon Feb 23 01:41:15 -0500 2009
34
+ >> obj.bar
35
+ => Mon Feb 23 01:41:15 -0500 2009
36
+ >> obj.bar = Time.now
37
+ => Mon Feb 23 01:41:20 -0500 2009
38
+ >> obj.bar
39
+ => Mon Feb 23 01:41:20 -0500 2009
40
+
41
+
42
+
43
+ Scott Taylor // scott@railsnewbie.com
@@ -0,0 +1,4 @@
1
+ ---
2
+ :major: 1
3
+ :minor: 0
4
+ :patch: 1
@@ -0,0 +1,18 @@
1
+ module LazyAttr
2
+ def lazy_attr_reader(method_name, default_value)
3
+ ivar_name = "@#{method_name}"
4
+
5
+ define_method method_name do
6
+ if ivar = instance_variable_get(ivar_name)
7
+ ivar
8
+ else
9
+ instance_variable_set(ivar_name, default_value.call)
10
+ end
11
+ end
12
+ end
13
+
14
+ def lazy_attr_accessor(method_name, default_value)
15
+ lazy_attr_reader(method_name, default_value)
16
+ attr_writer(method_name)
17
+ end
18
+ end
@@ -0,0 +1,105 @@
1
+ require File.dirname(__FILE__) + "/spec_helper"
2
+
3
+ describe LazyAttr do
4
+ def new_class(&block)
5
+ klass = Class.new do
6
+ extend LazyAttr
7
+ end
8
+ klass.class_eval(&block)
9
+ klass
10
+ end
11
+
12
+ describe "lazy attr reader" do
13
+ it "should define a lazy attr reader which sets the value on access" do
14
+ klass = new_class do
15
+ lazy_attr_reader :foo, lambda { [] }
16
+ end
17
+
18
+ klass.new.foo.should == []
19
+ end
20
+
21
+ it "should use the correct value for the reader" do
22
+ klass = new_class do
23
+ lazy_attr_reader :foo, lambda { "foobar" }
24
+ end
25
+
26
+ klass.new.foo.should == "foobar"
27
+ end
28
+
29
+ it "should use the real value if given" do
30
+ klass = new_class do
31
+ lazy_attr_reader :foo, lambda { "foobar" }
32
+ attr_writer :foo
33
+ end
34
+
35
+ obj = klass.new
36
+
37
+ obj.foo = "bar"
38
+ obj.foo.should == "bar"
39
+ end
40
+
41
+ it "should set the ivar on the first access" do
42
+ klass = new_class do
43
+ lazy_attr_reader :foo, lambda { "foobar" }
44
+ end
45
+
46
+ obj = klass.new
47
+
48
+ obj.foo
49
+ obj.instance_variable_get("@foo").should == "foobar"
50
+ end
51
+
52
+ it "should use the correct name for the accessor" do
53
+ klass = new_class do
54
+ lazy_attr_reader :bar, lambda { "foobar" }
55
+ end
56
+
57
+ obj = klass.new
58
+
59
+ obj.should respond_to(:bar)
60
+ end
61
+ end
62
+
63
+ describe "lazy_attr_accessor" do
64
+ it "should setup a normal writer" do
65
+ klass = new_class do
66
+ lazy_attr_accessor :foo, lambda { nil }
67
+ end
68
+
69
+ obj = klass.new
70
+
71
+ obj.foo = "bar"
72
+ obj.foo.should == "bar"
73
+ end
74
+
75
+ it "should setup a lazy_attr_reader" do
76
+ klass = new_class do
77
+ lazy_attr_accessor :foo, lambda { "baz" }
78
+ end
79
+
80
+ obj = klass.new
81
+ obj.foo.should == "baz"
82
+ end
83
+
84
+ it "should setup a reader & writer with the correct names" do
85
+ klass = new_class do
86
+ lazy_attr_accessor :baz, lambda { "baz" }
87
+ end
88
+
89
+ obj = klass.new
90
+ obj.should respond_to(:baz)
91
+ obj.should respond_to(:baz=)
92
+ end
93
+
94
+ it "should use the default value given" do
95
+ an_object = mock 'an object'
96
+
97
+ klass = new_class do
98
+ lazy_attr_accessor :baz, lambda { an_object }
99
+ end
100
+
101
+ obj = klass.new
102
+ obj.baz.should equal(an_object)
103
+ end
104
+ end
105
+ end
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format profile
@@ -0,0 +1,4 @@
1
+ require "rubygems"
2
+ require "spec"
3
+
4
+ require File.dirname(__FILE__) + "/../lib/lazy_attr"
metadata ADDED
@@ -0,0 +1,60 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: smtlaissezfaire-lazy_attr
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Scott Taylor
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-02-24 00:00:00 -08:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: TODO
17
+ email: scott@railsnewbie.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - README.markdown
26
+ - VERSION.yml
27
+ - lib/lazy_attr.rb
28
+ - spec/lazy_attr
29
+ - spec/lazy_attr_spec.rb
30
+ - spec/spec.opts
31
+ - spec/spec_helper.rb
32
+ has_rdoc: true
33
+ homepage: http://github.com/smtlaissezfaire/lazy_attr
34
+ post_install_message:
35
+ rdoc_options:
36
+ - --inline-source
37
+ - --charset=UTF-8
38
+ require_paths:
39
+ - lib
40
+ required_ruby_version: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ version: "0"
45
+ version:
46
+ required_rubygems_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: "0"
51
+ version:
52
+ requirements: []
53
+
54
+ rubyforge_project:
55
+ rubygems_version: 1.2.0
56
+ signing_key:
57
+ specification_version: 2
58
+ summary: TODO
59
+ test_files: []
60
+