tbpgr_utils 0.0.18 → 0.0.19

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -18,32 +18,33 @@ Or install it yourself as:
18
18
 
19
19
  ## Usage
20
20
  ### List
21
- | class/module/method | mean |
22
- |:----------- |:------------ |
23
- |TbpgrUtils Array#together |loop all arrays by block |
24
- |TbpgrUtils Array#together_concat |together version of Array#concat. together_concat has alias :tconcat |
25
- |TbpgrUtils Array#together_map |together version of Enumerable#map. together_map has aliases [:tmap, :together_collect, :tcollect] |
26
- |TbpgrUtils Array#together_reduce |together version of Enumerable#reduce. together_reduce has aliases [:treduce, :together_inject, :tinject] |
27
- |TbpgrUtils Array#together_select |together version of Enumerable#select. together_select has aliases [:tselect, :together_find_all, :tfindall]|
28
- |TbpgrUtils Array#together_with_index |loop all arrays by block with index |
29
- |AttributesInitializable::ClassMethods.attr_accessor_init|generate attr_accessor + initializer |
30
- |AttributesInitializable::ClassMethods.attr_reader_init |generate attr_reader + initializer |
31
- |AttributesInitializable::ClassMethods.attr_writer init |generate attr_writer + initializer |
32
- |Ghostable module |help to create ghost method(dynamic method define by ussing method_missing + pattern-method-name) |
33
- |TbpgrUtils Kernel#bulk_define_methods |define methods to classes. methods have simple return value. |
34
- |TestToolbox Kernel#capture_stdout |capture STDOUT |
35
- |TestToolbox Kernel#dp_line |debug print line for print-debugging |
36
- |TbpgrUtils Kernel#print_eval |Print code + eval result |
37
- |TbpgrUtils Kernel#puts_eval |Puts code + eval result |
38
- |TbpgrUtils Kernel#bulk_puts_eval |Puts each-line-code + eval result |
39
- |TbpgrUtils Module.alias_methods |create alias methods |
40
- |TbpgrUtils Object#any_of? |if self match any one of items, return true |
41
- |TbpgrUtils Object#boolean? |data type check for boolean |
42
- |TbpgrUtils Object#my_methods |return public/protected/private self define methods |
43
- |TbpgrUtils Object#to_bool |syntax sugar of !!. convert [false, nil] => fasel, other => true. |
44
- |TbpgrUtils String#justify_table |justify pipe format table string |
45
- |Templatable module |get result from template + placeholder |
46
- |TemplateMethodable module |for Template Method Pattern |
21
+ | class/module/method | mean |
22
+ |:----------- |:------------ |
23
+ |[TbpgrUtils Array#together](#arraytogether) |loop all arrays by block |
24
+ |[TbpgrUtils Array#together_concat](#arraytogether_concat) |together version of Array#concat. together_concat has alias :tconcat |
25
+ |[TbpgrUtils Array#together_map](#arraytogether_mapor-tmap-together_collect-tcollect) |together version of Enumerable#map. together_map has aliases [:tmap, :together_collect, :tcollect] |
26
+ |[TbpgrUtils Array#together_reduce](#arraytogether_reduceor-treduce-together_inject-tinject) |together version of Enumerable#reduce. together_reduce has aliases [:treduce, :together_inject, :tinject] |
27
+ |[TbpgrUtils Array#together_select](#arraytogether_selector-tselect-together_find_all-tfindall) |together version of Enumerable#select. together_select has aliases [:tselect, :together_find_all, :tfindall] |
28
+ |[TbpgrUtils Array#together_with_index](#arraytogether_with_index) |loop all arrays by block with index |
29
+ |[AttributesHashable.to_hash](#attributeshashableto_hash) |define to_hash method for get instance_values |
30
+ |[AttributesInitializable::ClassMethods.attr_accessor_init](#attributesinitializableclassmethodsattr_accessor_init) |generate attr_accessor + initializer |
31
+ |[AttributesInitializable::ClassMethods.attr_reader_init](#attributesinitializableclassmethodsattr_reader_init) |generate attr_reader + initializer |
32
+ |[AttributesInitializable::ClassMethods.attr_writer init](#attributesinitializableclassmethodsattr_writer_init) |generate attr_writer + initializer |
33
+ |[Ghostable module](#ghostable) |help to create ghost method(dynamic method define by ussing method_missing + pattern-method-name) |
34
+ |[TbpgrUtils Kernel#bulk_define_methods](#kernelbulk_define_methods) |define methods to classes. methods have simple return value. |
35
+ |[TestToolbox Kernel#capture_stdout](#kernelcapture_stdout) |capture STDOUT |
36
+ |[TestToolbox Kernel#dp_line](#kerneldp_line) |debug print line for print-debugging |
37
+ |[TbpgrUtils Kernel#print_eval](#kernelprint_eval) |Print code + eval result |
38
+ |[TbpgrUtils Kernel#puts_eval](#kernelputs_eval) |Puts code + eval result |
39
+ |[TbpgrUtils Kernel#bulk_puts_eval](#kernelbulk_puts_eval) |Puts each-line-code + eval result |
40
+ |[TbpgrUtils Module.alias_methods](#modulealias_methods) |create alias methods |
41
+ |[TbpgrUtils Object#any_of?](#objectany_of) |if self match any one of items, return true |
42
+ |[TbpgrUtils Object#boolean?](#objectboolean) |data type check for boolean |
43
+ |[TbpgrUtils Object#my_methods](#objectmy_methods) |return public/protected/private self define methods |
44
+ |[TbpgrUtils Object#to_bool](#objectto_bool) |syntax sugar of !!. convert [false, nil] => fasel, other => true. |
45
+ |[TbpgrUtils String#justify_table](#stringjustify_table) |justify pipe format table string |
46
+ |[Templatable module](#templatable) |get result from template + placeholder |
47
+ |[TemplateMethodable module](#templatemethodable) |for Template Method Pattern |
47
48
 
48
49
  ### Array#together
49
50
  ~~~ruby
@@ -56,6 +57,8 @@ numbers = %w{1 2 3}
56
57
  end
57
58
  ~~~
58
59
 
60
+ [back to list](#list)
61
+
59
62
  ### Array#together_concat
60
63
  ~~~ruby
61
64
  require 'tbpgr_utils'
@@ -68,6 +71,8 @@ print alpha # => ["one", "two", "three", 4, 5, 6]
68
71
  print numbers # => ["1", "2", "3", 4, 5, 6]
69
72
  ~~~
70
73
 
74
+ [back to list](#list)
75
+
71
76
  ### Array#together_map(or tmap, together_collect, tcollect)
72
77
  ~~~ruby
73
78
  require 'tbpgr_utils'
@@ -88,6 +93,8 @@ ret = [alpha, numbers].together_map {|first, second|[["#{first}:ret"], ["#{secon
88
93
  print ret # => output [["one:ret", "two:ret", "three:ret"],["1:ret", "2:ret", "3:ret"]]
89
94
  ~~~
90
95
 
96
+ [back to list](#list)
97
+
91
98
  ### Array#together_reduce(or :treduce, :together_inject, :tinject)
92
99
  * if you want to single return
93
100
 
@@ -134,6 +141,8 @@ ret = [firsts, seconds].together_reduce({}){|memo, first, second|memo[first] = s
134
141
  print ret # => output {1=>4, 2=>2, 3=>3, 4=>1}
135
142
  ~~~
136
143
 
144
+ [back to list](#list)
145
+
137
146
  ### Array#together_select(or tselect, together_find_all, tfindall)
138
147
  ~~~ruby
139
148
  require 'tbpgr_utils'
@@ -154,6 +163,8 @@ ret = [firsts, seconds].together_select{|first, second|[first.odd?, second.even?
154
163
  print ret # => output [[1, 3], [4, 2]]
155
164
  ~~~
156
165
 
166
+ [back to list](#list)
167
+
157
168
  ### Array#together_with_index
158
169
  ~~~ruby
159
170
  require 'tbpgr_utils'
@@ -165,6 +176,32 @@ numbers = %w{1 2 3}
165
176
  end
166
177
  ~~~
167
178
 
179
+ [back to list](#list)
180
+
181
+ ### AttributesHashable.to_hash
182
+ ~~~ruby
183
+ require 'attributes_initializable'
184
+ require 'attributes_hashable'
185
+
186
+ class Hoge
187
+ include AttributesInitializable
188
+ attr_accessor_init :hoge, :hige
189
+ include AttributesHashable
190
+ end
191
+
192
+ hoge = Hoge.new do |h|
193
+ h.hoge = 'hoge'
194
+ h.hige = 'hige'
195
+ end
196
+
197
+ hoge.to_hash # => {:hoge=>"hoge", :hige=>"hige"}
198
+
199
+ # After include AttributesHashable, you can use Hash.try_convert.
200
+ Hash.try_convert hoge # => {:hoge=>"hoge", :hige=>"hige"}
201
+ ~~~
202
+
203
+ [back to list](#list)
204
+
168
205
  ### AttributesInitializable::ClassMethods.attr_accessor_init
169
206
  ~~~ruby
170
207
  require 'attributes_initializable'
@@ -210,6 +247,8 @@ p atr_sample2.atr1 # => atr1
210
247
  p atr_sample2.atr2 # => atr2
211
248
  ~~~
212
249
 
250
+ [back to list](#list)
251
+
213
252
  ### AttributesInitializable::ClassMethods.attr_reader_init
214
253
  ~~~ruby
215
254
  require 'attributes_initializable'
@@ -253,6 +292,8 @@ p atr_sample1.atr2 # => atr2
253
292
  # end
254
293
  ~~~
255
294
 
295
+ [back to list](#list)
296
+
256
297
  ### AttributesInitializable::ClassMethods.attr_writer_init
257
298
  ~~~ruby
258
299
  require 'attributes_initializable'
@@ -311,6 +352,8 @@ atr_sample2.instance_variable_get "@atr1" # => atr1
311
352
  atr_sample2.instance_variable_get "@atr2" # => atr2
312
353
  ~~~
313
354
 
355
+ [back to list](#list)
356
+
314
357
  ### Ghostable
315
358
  * include Ghostable
316
359
  * create ghost method by using Ghostable::ghost_method
@@ -358,6 +401,8 @@ sample.contain_hoge? "test_hige_test" # => return false
358
401
  sample.contain_hige? "test_hige_test" # => return true
359
402
  ~~~
360
403
 
404
+ [back to list](#list)
405
+
361
406
  ### Kernel#capture_stdout
362
407
  capture STDOUT to String. This method can use in STDOUT contents test.
363
408
 
@@ -370,6 +415,8 @@ result = capture_stdout {puts "test"} # => "test"
370
415
  result = capture_stdout {sleep 0.1} # => ""(empty)
371
416
  ~~~
372
417
 
418
+ [back to list](#list)
419
+
373
420
  ### Kernel#dp_line
374
421
  debug print line for print-debugging.
375
422
 
@@ -392,6 +439,8 @@ dp_line __LINE__, filename: __FILE__, char: '@'
392
439
  # => @@@@@@@@@@@@@@@@@@@@|filename=xx|line=yy$|@@@@@@@@@@@@@@@@@@@@\n
393
440
  ~~~
394
441
 
442
+ [back to list](#list)
443
+
395
444
  ### Kernel#bulk_define_methods
396
445
  Define methods to classes. Methods have simple return value.
397
446
 
@@ -441,6 +490,8 @@ class FalseClass
441
490
  end
442
491
  ~~~
443
492
 
493
+ [back to list](#list)
494
+
444
495
  ### Kernel#print_eval
445
496
  This method for sample code. for manual, for blog-entry's-snippet ...etc.
446
497
 
@@ -456,6 +507,8 @@ output
456
507
  8/4 # => 2"hoge-#{message}" # => "hoge-msg"
457
508
  ~~~
458
509
 
510
+ [back to list](#list)
511
+
459
512
  ### Kernel#puts_eval
460
513
  This method for sample code. for manual, for blog-entry's-snippet ...etc.
461
514
 
@@ -472,6 +525,8 @@ output
472
525
  "hoge-#{message}" # => "hoge-msg"
473
526
  ~~~
474
527
 
528
+ [back to list](#list)
529
+
475
530
  ### Kernel#bulk_puts_eval
476
531
  multi line version of puts_eval.
477
532
 
@@ -489,6 +544,8 @@ output
489
544
  "hoge-hige2" + "add" + message # => "hoge-hige2addmsg"
490
545
  ~~~
491
546
 
547
+ [back to list](#list)
548
+
492
549
  ### Module.alias_methods
493
550
  create alias methods.
494
551
 
@@ -522,6 +579,8 @@ class Hoge
522
579
  end
523
580
  ~~~
524
581
 
582
+ [back to list](#list)
583
+
525
584
  ### Object#any_of?
526
585
  ~~~ruby
527
586
  require 'tbpgr_utils'
@@ -534,6 +593,8 @@ p 1.any_of? 1, 2, 3 # =>true
534
593
  p 4.any_of? 1, 2, 3 # =>false
535
594
  ~~~
536
595
 
596
+ [back to list](#list)
597
+
537
598
  ### Object#boolean?
538
599
  ~~~ruby
539
600
  require 'tbpgr_utils'
@@ -545,6 +606,8 @@ p "".boolean? # =>false
545
606
  p "true".boolean? # =>false
546
607
  ~~~
547
608
 
609
+ [back to list](#list)
610
+
548
611
  ### Object#my_methods
549
612
  ~~~ruby
550
613
  require 'tbpgr_utils'
@@ -565,6 +628,8 @@ end
565
628
  p Hoge.new.my_methods # =>[:hoge, :hige, :hege]
566
629
  ~~~
567
630
 
631
+ [back to list](#list)
632
+
568
633
  ### Object#to_bool
569
634
  ~~~ruby
570
635
  require 'tbpgr_utils'
@@ -577,6 +642,8 @@ p nil.to_bool # => false
577
642
  p 0.to_bool # => true
578
643
  ~~~
579
644
 
645
+ [back to list](#list)
646
+
580
647
  ### String#justify_table
581
648
  ~~~ruby
582
649
  require 'tbpgr_utils'
@@ -597,6 +664,8 @@ output
597
664
  |test |tester|aaaaaaaaaaaaaaaaaaaaaaatestest|
598
665
  ~~~
599
666
 
667
+ [back to list](#list)
668
+
600
669
  ### Templatable
601
670
  * include Templatable
602
671
  * set template by here-document
@@ -633,6 +702,8 @@ line1:hoge-sample
633
702
  line2:hige-sample
634
703
  ~~~
635
704
 
705
+ [back to list](#list)
706
+
636
707
  ## TemplateMethodable
637
708
  sample usage
638
709
 
@@ -695,6 +766,7 @@ if you are Sublime Text2 user, you can use snippet for TbpgrUtils.
695
766
  https://github.com/tbpgr/tbpgr_utils_snippets
696
767
 
697
768
  ## History
769
+ * version 0.0.19 : add AttributesHashable module.
698
770
  * version 0.0.18 : add Array#together_concat. together_concat has alias :tconcat
699
771
  * version 0.0.17 : add Array#together_reduce(or :treduce, :together_inject, :tinject)
700
772
  * version 0.0.16 : add Array#together_select(or tselect, together_find_all, tfindall)
@@ -0,0 +1,29 @@
1
+ # encoding: utf-8
2
+
3
+ # implement to_hash(instance_variable_name => key, instance_variable_value => value)
4
+ module AttributesHashable
5
+ # create hash(instance_variable_name => key, instance_variable_value => value)
6
+ #
7
+ # class Hoge have two_attributes(:hoge, :hige)
8
+ # class Hoge
9
+ # include AttributesInitializable
10
+ # attr_accessor_init :hoge, :hige
11
+ # include AttributesHashable
12
+ # end
13
+ #
14
+ # hoge = Hoge.new do |h|
15
+ # h.hoge = 'hoge'
16
+ # h.hige = 'hige'
17
+ # end
18
+ #
19
+ # hoge.to_hash # => {:hoge=>"hoge", :hige=>"hige"}
20
+ #
21
+ # After include AttributesHashable, you can use Hash.try_convert.
22
+ # Hash.try_convert hoge # => {:hoge=>"hoge", :hige=>"hige"}
23
+ def to_hash
24
+ instance_variables.reduce({}) do |hash, var|
25
+ hash[var.to_s.delete('@').to_sym] = instance_variable_get(var)
26
+ hash
27
+ end
28
+ end
29
+ end
@@ -56,7 +56,6 @@ module AttributesInitializable
56
56
  instance_eval do
57
57
  define_method :initialize do |values = nil, &block|
58
58
  return block.call self if block
59
- # symbols.each { |symbol|send("#{symbol.to_s}=", values[symbol]) }
60
59
  symbols.each { |symbol|instance_variable_set("@#{symbol.to_s}", values[symbol]) }
61
60
  end
62
61
  end
@@ -2,5 +2,5 @@
2
2
 
3
3
  # Tbpgr Utilities
4
4
  module TbpgrUtils
5
- VERSION = '0.0.18'
5
+ VERSION = '0.0.19'
6
6
  end
@@ -0,0 +1,63 @@
1
+ # encoding: utf-8
2
+ require 'spec_helper'
3
+ require 'attributes_hashable'
4
+ require 'attributes_initializable'
5
+
6
+ describe AttributesHashable do
7
+ context :to_hash do
8
+ class AttributesHashableHoge
9
+ include AttributesInitializable
10
+ attr_accessor_init :hoge, :hige
11
+ include AttributesHashable
12
+ end
13
+
14
+ class NoAttributeHoge
15
+ include AttributesHashable
16
+ end
17
+
18
+ cases = [
19
+ {
20
+ case_no: 1,
21
+ case_title: '2 attributes case',
22
+ klass: AttributesHashableHoge,
23
+ expected: { hoge: 'hoge', hige: 'hige' }
24
+ },
25
+ {
26
+ case_no: 2,
27
+ case_title: 'no attribute case',
28
+ klass: NoAttributeHoge,
29
+ expected: {}
30
+ },
31
+ ]
32
+
33
+ cases.each do |c|
34
+ it "|case_no=#{c[:case_no]}|case_title=#{c[:case_title]}" do
35
+ begin
36
+ case_before c
37
+
38
+ # -- given --
39
+ instance = c[:klass].new do |k|
40
+ k.hoge = 'hoge'
41
+ k.hige = 'hige'
42
+ end
43
+
44
+ # -- when --
45
+ actual = instance.to_hash
46
+
47
+ # -- then --
48
+ expect(actual).to eq(c[:expected])
49
+ ensure
50
+ case_after c
51
+ end
52
+ end
53
+
54
+ def case_before(c)
55
+ # implement each case before
56
+ end
57
+
58
+ def case_after(c)
59
+ # implement each case after
60
+ end
61
+ end
62
+ end
63
+ 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.18
4
+ version: 0.0.19
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-25 00:00:00.000000000 Z
12
+ date: 2014-01-26 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
16
- requirement: &28417764 !ruby/object:Gem::Requirement
16
+ requirement: &21860136 !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: *28417764
24
+ version_requirements: *21860136
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: bundler
27
- requirement: &28414692 !ruby/object:Gem::Requirement
27
+ requirement: &21859524 !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: *28414692
35
+ version_requirements: *21859524
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: rake
38
- requirement: &28414140 !ruby/object:Gem::Requirement
38
+ requirement: &21859044 !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: *28414140
46
+ version_requirements: *21859044
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: rspec
49
- requirement: &28434732 !ruby/object:Gem::Requirement
49
+ requirement: &21858420 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ~>
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: 2.14.1
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *28434732
57
+ version_requirements: *21858420
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: simplecov
60
- requirement: &28432356 !ruby/object:Gem::Requirement
60
+ requirement: &21857544 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ~>
@@ -65,7 +65,7 @@ dependencies:
65
65
  version: 0.8.2
66
66
  type: :development
67
67
  prerelease: false
68
- version_requirements: *28432356
68
+ version_requirements: *21857544
69
69
  description: Utilities
70
70
  email:
71
71
  - tbpgr@tbpgr.jp
@@ -79,6 +79,7 @@ files:
79
79
  - LICENSE.txt
80
80
  - README.md
81
81
  - Rakefile
82
+ - lib/attributes_hashable.rb
82
83
  - lib/attributes_initializable.rb
83
84
  - lib/ghostable.rb
84
85
  - lib/open_classes/array.rb
@@ -92,6 +93,7 @@ files:
92
93
  - lib/template_methodable.rb
93
94
  - lib/test_toolbox.rb
94
95
  - lib/test_toolbox/kernel.rb
96
+ - spec/attributes_hashable_spec.rb
95
97
  - spec/attributes_initializable_spec.rb
96
98
  - spec/ghostable_spec.rb
97
99
  - spec/open_classes/array_spec.rb
@@ -130,6 +132,7 @@ signing_key:
130
132
  specification_version: 3
131
133
  summary: Utilities
132
134
  test_files:
135
+ - spec/attributes_hashable_spec.rb
133
136
  - spec/attributes_initializable_spec.rb
134
137
  - spec/ghostable_spec.rb
135
138
  - spec/open_classes/array_spec.rb