renewable 0.0.1 → 0.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: 648680f8f5c0283aa11938e169a4ab509991221e
4
- data.tar.gz: bbc9186b86c072cf0ece86692883486088325c62
3
+ metadata.gz: af1d37fe4a9a6091904ef8fb8cde432d4c47c1e6
4
+ data.tar.gz: bc9807d81c12e440dc65a15e87c03381a1d98dac
5
5
  SHA512:
6
- metadata.gz: 0cdab0882b58b0680062093a69723fcea91a86803a4ce7936bc0eeb42f37d51c97f8930e9a5fcc90ff3578500c351e33007e8c67cf9b0e438070482445736012
7
- data.tar.gz: 9b64237e91316164f7dabc144e40f14f7e1e2b764d654ba22b4f16394f7170b1a37ed922da186fca65cebdf89e5964583183bac13162ce639161acd15d37a087
6
+ metadata.gz: 7a7fb4807078d3cfe26e4a86147eb256784cf6bc5ceab880a2fd5456d95828d417b0881adba3661ee3dbf1a096848e34958f6b1d957b0cdc9b7bc772c80b7388
7
+ data.tar.gz: 07207c55ba56c04dc4b463c9035534255271a1328076ccf2a558b7663dae36a84b5b4d5f6b6e4bd8fb2ab01bffe2f9f92a970dfc98e9188f6697bf0051ac8861
data/README.md CHANGED
@@ -108,11 +108,41 @@ end
108
108
 
109
109
  ### Mutators (Methods that change stuff)
110
110
 
111
- TODO: Write this section.
111
+ *Don't write them!* This is where the idea of being more Functional comes into
112
+ play. Methods that modify the internal state of an object make it harder to
113
+ ensure the thread-safety of your code. But, so much of Object-Oriented
114
+ Programming is directly tied to mutation. Thankfully, with Renewable objects
115
+ that functionality is explicitly not available.
112
116
 
113
- ### #renew
117
+ Instead, Renewable provides a method called `renew` that allows you to achieve
118
+ functionality similar to mutation, but without the same side effects. Instead
119
+ `renew` returns a completely new instance of the object, and you get control
120
+ over exactly what aspects of internal state change for the new instance.
114
121
 
115
- TODO: Write this section.
122
+ The major downside to this is that you have to expect your mutator methods to
123
+ return a new instance, never just a value or nil. But, that's a pretty easy
124
+ idea to get used to.
125
+
126
+ #### #renew
127
+
128
+ The `renew` method is what you use to move beyond your frozen object, it accepts
129
+ two hashes: `attributes` and `options`. In that way, it is like calling `new`,
130
+ but it starts with the state of your frozen object and applies the changes you
131
+ specify.
132
+
133
+ ```ruby
134
+ class Person
135
+ #...
136
+
137
+ def change_name(name)
138
+ self.renew(name: name.to_s)
139
+ end
140
+ end
141
+ ```
142
+
143
+ That really is as simple as `renew` is in many cases. Because it accepts the
144
+ `attributes` and `options` arguments and calls `initialize` under the hood you
145
+ still get all the functionality of the callback handling when renewing objects.
116
146
 
117
147
  ## Contributing
118
148
 
@@ -10,15 +10,22 @@ module Renewable
10
10
  after_freeze(attributes, options)
11
11
  end
12
12
 
13
+ def renew(attributes = {}, options = {})
14
+ merged_attributes = self.renewable_attributes.merge(attributes)
15
+ merged_options = self.renewable_options.merge(options)
16
+
17
+ self.class.new(merged_attributes, merged_options)
18
+ end
19
+
13
20
  protected
14
21
 
15
- # def renewable_attributes
16
- # @renwable_attributes
17
- # end
22
+ def renewable_attributes
23
+ @renewable_attributes
24
+ end
18
25
 
19
- # def renewable_options
20
- # @renwable_options
21
- # end
26
+ def renewable_options
27
+ @renewable_options
28
+ end
22
29
 
23
30
  private
24
31
 
@@ -31,11 +38,11 @@ module Renewable
31
38
  self.instance_variable_set(:"@#{name}", value)
32
39
  end
33
40
 
34
- #@renwable_attributes = attributes
41
+ @renewable_attributes = attributes
35
42
  end
36
43
 
37
44
  def renewable_process_options(options)
38
- #@renwable_options = options
45
+ @renewable_options = options
39
46
  end
40
47
 
41
48
  def before_freeze(attributes, options)
@@ -1,3 +1,3 @@
1
1
  module Renewable
2
- VERSION = '0.0.1'
2
+ VERSION = '0.1.0'
3
3
  end
@@ -3,11 +3,11 @@ require 'samples/inheritance_sample'
3
3
  RSpec.describe Renewable do
4
4
  context 'used via inheritance' do
5
5
  it 'initializes to a frozen object' do
6
- expect(PersonA.new(birth_date: '1972-06-13').frozen?).to eq(true)
6
+ expect(InheritedPerson.new(birth_date: '1972-06-13').frozen?).to eq(true)
7
7
  end
8
8
 
9
9
  it 'assigns all attributes' do
