ruby_peter_v 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/HISTORY.txt CHANGED
@@ -8,4 +8,8 @@
8
8
 
9
9
  0.0.3 (2013-05-22)
10
10
 
11
- * Object#max_with_nil added
11
+ * Object#max_with_nil(a, b) added
12
+
13
+ 0.0.4 (20123-05-22)
14
+
15
+ * Object#set_once(attribute, value) added
data/README.md CHANGED
@@ -34,6 +34,15 @@ Or install it yourself as:
34
34
  This is similar to the behavior ios nil would be considered
35
35
  smaller than all other objects.
36
36
 
37
+ * #set_once(attribute, value) on Object
38
+
39
+ Setting the instance variable with name attribute (a symbol),
40
+ once to a value value. This acts as a simple form of an
41
+ immutable attribute. After initialization of the object, it
42
+ can still be set once from nil to a specific value, but after
43
+ that, it can never be changed to a different value. Writing
44
+ it twice with the same value does not trow an exception.
45
+
37
46
  ## Contributing
38
47
 
39
48
  1. Fork it
@@ -0,0 +1,16 @@
1
+ class SetOnceError < StandardError ; end
2
+
3
+ class Object
4
+
5
+ def set_once(attribute, value)
6
+ ivar_symbol = :"@#{attribute}"
7
+ instance_value = instance_variable_get(ivar_symbol)
8
+ if (instance_value && instance_value != value)
9
+ raise(
10
+ SetOnceError,
11
+ "Value of #{attribute} was #{instance_value}, trying to set it to #{value}")
12
+ end
13
+ instance_variable_set(:"@#{attribute}", value)
14
+ end
15
+
16
+ end
@@ -1,3 +1,3 @@
1
1
  module RubyPeterV
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
data/lib/ruby_peter_v.rb CHANGED
@@ -1,3 +1,4 @@
1
1
  require 'ruby_peter_v/version'
2
2
  require 'ruby_peter_v/single'
3
3
  require 'ruby_peter_v/max_with_nil'
4
+ require 'ruby_peter_v/set_once'
@@ -0,0 +1,30 @@
1
+ require 'spec_helper'
2
+
3
+ describe "set_once" do
4
+
5
+ let(:subject) { Object.new }
6
+
7
+ it "can be set when nil" do
8
+ subject.instance_variable_get(:@attr).should be_nil # assert pre-condition
9
+ value = 10
10
+ subject.set_once(:attr, value)
11
+ subject.instance_variable_get(:@attr).should == value
12
+ end
13
+
14
+ describe "setting it two times" do
15
+ it "with the same value just works" do
16
+ value = 10
17
+ subject.set_once(:attr, value)
18
+ subject.set_once(:attr, value)
19
+ subject.instance_variable_get(:@attr).should == value
20
+ end
21
+
22
+ it "with a different value raises a SetOnceError" do
23
+ value = 10
24
+ subject.set_once(:attr, value)
25
+ lambda { subject.set_once(:attr, value+1) } . should raise_error(
26
+ SetOnceError,
27
+ "Value of attr was 10, trying to set it to 11")
28
+ end
29
+ end
30
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby_peter_v
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-05-21 00:00:00.000000000 Z
12
+ date: 2013-05-22 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
@@ -43,10 +43,12 @@ files:
43
43
  - Rakefile
44
44
  - lib/ruby_peter_v.rb
45
45
  - lib/ruby_peter_v/max_with_nil.rb
46
+ - lib/ruby_peter_v/set_once.rb
46
47
  - lib/ruby_peter_v/single.rb
47
48
  - lib/ruby_peter_v/version.rb
48
49
  - ruby_peter_v.gemspec
49
50
  - spec/max_with_nil_spec.rb
51
+ - spec/set_once_spec.rb
50
52
  - spec/single_spec.rb
51
53
  - spec/spec_helper.rb
52
54
  homepage: https://github.com/petervandenabeele/ruby_peter_v
@@ -75,5 +77,6 @@ specification_version: 3
75
77
  summary: Ruby helpers for @peter_v
76
78
  test_files:
77
79
  - spec/max_with_nil_spec.rb
80
+ - spec/set_once_spec.rb
78
81
  - spec/single_spec.rb
79
82
  - spec/spec_helper.rb