tbpgr_utils 0.0.11 → 0.0.12

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -92,6 +92,107 @@ p atr_sample2.atr1 # => atr1
92
92
  p atr_sample2.atr2 # => atr2
93
93
  ~~~
94
94
 
95
+ ### AttributesInitializable::ClassMethods.attr_reader_init
96
+ ~~~ruby
97
+ require 'attributes_initializable'
98
+
99
+ class AccessorSample
100
+ include AttributesInitializable
101
+ attr_reader_init :atr1, :atr2
102
+ end
103
+
104
+ atr_sample1 = AccessorSample.new :atr1 => 'atr1', :atr2 => 'atr2'
105
+ p atr_sample1.atr1 # => atr1
106
+ p atr_sample1.atr2 # => atr2
107
+
108
+ # can not use writer.
109
+ # atr_sample2 = AccessorSample.new do |a|
110
+ # a.atr1 = 'atr1'
111
+ # a.atr2 = 'atr2'
112
+ # end
113
+ ~~~
114
+
115
+ same mean code is
116
+ ~~~ruby
117
+ class AccessorSample
118
+ attr_reader :atr1, :atr2
119
+
120
+ def initialize(values = nil, &block)
121
+ return yield self if block
122
+ @atr1 = values[:atr1]
123
+ @atr2 = values[:atr2]
124
+ end
125
+ end
126
+
127
+ atr_sample1 = AccessorSample.new :atr1 => 'atr1', :atr2 => 'atr2'
128
+ p atr_sample1.atr1 # => atr1
129
+ p atr_sample1.atr2 # => atr2
130
+
131
+ # can not use writer.
132
+ # atr_sample2 = AccessorSample.new do |a|
133
+ # a.atr1 = 'atr1'
134
+ # a.atr2 = 'atr2'
135
+ # end
136
+ ~~~
137
+
138
+ ### AttributesInitializable::ClassMethods.attr_writer_init
139
+ ~~~ruby
140
+ require 'attributes_initializable'
141
+
142
+ class AccessorSample
143
+ include AttributesInitializable
144
+ attr_writer_init :atr1, :atr2
145
+ end
146
+
147
+ atr_sample1 = AccessorSample.new :atr1 => 'atr1', :atr2 => 'atr2'
148
+ # can not use reader
149
+ # p atr_sample1.atr1 # => atr1
150
+ # p atr_sample1.atr2 # => atr2
151
+ atr_sample1.instance_variable_get "@atr1" # => atr1
152
+ atr_sample1.instance_variable_get "@atr2" # => atr2
153
+
154
+ atr_sample2 = AccessorSample.new do |a|
155
+ a.atr1 = 'atr1'
156
+ a.atr2 = 'atr2'
157
+ end
158
+
159
+ # can not use reader
160
+ # p atr_sample2.atr1 # => atr1
161
+ # p atr_sample2.atr2 # => atr2
162
+ atr_sample2.instance_variable_get "@atr1" # => atr1
163
+ atr_sample2.instance_variable_get "@atr2" # => atr2
164
+ ~~~
165
+
166
+ same mean code is
167
+ ~~~ruby
168
+ class AccessorSample
169
+ attr_writer :atr1, :atr2
170
+
171
+ def initialize(values = nil, &block)
172
+ return yield self if block
173
+ @atr1 = values[:atr1]
174
+ @atr2 = values[:atr2]
175
+ end
176
+ end
177
+
178
+ atr_sample1 = AccessorSample.new :atr1 => 'atr1', :atr2 => 'atr2'
179
+ # can not use reader
180
+ # p atr_sample1.atr1 # => atr1
181
+ # p atr_sample1.atr2 # => atr2
182
+ atr_sample1.instance_variable_get "@atr1" # => atr1
183
+ atr_sample1.instance_variable_get "@atr2" # => atr2
184
+
185
+ atr_sample2 = AccessorSample.new do |a|
186
+ a.atr1 = 'atr1'
187
+ a.atr2 = 'atr2'
188
+ end
189
+ # can not use reader
190
+ # p atr_sample2.atr1 # => atr1
191
+ # p atr_sample2.atr2 # => atr2
192
+ atr_sample2.instance_variable_get "@atr1" # => atr1
193
+ atr_sample2.instance_variable_get "@atr2" # => atr2
194
+ ~~~
195
+
95
196
  ### Ghostable
96
197
  * include Ghostable
97
198
  * create ghost method by using Ghostable::ghost_method
@@ -426,6 +527,7 @@ if you are Sublime Text2 user, you can use snippet for TbpgrUtils.
426
527
  https://github.com/tbpgr/tbpgr_utils_snippets
427
528
 
428
529
  ## History