10
- person = PersonA.new(name: 'John',
10
+ person = InheritedPerson.new(name: 'John',
11
11
  birth_date: '1972-06-13',
12
12
  hair_color: 'Brown')
13
13
 
@@ -18,19 +18,19 @@ RSpec.describe Renewable do
18
18
 
19
19
  it 'runs process_arguments callback' do
20
20
  expect {
21
- PersonA.new({}, {raise_process_arguments: true})
21
+ InheritedPerson.new({}, {raise_process_arguments: true})
22
22
  }.to raise_error(ArgumentError, 'entered process_arguments with raise option')
23
23
  end
24
24
 
25
25
  it 'runs before_freeze callback' do
26
26
  expect {
27
- PersonA.new({}, {raise_before_freeze: true})
27
+ InheritedPerson.new({}, {raise_before_freeze: true})
28
28
  }.to raise_error(ArgumentError, 'entered before_freeze with raise option')
29
29
  end
30
30
 
31
31
  it 'runs after_freeze callback' do
32
32
  expect {
33
- PersonA.new({}, {raise_after_freeze: true})
33
+ InheritedPerson.new({}, {raise_after_freeze: true})
34
34
  }.to raise_error(ArgumentError, 'entered after_freeze with raise option')
35
35
  end
36
36
  end
@@ -3,11 +3,11 @@ require 'samples/mixin_sample'
3
3
  RSpec.describe Renewable do
4
4
  context 'used as a mixin' do
5
5
  it 'initializes to a frozen object' do
6
- expect(PersonB.new(birth_date: '1972-06-13').frozen?).to eq(true)
6
+ expect(MixinPerson.new(birth_date: '1972-06-13').frozen?).to eq(true)
7
7
  end
8
8
 
9
9
  it 'assigns all attributes' do
10
- person = PersonB.new(name: 'John',
10
+ person = MixinPerson.new(name: 'John',
11
11
  birth_date: '1972-06-13',
12
12
  hair_color: 'Brown')
13
13
 
@@ -18,19 +18,19 @@ RSpec.describe Renewable do
18
18
 
19
19
  it 'runs process_arguments callback' do
20
20
  expect {
21
- PersonB.new({}, {raise_process_arguments: true})
21
+ MixinPerson.new({}, {raise_process_arguments: true})
22
22
  }.to raise_error(ArgumentError, 'entered process_arguments with raise option')
23
23
  end
24
24
 
25
25
  it 'runs before_freeze callback' do
26
26
  expect {
27
- PersonB.new({}, {raise_before_freeze: true})
27
+ MixinPerson.new({}, {raise_before_freeze: true})
28
28
  }.to raise_error(ArgumentError, 'entered before_freeze with raise option')
29
29
  end
30
30
 
31
31
  it 'runs after_freeze callback' do
32
32
  expect {
33
- PersonB.new({}, {raise_after_freeze: true})
33
+ MixinPerson.new({}, {raise_after_freeze: true})
34
34
  }.to raise_error(ArgumentError, 'entered after_freeze with raise option')
35
35
  end
36
36
  end
@@ -0,0 +1,28 @@
1
+ require 'samples/inheritance_sample'
2
+
3
+ RSpec.describe Renewable do
4
+ describe '#renew' do
5
+ let(:subject) { InheritedPerson.new(attributes) }
6
+ let(:attributes) { {
7
+ name: 'John',
8
+ birth_date: '1973-06-15'
9
+ } }
10
+
11
+ it 'returns a new object' do
12
+ new_object = subject.renew
13
+ expect(new_object.__id__).not_to eq(subject.__id__)
14
+ end
15
+
16
+ it 'changes attributes of new object' do
17
+ new_object = subject.renew(name: 'Jack')
18
+ name = new_object.instance_variable_get(:@name)
19
+ expect(name).to eq('Jack')
20
+ end
21
+
22
+ it 'changes options of new object' do
23
+ new_object = subject.renew({}, example: 'test')
24
+ options = new_object.instance_variable_get(:@renewable_options)
25
+ expect(options[:example]).to eq('test')
26
+ end
27
+ end
28
+ end
@@ -1,7 +1,7 @@
1
1
  require 'date'
2
2
  require 'renewable'
3
3
 
4
- class PersonA < Renewable::Object
4
+ class InheritedPerson < Renewable::Object
5
5
  attr_accessor :name, :birth_date
6
6
 
7
7
  def age
@@ -1,7 +1,7 @@
1
1
  require 'date'
2
2
  require 'renewable'
3
3
 
4
- class PersonB
4
+ class MixinPerson
5
5
  include ::Renewable
6
6
 
7
7
  attr_accessor :name, :birth_date
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: renewable
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - James Thompson
@@ -102,6 +102,7 @@ files:
102
102
  - renewable.gemspec
103
103
  - spec/inhertitance_spec.rb
104
104
  - spec/mixin_spec.rb
105
+ - spec/renew_spec.rb
105
106
  - spec/samples/inheritance_sample.rb
106
107
  - spec/samples/mixin_sample.rb
107
108
  - spec/spec_helper.rb
@@ -132,6 +133,7 @@ summary: Provides frozen by default objects
132
133
  test_files:
133
134
  - spec/inhertitance_spec.rb
134
135
  - spec/mixin_spec.rb
136
+ - spec/renew_spec.rb
135
137
  - spec/samples/inheritance_sample.rb
136
138
  - spec/samples/mixin_sample.rb
137
139
  - spec/spec_helper.rb