tbpgr_utils 0.0.135 → 0.0.136

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: b98d24e3c4feef51fa216b88cb76d69910a0c86a
4
- data.tar.gz: d7c95e332b3f975abf0dc358922655f7c07ae46f
3
+ metadata.gz: a04fd972d51e9a1cca40f5ca22e5ce60c791fafb
4
+ data.tar.gz: 5058986a81274eda20e8daeb224c164308086256
5
5
  SHA512:
6
- metadata.gz: cbc83ca317bcaa82610c48d0313deb245851a34ae1b181520d86e9fc9274d4d287060df589bedf87272355881db7b7cd971e1c1bc351628877ba80ed86e46cb8
7
- data.tar.gz: 154ccd41ff57237b88ab9f7dbe00b7214afc2d147525180e243de7d20c44f2299f1fbc22404f3402c3564d7cb49788a6ffbb690f6c477e0f96f5ce16c8599121
6
+ metadata.gz: 963693f1d332754521be42cb9e0f28d507baa47b80eaf7ac5ec45c8885b3ebd494da3d50a3bfca46cb5e4fd5bae837d4cef96d1efbe76072a2d282f43f851437
7
+ data.tar.gz: 033f99cf39352ac6a032052eeb3299912cf05bf8997fa71298f972009ee81eaf0e15568f9832d5d888eda511148c49f1732bc366b1d99f5f61411a763acc8be9
data/README.md CHANGED
@@ -60,7 +60,8 @@ Or install it yourself as:
60
60
  |[TbpgrUtils Array#together_slice](#arraytogether_sliceor-tslice) |together version of Array#slice. together_slice has alias :tslice |
61
61
  |[TbpgrUtils Array#together_with_index](#arraytogether_with_index) |loop all arrays by block with index |
62
62
  |[TbpgrUtils Array#uniq_size](#arrayuniq_size) |return uniq size |
63
- |[AttrEnumerable#each_attr](#attrenumerable_each_attr) |define each_xxx. it call Class attributes(collection)'s attribute iterator |
63
+ |[AttrEnumerable.each_attr](#attrenumerableeach_attr) |define each_xxx. it call Class attributes(collection)'s attribute iterator |
64
+ |[AttrEnumerable.each_attr_with_index](#attrenumerableeach_attr_with_index) |define each_xxx_with_index. it call Class attributes(collection)'s attribute iterator with index |
64
65
  |[AttributesHashable.to_hash](#attributeshashableto_hash) |define to_hash method for get instance_values |
65
66
  |[AttributesInitializable::ClassMethods.attr_accessor_init](#attributesinitializableclassmethodsattr_accessor_init) |generate attr_accessor + initializer |
66
67
  |[AttributesInitializable::ClassMethods.attr_reader_init](#attributesinitializableclassmethodsattr_reader_init) |generate attr_reader + initializer |
@@ -1101,6 +1102,40 @@ require 'tbpgr_utils'
1101
1102
 
1102
1103
  [back to list](#list)
1103
1104
 
1105
+ ### AttrEnumerable.each_attr_with_index
1106
+ ~~~ruby
1107
+ require 'attr_enumerable'
1108
+ class Person
1109
+ attr_reader :name, :age
1110
+ def initialize(name, age)
1111
+ @name, @age = name, age
1112
+ end
1113
+ end
1114
+
1115
+ class Persons
1116
+ attr_reader :persons
1117
+ include AttrEnumerable
1118
+ def initialize(persons = [])
1119
+ @persons = persons
1120
+ end
1121
+
1122
+ def <<(person)
1123
+ @persons << person
1124
+ end
1125
+ end
1126
+
1127
+ persons = Persons.new([Person.new("tanaka", 84), Person.new("suzuki", 103)])
1128
+ persons.each_name_with_index do |name, i|
1129
+ puts "#{name}:#{i}" # => "tanaka:0", "suzuki:1"
1130
+ end
1131
+
1132
+ persons.each_age_with_index do |age, i|
1133
+ puts "#{age.to_s}:#{i}" # => "84:0", "103:0"
1134
+ end
1135
+ ~~~
1136
+
1137
+ [back to list](#list)
1138
+
1104
1139
  ### AttrEnumerable.each_attr
1105
1140
  ~~~ruby
1106
1141
  require 'attr_enumerable'
@@ -3837,6 +3872,7 @@ if you are Sublime Text2 user, you can use snippet for TbpgrUtils.
3837
3872
  https://github.com/tbpgr/tbpgr_utils_snippets
3838
3873
 
3839
3874
  ## History
3875
+ * version 0.0.136 : add AttrEnumerable.each_attr_with_index
3840
3876
  * version 0.0.135 : add AttrEnumerable.each_attr
3841
3877
  * version 0.0.134 : add Integer#reverse_each_digit
3842
3878
  * version 0.0.133 : add Integer#each_digit_with_index
@@ -0,0 +1,38 @@
1
+ # encoding: utf-8
2
+ require 'active_support/inflector'
3
+
4
+ # AttrEnumerable
5
+ module AttrEnumerable
6
+ ATTR_METHODS = [
7
+ { regexp: /^each_(.*)_with_index$/, call_method: :each_attr_with_index },
8
+ { regexp: /^each_(.*)$/, call_method: :each_attr }
9
+ ]
10
+
11
+ # call attr enumerable method.
12
+ def method_missing(method_name, *args, &block)
13
+ attr_method = detect(method_name)
14
+ send(attr_method[:call_method], attr_method[:attribute], &block)
15
+ rescue
16
+ super(method_name, *args, &block)
17
+ end
18
+
19
+ private
20
+ def detect(method_name)
21
+ ATTR_METHODS.each do |attr_method|
22
+ regexp = attr_method[:regexp]
23
+ if method_name.to_s =~ regexp
24
+ attribute = method_name.to_s.scan(regexp).first.first
25
+ return { call_method: attr_method[:call_method], attribute: attribute }
26
+ end
27
+ end
28
+ fail NoMethodError, "method is not exists #{method_name}"
29
+ end
30
+
31
+ def include_attr?(element, attribute)
32
+ element.instance_variables.include? :"@#{attribute}"
33
+ end
34
+
35
+ def collection
36
+ instance_variable_get("@#{self.class.name.underscore}")
37
+ end
38
+ end
@@ -0,0 +1,14 @@
1
+ # encoding: utf-8
2
+ require 'active_support/inflector'
3
+ require 'attr_enumerable/attr_enumerable_helper'
4
+
5
+ # AttrEnumerable
6
+ module AttrEnumerable
7
+ private
8
+ def each_attr(attribute, &block)
9
+ collection.each do |element|
10
+ super(method_name, *args) unless include_attr?(element, attribute)
11
+ yield element.send(attribute)
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,14 @@
1
+ # encoding: utf-8
2
+ require 'active_support/inflector'
3
+ require 'attr_enumerable/attr_enumerable_helper'
4
+
5
+ # AttrEnumerable
6
+ module AttrEnumerable
7
+ private
8
+ def each_attr_with_index(attribute, &block)
9
+ collection.each_with_index do |element, index|
10
+ super(method_name, *args) unless include_attr?(element, attribute)
11
+ yield element.send(attribute), index
12
+ end
13
+ end
14
+ end
@@ -1,29 +1,7 @@
1
1
  # encoding: utf-8
2
- require 'active_support/inflector'
3
2
 
4
3
  # AttrEnumerable
5
4
  module AttrEnumerable
6
- # define each_xxx.
7
- #
8
- # xxx is attribute name.
9
- # Class XxxxYyyys must have attributes xxxx_yyyys.
10
- # xxxx_yyyys is collection of Class XxxxYyyy.
11
- # XxxxYyyy has attributes.
12
- def method_missing(method_name, *args, &block)
13
- if method_name =~ /each_/
14
- attribute = method_name.to_s.scan(/each_(.*)/).first.first
15
- each_attr(attribute, &block)
16
- else
17
- super(method_name, *args, &block)
18
- end
19
- end
20
-
21
- private
22
- def each_attr(attribute, &block)
23
- collection = self.instance_variable_get("@#{self.class.name.underscore}")
24
- collection.each do |element|
25
- super(method_name, *args) unless element.instance_variables.include? :"@#{attribute}"
26
- yield element.send(attribute)
27
- end
28
- end
5
+ require 'attr_enumerable/each_attr'
6
+ require 'attr_enumerable/each_attr_with_index'
29
7
  end
@@ -2,5 +2,5 @@
2
2
 
3
3
  # Tbpgr Utilities
4
4
  module TbpgrUtils
5
- VERSION = '0.0.135'
5
+ VERSION = '0.0.136'
6
6
  end
@@ -29,8 +29,8 @@ describe AttrEnumerable do
29
29
  case_title: 'name case',
30
30
  klass: AttrEnumerablePersons.new(
31
31
  [
32
- AttrEnumerablePerson.new("tanaka", 84),
33
- AttrEnumerablePerson.new("suzuki", 103)
32
+ AttrEnumerablePerson.new('tanaka', 84),
33
+ AttrEnumerablePerson.new('suzuki', 103)
34
34
  ]
35
35
  ),
36
36
  method: :each_name,
@@ -41,8 +41,8 @@ describe AttrEnumerable do
41
41
  case_title: 'age case',
42
42
  klass: AttrEnumerablePersons.new(
43
43
  [
44
- AttrEnumerablePerson.new("tanaka", 84),
45
- AttrEnumerablePerson.new("suzuki", 103)
44
+ AttrEnumerablePerson.new('tanaka', 84),
45
+ AttrEnumerablePerson.new('suzuki', 103)
46
46
  ]
47
47
  ),
48
48
  method: :each_age,
@@ -0,0 +1,81 @@
1
+ # encoding: utf-8
2
+ require 'spec_helper'
3
+ require 'attr_enumerable'
4
+
5
+ describe AttrEnumerable do
6
+ context :each_attr_with_index do
7
+ class EachAttrWithIndexPerson
8
+ attr_reader :name, :age
9
+ def initialize(name, age)
10
+ @name, @age = name, age
11
+ end
12
+ end
13
+
14
+ class EachAttrWithIndexPersons
15
+ attr_reader :each_attr_with_index_persons
16
+ include AttrEnumerable
17
+ def initialize(persons = [])
18
+ @each_attr_with_index_persons = persons
19
+ end
20
+
21
+ def <<(person)
22
+ @each_attr_with_index_persons << person
23
+ end
24
+ end
25
+
26
+ cases = [
27
+ {
28
+ case_no: 1,
29
+ case_title: 'name case',
30
+ klass: EachAttrWithIndexPersons.new(
31
+ [
32
+ EachAttrWithIndexPerson.new('tanaka', 84),
33
+ EachAttrWithIndexPerson.new('suzuki', 103)
34
+ ]
35
+ ),
36
+ method: :each_name_with_index,
37
+ expected: ['tanaka:0', 'suzuki:1']
38
+ },
39
+ {
40
+ case_no: 2,
41
+ case_title: 'age case',
42
+ klass: EachAttrWithIndexPersons.new(
43
+ [
44
+ EachAttrWithIndexPerson.new('tanaka', 84),
45
+ EachAttrWithIndexPerson.new('suzuki', 103)
46
+ ]
47
+ ),
48
+ method: :each_age_with_index,
49
+ expected: ['84:0', '103:1']
50
+ },
51
+ ]
52
+
53
+ cases.each do |c|
54
+ it "|case_no=#{c[:case_no]}|case_title=#{c[:case_title]}" do
55
+ begin
56
+ case_before c
57
+
58
+ # -- given --
59
+ # nothing
60
+
61
+ # -- when --
62
+ ret = []
63
+ c[:klass].send c[:method] { |attribute, index| ret << attribute.to_s + ":#{index}" }
64
+
65
+ # -- then --
66
+ expect(ret).to eq(c[:expected])
67
+ ensure
68
+ case_after c
69
+ end
70
+ end
71
+
72
+ def case_before(c)
73
+ # implement each case before
74
+ end
75
+
76
+ def case_after(c)
77
+ # implement each case after
78
+ end
79
+ end
80
+ end
81
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tbpgr_utils
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.135
4
+ version: 0.0.136
5
5
  platform: ruby
6
6
  authors:
7
7
  - tbpgr
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-05-22 00:00:00.000000000 Z
11
+ date: 2014-05-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -97,6 +97,9 @@ files:
97
97
  - README.md
98
98
  - Rakefile
99
99
  - lib/attr_enumerable.rb
100
+ - lib/attr_enumerable/attr_enumerable_helper.rb
101
+ - lib/attr_enumerable/each_attr.rb
102
+ - lib/attr_enumerable/each_attr_with_index.rb
100
103
  - lib/attributes_hashable.rb
101
104
  - lib/attributes_initializable.rb
102
105
  - lib/end_erb.rb
@@ -253,7 +256,8 @@ files:
253
256
  - lib/test_toolbox.rb
254
257
  - lib/test_toolbox/kernel.rb
255
258
  - rubocop-todo.yml
256
- - spec/attr_enumerable_spec.rb
259
+ - spec/attr_enumerable/each_attr_spec.rb
260
+ - spec/attr_enumerable/each_attr_with_index_spec.rb
257
261
  - spec/attributes_hashable_spec.rb
258
262
  - spec/attributes_initializable_spec.rb
259
263
  - spec/eval_helper/attr_accessor_init_code_spec.rb
@@ -417,7 +421,8 @@ signing_key:
417
421
  specification_version: 4
418
422
  summary: Utilities
419
423
  test_files:
420
- - spec/attr_enumerable_spec.rb
424
+ - spec/attr_enumerable/each_attr_spec.rb
425
+ - spec/attr_enumerable/each_attr_with_index_spec.rb
421
426
  - spec/attributes_hashable_spec.rb
422
427
  - spec/attributes_initializable_spec.rb
423
428
  - spec/eval_helper/attr_accessor_init_code_spec.rb