530
+ * version 0.0.12 : AttributesInitializable::ClassMethods.attr_reader_init,attr_writer_init
429
531
  * version 0.0.11 : add Object#to_bool.
430
532
  * version 0.0.10 : add TemplateMethodable module.
431
533
  * version 0.0.9 : add TestToolbox module. add Kernel#capture_stdout, Kernel#dp_line
@@ -10,25 +10,54 @@ module AttributesInitializable
10
10
  # params
11
11
  # - symbols : attribute names
12
12
  def attr_accessor_init(*symbols)
13
- generate_accessors(symbols)
13
+ generate_attr_accessor(symbols)
14
+ generate_initializer(symbols)
15
+ end
16
+
17
+ # generate attr_reader + initialize
18
+ # params
19
+ # - symbols : attribute names
20
+ def attr_reader_init(*symbols)
21
+ generate_attr_reader(symbols)
22
+ generate_initializer(symbols)
23
+ end
24
+
25
+ # generate attr_writer + initialize
26
+ # params
27
+ # - symbols : attribute names
28
+ def attr_writer_init(*symbols)
29
+ generate_attr_writer(symbols)
14
30
  generate_initializer(symbols)
15
31
  end
16
32
 
17
33
  private
18
34
 
19
- def generate_accessors(symbols)
20
- accessors = symbols.reduce([]) do |accessors, sym|
35
+ def generate_attr_accessor(symbols)
36
+ generate_attr symbols, :accessor
37
+ end
38
+
39
+ def generate_attr_reader(symbols)
40
+ generate_attr symbols, :reader
41
+ end
42
+
43
+ def generate_attr_writer(symbols)
44
+ generate_attr symbols, :writer
45
+ end
46
+
47
+ def generate_attr(symbols, type)
48
+ results = symbols.reduce([]) do |results, sym|
21
49
  fail TypeError, "invalid type #{sym.class}. you have to use Symbol" unless sym.class == Symbol
22
- accessors << ":#{sym.to_s}"
50
+ results << ":#{sym.to_s}"
23
51
  end
24
- class_eval "attr_accessor #{accessors.join(',')}"
52
+ class_eval "attr_#{type.to_s} #{results.join(',')}"
25
53
  end
26
54
 
27
55
  def generate_initializer(symbols)
28
56
  instance_eval do
29
57
  define_method :initialize do |values = nil, &block|
30
58
  return block.call self if block
31
- symbols.each { |symbol|method("#{symbol.to_s}=").call values[symbol] }
59
+ # symbols.each { |symbol|send("#{symbol.to_s}=", values[symbol]) }
60
+ symbols.each { |symbol|self.instance_variable_set("@#{symbol.to_s}", values[symbol]) }
32
61
  end
33
62
  end
34
63
  end
@@ -2,5 +2,5 @@
2
2
 
3
3
  # Tbpgr Utilities
4
4
  module TbpgrUtils
5
- VERSION = '0.0.11'
5
+ VERSION = '0.0.12'
6
6
  end
@@ -4,7 +4,7 @@ require 'attributes_initializable'
4
4
 
5
5
  describe AttributesInitializable do
6
6
  context :attr_accessor_init do
7
- class AccessorSample
7
+ class AccessorAccessorSample
8
8
  include AttributesInitializable
9
9
  attr_accessor_init :atr1, :atr2
10
10
  end
