subtle 0.3.6 → 0.3.8

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -104,6 +104,38 @@ end
104
104
  person = Person.new(first_name: "John", last_name: "Galt")
105
105
  ````
106
106
 
107
+ You can also pass a block to the constructor when it is instantiated, like this:
108
+
109
+ ````ruby
110
+ class Person
111
+ params_constructor do
112
+ self.first_name = 'Howard'
113
+ end
114
+ attr_accessor :first_name, :last_name
115
+ end
116
+
117
+ person = Person.new(first_name: 'Dagny', last_name: 'Roark')
118
+
119
+ person.first_name #= Howard
120
+
121
+ ````
122
+
123
+ or like this:
124
+
125
+ ````ruby
126
+ class Person
127
+ params_constructor
128
+ attr_accessor :first_name, :last_name
129
+ end
130
+
131
+ person = Person.new(first_name: 'Dagny', last_name: 'Roark') do |p|
132
+ p.first_name = 'Howard'
133
+ end
134
+
135
+ person.first_name #= Howard
136
+
137
+ ````
138
+
107
139
  ### Proc to Object
108
140
 
109
141
  I was inspired to write this feature while dealing with some bad Rails code. A programmer wrote a before_filter on ApplicationController that made a big, expensive web service call to pass the users current weather information to the view. This weather information was shown in various places on the site, but there were many pages on the site where the data was not being used at all.
@@ -1,10 +1,18 @@
1
1
  class Object
2
- def param_constructor
2
+ def param_constructor(&block_for_each)
3
3
  self.class_eval('
4
- def initialize(params={})
4
+ def initialize(params={}, &block)
5
5
  params.each do |attr, value|
6
6
  self.public_send("#{attr}=", value)
7
7
  end if params
8
+ unless block.nil?
9
+ block.call(self)
10
+ end
11
+ constructor_method if respond_to?(:constructor_method)
12
+ block.call(self) if block
8
13
  end')
14
+ unless block_for_each.nil?
15
+ self.send(:define_method, :constructor_method, &block_for_each)
16
+ end
9
17
  end
10
18
  end
@@ -1,3 +1,3 @@
1
1
  module Subtle
2
- VERSION = "0.3.6"
2
+ VERSION = "0.3.8"
3
3
  end
@@ -5,15 +5,65 @@ class ParamConstructorTest
5
5
  attr_accessor :first_name, :last_name
6
6
  end
7
7
 
8
+ class SecondParamConstructorTest
9
+ param_constructor
10
+ attr_accessor :first_name, :last_name
11
+ end
12
+
13
+ class ThirdParamConstructorTest
14
+ param_constructor { self.first_name = "expected value" }
15
+ attr_accessor :first_name, :last_name
16
+ end
17
+
18
+ class FourthParamConstructorTest
19
+ param_constructor do
20
+ self.middle_name = 'm'
21
+ self.first_name = 'f'
22
+ self.last_name = 'l'
23
+ end
24
+ attr_accessor :first_name, :last_name, :middle_name
25
+ end
26
+
8
27
  describe "param_constructor" do
9
- it "should let the object be instantiated with a hash" do
10
- test = ParamConstructorTest.new(first_name: "John", last_name: "Galt")
11
- test.first_name.must_equal "John"
12
- test.last_name.must_equal "Galt"
28
+ describe 'just the constructor itself' do
29
+ it "should let the object be instantiated with a hash" do
30
+ test = ParamConstructorTest.new(first_name: "John", last_name: "Galt")
31
+ test.first_name.must_equal "John"
32
+ test.last_name.must_equal "Galt"
33
+ end
34
+
35
+ it "should allow the object to be instantiated with no params" do
36
+ test = ParamConstructorTest.new
37
+ # should not throw an error
38
+ end
39
+ end
40
+
41
+ describe 'pass a block to the constructor' do
42
+ it "should let the object be instantiated with a hash" do
43
+ test = SecondParamConstructorTest.new(first_name: "Dagny", last_name: "Roark") do |o|
44
+ o.first_name = 'Howard'
45
+ end
46
+ test.first_name.must_equal 'Howard'
47
+ test.last_name.must_equal 'Roark'
48
+ end
49
+ end
50
+
51
+ describe 'pass a block with the constructor' do
52
+ it "should let the object be instantiated with a hash" do
53
+ test = ThirdParamConstructorTest.new(first_name: "Dagny", last_name: "Roark")
54
+ test.first_name.must_equal 'expected value'
55
+ end
13
56
  end
14
57
 
15
- it "should allow the object to be instantiated with no params" do
16
- test = ParamConstructorTest.new
17
- # should not throw an error
58
+ describe 'pass a block with the constructor AND to the constructor' do
59
+ it "should run both and let the last one win" do
60
+ test = FourthParamConstructorTest.new(first_name: "", last_name: "") do |o|
61
+ o.last_name = "second"
62
+ o.first_name = "first"
63
+ end
64
+ test.first_name.must_equal 'first'
65
+ test.last_name.must_equal 'second'
66
+ test.middle_name.must_equal 'm'
67
+ end
18
68
  end
19
69
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: subtle
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.6
4
+ version: 0.3.8
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: 2012-08-04 00:00:00.000000000 Z
12
+ date: 2012-08-07 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: blankslate
@@ -105,7 +105,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
105
105
  version: '0'
106
106
  segments:
107
107
  - 0
108
- hash: 3253476419710169719
108
+ hash: 2130171851607825490
109
109
  required_rubygems_version: !ruby/object:Gem::Requirement
110
110
  none: false
111
111
  requirements:
@@ -114,7 +114,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
114
114
  version: '0'
115
115
  segments:
116
116
  - 0
117
- hash: 3253476419710169719
117
+ hash: 2130171851607825490
118
118
  requirements: []
119
119
  rubyforge_project:
120
120
  rubygems_version: 1.8.24