@@ -13,7 +13,7 @@ describe AttributesInitializable do
13
13
  {
14
14
  case_no: 1,
15
15
  case_title: 'not block case',
16
- klass: AccessorSample,
16
+ klass: AccessorAccessorSample,
17
17
  inputs: {
18
18
  atr1: 'atr1',
19
19
  atr2: 'atr2',
@@ -27,7 +27,7 @@ describe AttributesInitializable do
27
27
  {
28
28
  case_no: 2,
29
29
  case_title: 'block case',
30
- klass: AccessorSample,
30
+ klass: AccessorAccessorSample,
31
31
  inputs: {
32
32
  atr1: 'atr1',
33
33
  atr2: 'atr2',
@@ -77,4 +77,158 @@ describe AttributesInitializable do
77
77
  end
78
78
  end
79
79
  end
80
+
81
+ context :attr_reader_init do
82
+ class ReaderSample
83
+ include AttributesInitializable
84
+ attr_reader_init :atr1, :atr2
85
+ end
86
+
87
+ cases = [
88
+ {
89
+ case_no: 1,
90
+ case_title: 'not block case',
91
+ klass: ReaderSample,
92
+ inputs: {
93
+ atr1: 'atr1',
94
+ atr2: 'atr2',
95
+ },
96
+ block: false,
97
+ expected: {
98
+ atr1: 'atr1',
99
+ atr2: 'atr2',
100
+ },
101
+ },
102
+ {
103
+ case_no: 2,
104
+ case_title: 'cant use writer case',
105
+ klass: ReaderSample,
106
+ inputs: {
107
+ atr1: 'atr1',
108
+ atr2: 'atr2',
109
+ },
110
+ block: true,
111
+ expected: {
112
+ atr1: 'atr1',
113
+ atr2: 'atr2',
114
+ },
115
+ },
116
+ ]
117
+
118
+ cases.each do |c|
119
+ it "|case_no=#{c[:case_no]}|case_title=#{c[:case_title]}" do
120
+ begin
121
+ case_before c
122
+
123
+ # -- given --
124
+ # nothing
125
+
126
+ # -- when --
127
+ reader_sample = nil
128
+ if c[:block]
129
+ # -- then --
130
+ lambda {
131
+ reader_sample = c[:klass].new do |a|
132
+ a.atr1 = c[:inputs][:atr1]
133
+ a.atr2 = c[:inputs][:atr2]
134
+ end
135
+ }.should raise_error(NoMethodError)
136
+ next
137
+ else
138
+ reader_sample = c[:klass].new atr1: 'atr1', atr2: 'atr2'
139
+ end
140
+
141
+ # -- then --
142
+ c[:expected].each do |key, value|
143
+ expect(reader_sample.method(key).call).to eq(value)
144
+ end
145
+ ensure
146
+ case_after c
147
+ end
148
+ end
149
+
150
+ def case_before(c)
151
+ # implement each case before
152
+ end
153
+
154
+ def case_after(c)
155
+ # implement each case after
156
+ end
157
+ end
158
+ end
159
+
160
+ context :attr_writer_init do
161
+ class WriterSample
162
+ include AttributesInitializable
163
+ attr_writer_init :atr1, :atr2
164
+ end
165
+
166
+ cases = [
167
+ {
168
+ case_no: 1,
169
+ case_title: 'not block case',
170
+ klass: WriterSample,
171
+ inputs: {
172
+ atr1: 'atr1',
173
+ atr2: 'atr2',
174
+ },
175
+ block: false,
176
+ expected: {
177
+ atr1: 'atr1',
178
+ atr2: 'atr2',
179
+ },
180
+ },
181
+ {
182
+ case_no: 2,
183
+ case_title: 'block case',
184
+ klass: WriterSample,
185
+ inputs: {
186
+ atr1: 'atr1',
187
+ atr2: 'atr2',
188
+ },
189
+ block: true,
190
+ expected: {
191
+ atr1: 'atr1',
192
+ atr2: 'atr2',
193
+ },
194
+ },
195
+ ]
196
+
197
+ cases.each do |c|
198
+ it "|case_no=#{c[:case_no]}|case_title=#{c[:case_title]}" do
199
+ begin
200
+ case_before c
201
+
202
+ # -- given --
203
+ # nothing
204
+
205
+ # -- when --
206
+ writer_sample = nil
207
+ if c[:block]
208
+ writer_sample = c[:klass].new do |a|
209
+ a.atr1 = c[:inputs][:atr1]
210
+ a.atr2 = c[:inputs][:atr2]
211
+ end
212
+ else
213
+ writer_sample = c[:klass].new atr1: 'atr1', atr2: 'atr2'
214
+ end
215
+
216
+ # -- then --
217
+ c[:expected].each do |key, value|
218
+ expect(writer_sample.instance_variable_get("@#{key}")).to eq(value)
219
+ end
220
+ ensure
221
+ case_after c
222
+ end
223
+ end
224
+
225
+ def case_before(c)
226
+ # implement each case before
227
+ end
228
+
229
+ def case_after(c)
230
+ # implement each case after
231
+ end
232
+ end
233
+ end
80
234
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tbpgr_utils
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.11
4
+ version: 0.0.12
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-01-18 00:00:00.000000000 Z
12
+ date: 2014-01-19 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
16
- requirement: &28249812 !ruby/object:Gem::Requirement
16
+ requirement: &27383724 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: 4.0.1
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *28249812
24
+ version_requirements: *27383724
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: bundler
27
- requirement: &28249032 !ruby/object:Gem::Requirement
27
+ requirement: &27383028 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ~>
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '1.3'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *28249032
35
+ version_requirements: *27383028
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: rake
38
- requirement: &28247760 !ruby/object:Gem::Requirement
38
+ requirement: &27382476 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '0'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *28247760
46
+ version_requirements: *27382476
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: rspec
49
- requirement: &28245348 !ruby/object:Gem::Requirement
49
+ requirement: &28249968 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ~>
@@ -54,7 +54,7 @@ dependencies:
54
54
  version: 2.14.1
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *28245348
57
+ version_requirements: *28249968
58
58
  description: Utilities
59
59
  email:
60
60
  - tbpgr@tbpgr.jp