tbpgr_utils 0.0.137 → 0.0.138

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: 355d0061397d2091ab112230f5cf80b9c233359a
4
- data.tar.gz: 78dc4e5796f88f731af8317200ca721b3128867a
3
+ metadata.gz: c45387fe37df5872e279a11c7cbce12626f8fe90
4
+ data.tar.gz: 393ee517fb8167901c950b29e81d64ff1bedfffb
5
5
  SHA512:
6
- metadata.gz: 5bfc588eb1ca866bbdff7e77f4ee7ea93c2d123772f6f8c379087dbdd59425a08cb09a64ff8469eaa204251210cbae322e247c02e5f81aa9b255d70d7298a2bf
7
- data.tar.gz: 5a3a00830e0693bf526f30f8320fd9f06aa8bda96b87e80ef76caffb943df8655c8e8da15c39fa93c29cede482c870acfc614e06fbb77849d57f786e4790e395
6
+ metadata.gz: 8110c1a9c47b53c74dbd78db50b7a65bbc4b29c0c42d4e41c39cc0f8f385e62564184cbc33d3abd2ad0ef573de49b839835827533518a933cf8e5644ac19f0ef
7
+ data.tar.gz: 9beef52ccfd63e1d3dfb78c26f3bd55c909cfeab607b6edbc14d46f43bb77ce742b3aa98909da28a11621782d223788fc4cbe1440ea6c157c648598eec86d9ba
data/README.md CHANGED
@@ -60,6 +60,7 @@ 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.at_attr](#attrenumerableat_attr) |define at_xxx. it returns Class attributes(collection)'s at result. |
63
64
  |[AttrEnumerable.each_attr](#attrenumerableeach_attr) |define each_xxx. it call Class attributes(collection)'s attribute iterator |
64
65
  |[AttrEnumerable.each_attr_with_index](#attrenumerableeach_attr_with_index) |define each_xxx_with_index. it call Class attributes(collection)'s attribute iterator with index |
65
66
  |[AttrEnumerable.reverse_attr](#attrenumerablereverse_attr) |define reverse_xxx. it returns Class attributes(collection)'s reverse Array |
@@ -1103,7 +1104,7 @@ require 'tbpgr_utils'
1103
1104
 
1104
1105
  [back to list](#list)
1105
1106
 
1106
- ### AttrEnumerable.each_attr_with_index
1107
+ ### AttrEnumerable.at_attr
1107
1108
  ~~~ruby
1108
1109
  require 'attr_enumerable'
1109
1110
  class Person
@@ -1126,21 +1127,21 @@ class Persons
1126
1127
  end
1127
1128
 
1128
1129
  persons = Persons.new([Person.new("tanaka", 84), Person.new("suzuki", 103)])
1129
- persons.each_name_with_index do |name, i|
1130
- puts "#{name}:#{i}" # => "tanaka:0", "suzuki:1"
1131
- end
1130
+ persons.at_name 0 # => 'tanaka'
1131
+ persons.at_name 1 # => 'suzuki'
1132
+ persons.at_name -1 # => 'suzuki'
1133
+ persons.at_age 0 # => 84
1134
+ persons.at_age 2 # => nil
1132
1135
 
1133
- persons.each_age_with_index do |age, i|
1134
- puts "#{age.to_s}:#{i}" # => "84:0", "103:0"
1135
- end
1136
+ persons = Persons.new([])
1137
+ persons.at_name 0 # => nil
1136
1138
  ~~~
1137
1139
 
1138
1140
  [back to list](#list)
1139
1141
 
1140
- ### AttrEnumerable.reverse_attr
1142
+ ### AttrEnumerable.each_attr
1141
1143
  ~~~ruby
1142
1144
  require 'attr_enumerable'
1143
-
1144
1145
  class Person
1145
1146
  attr_reader :name, :age
1146
1147
  def initialize(name, age)
@@ -1161,14 +1162,18 @@ class Persons
1161
1162
  end
1162
1163
 
1163
1164
  persons = Persons.new([Person.new("tanaka", 84), Person.new("suzuki", 103)])
1164
- print persons.reverse_name # => ['suzuki', 'tanaka']
1165
+ persons.each_name do |name|
1166
+ puts name # => "tanaka", "suzuki"
1167
+ end
1165
1168
 
1166
- print persons.reverse_age # => [103, 84]
1169
+ persons.each_age do |age|
1170
+ puts age # => 84, 103
1171
+ end
1167
1172
  ~~~
1168
1173
 
1169
1174
  [back to list](#list)
1170
1175
 
1171
- ### AttrEnumerable.each_attr
1176
+ ### AttrEnumerable.each_attr_with_index
1172
1177
  ~~~ruby
1173
1178
  require 'attr_enumerable'
1174
1179
  class Person
@@ -1191,17 +1196,48 @@ class Persons
1191
1196
  end
1192
1197
 
1193
1198
  persons = Persons.new([Person.new("tanaka", 84), Person.new("suzuki", 103)])
1194
- persons.each_name do |name|
1195
- puts name # => "tanaka", "suzuki"
1199
+ persons.each_name_with_index do |name, i|
1200
+ puts "#{name}:#{i}" # => "tanaka:0", "suzuki:1"
1196
1201
  end
1197
1202
 
1198
- persons.each_age do |age|
1199
- puts age # => 84, 103
1203
+ persons.each_age_with_index do |age, i|
1204
+ puts "#{age.to_s}:#{i}" # => "84:0", "103:0"
1200
1205
  end
1201
1206
  ~~~
1202
1207
 
1203
1208
  [back to list](#list)
1204
1209
 
1210
+ ### AttrEnumerable.reverse_attr
1211
+ ~~~ruby
1212
+ require 'attr_enumerable'
1213
+
1214
+ class Person
1215
+ attr_reader :name, :age
1216
+ def initialize(name, age)
1217
+ @name, @age = name, age
1218
+ end
1219
+ end
1220
+
1221
+ class Persons
1222
+ attr_reader :persons
1223
+ include AttrEnumerable
1224
+ def initialize(persons = [])
1225
+ @persons = persons
1226
+ end
1227
+
1228
+ def <<(person)
1229
+ @persons << person
1230
+ end
1231
+ end
1232
+
1233
+ persons = Persons.new([Person.new("tanaka", 84), Person.new("suzuki", 103)])
1234
+ print persons.reverse_name # => ['suzuki', 'tanaka']
1235
+
1236
+ print persons.reverse_age # => [103, 84]
1237
+ ~~~
1238
+
1239
+ [back to list](#list)
1240
+
1205
1241
  ### AttributesHashable.to_hash
1206
1242
  ~~~ruby
1207
1243
  require 'attributes_initializable'
@@ -3904,6 +3940,7 @@ if you are Sublime Text2 user, you can use snippet for TbpgrUtils.
3904
3940
  https://github.com/tbpgr/tbpgr_utils_snippets
3905
3941
 
3906
3942
  ## History
3943
+ * version 0.0.138 : add AttrEnumerable.at_attr
3907
3944
  * version 0.0.137 : add AttrEnumerable.reverse_attr
3908
3945
  * version 0.0.136 : add AttrEnumerable.each_attr_with_index
3909
3946
  * version 0.0.135 : add AttrEnumerable.each_attr
@@ -0,0 +1,15 @@
1
+ # encoding: utf-8
2
+ require 'attr_enumerable/attr_enumerable_helper'
3
+
4
+ # AttrEnumerable
5
+ module AttrEnumerable
6
+ private
7
+ def at_attr(attribute, method_name, *args, &block)
8
+ return nil if collection.empty?
9
+ index = Integer(args.first)
10
+ super(method_name, *args) unless include_attr?(collection.first, attribute)
11
+ element = collection.at(index)
12
+ return nil if element.nil?
13
+ element.send(attribute)
14
+ end
15
+ end
@@ -6,7 +6,8 @@ module AttrEnumerable
6
6
  ATTR_METHODS = [
7
7
  { regexp: /^each_(.*)_with_index$/, call_method: :each_attr_with_index },
8
8
  { regexp: /^each_(.*)$/, call_method: :each_attr },
9
- { regexp: /^reverse_(.*)$/, call_method: :reverse_attr }
9
+ { regexp: /^reverse_(.*)$/, call_method: :reverse_attr },
10
+ { regexp: /^at_(.*)$/, call_method: :at_attr }
10
11
  ]
11
12
 
12
13
  # call attr enumerable method.
@@ -1,8 +1,9 @@
1
- # encoding: utf-8
2
-
3
- # AttrEnumerable
4
- module AttrEnumerable
5
- require 'attr_enumerable/each_attr'
6
- require 'attr_enumerable/each_attr_with_index'
7
- require 'attr_enumerable/reverse_attr'
8
- end
1
+ # encoding: utf-8
2
+
3
+ # AttrEnumerable
4
+ module AttrEnumerable
5
+ require 'attr_enumerable/at_attr'
6
+ require 'attr_enumerable/each_attr'
7
+ require 'attr_enumerable/each_attr_with_index'
8
+ require 'attr_enumerable/reverse_attr'
9
+ end
@@ -2,5 +2,5 @@
2
2
 
3
3
  # Tbpgr Utilities
4
4
  module TbpgrUtils
5
- VERSION = '0.0.137'
5
+ VERSION = '0.0.138'
6
6
  end
@@ -0,0 +1,103 @@
1
+ # encoding: utf-8
2
+ require 'spec_helper'
3
+ require 'attr_enumerable'
4
+
5
+ describe AttrEnumerable do
6
+ context :at_attr do
7
+ class AttrEnumerablePerson
8
+ attr_reader :name, :age
9
+ def initialize(name, age)
10
+ @name, @age = name, age
11
+ end
12
+ end
13
+
14
+ class AttrEnumerablePersons
15
+ attr_reader :attr_enumerable_persons
16
+ include AttrEnumerable
17
+ def initialize(attr_enumerable_persons = [])
18
+ @attr_enumerable_persons = attr_enumerable_persons
19
+ end
20
+
21
+ def <<(person)
22
+ @attr_enumerable_persons << attr_enumerable_persons
23
+ end
24
+ end
25
+
26
+ cases = [
27
+ {
28
+ case_no: 1,
29
+ case_title: 'name case',
30
+ klass: AttrEnumerablePersons.new(
31
+ [
32
+ AttrEnumerablePerson.new('tanaka', 84),
33
+ AttrEnumerablePerson.new('suzuki', 103)
34
+ ]
35
+ ),
36
+ method: :at_name,
37
+ args: 0,
38
+ expected: 'tanaka'
39
+ },
40
+ {
41
+ case_no: 2,
42
+ case_title: 'age case',
43
+ klass: AttrEnumerablePersons.new(
44
+ [
45
+ AttrEnumerablePerson.new('tanaka', 84),
46
+ AttrEnumerablePerson.new('suzuki', 103)
47
+ ]
48
+ ),
49
+ method: :at_age,
50
+ args: 1,
51
+ expected: 103
52
+ },
53
+ {
54
+ case_no: 3,
55
+ case_title: 'not exists case',
56
+ klass: AttrEnumerablePersons.new(
57
+ [
58
+ AttrEnumerablePerson.new('tanaka', 84),
59
+ AttrEnumerablePerson.new('suzuki', 103)
60
+ ]
61
+ ),
62
+ method: :at_name,
63
+ args: 2,
64
+ expected: nil
65
+ },
66
+ {
67
+ case_no: 4,
68
+ case_title: 'empty case',
69
+ klass: AttrEnumerablePersons.new([]),
70
+ method: :at_name,
71
+ args: 0,
72
+ expected: nil
73
+ }
74
+ ]
75
+
76
+ cases.each do |c|
77
+ it "|case_no=#{c[:case_no]}|case_title=#{c[:case_title]}" do
78
+ begin
79
+ case_before c
80
+
81
+ # -- given --
82
+ # nothing
83
+
84
+ # -- when --
85
+ actual = c[:klass].send c[:method], c[:args]
86
+
87
+ # -- then --
88
+ expect(actual).to eq(c[:expected])
89
+ ensure
90
+ case_after c
91
+ end
92
+ end
93
+
94
+ def case_before(c)
95
+ # implement each case before
96
+ end
97
+
98
+ def case_after(c)
99
+ # implement each case after
100
+ end
101
+ end
102
+ end
103
+ 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.137
4
+ version: 0.0.138
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-25 00:00:00.000000000 Z
11
+ date: 2014-05-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -97,6 +97,7 @@ files:
97
97
  - README.md
98
98
  - Rakefile
99
99
  - lib/attr_enumerable.rb
100
+ - lib/attr_enumerable/at_attr.rb
100
101
  - lib/attr_enumerable/attr_enumerable_helper.rb
101
102
  - lib/attr_enumerable/each_attr.rb
102
103
  - lib/attr_enumerable/each_attr_with_index.rb
@@ -257,6 +258,7 @@ files:
257
258
  - lib/test_toolbox.rb
258
259
  - lib/test_toolbox/kernel.rb
259
260
  - rubocop-todo.yml
261
+ - spec/attr_enumerable/at_attr_spec.rb
260
262
  - spec/attr_enumerable/each_attr_spec.rb
261
263
  - spec/attr_enumerable/each_attr_with_index_spec.rb
262
264
  - spec/attr_enumerable/reverse_attr_spec.rb
@@ -423,6 +425,7 @@ signing_key:
423
425
  specification_version: 4
424
426
  summary: Utilities
425
427
  test_files:
428
+ - spec/attr_enumerable/at_attr_spec.rb
426
429
  - spec/attr_enumerable/each_attr_spec.rb
427
430
  - spec/attr_enumerable/each_attr_with_index_spec.rb
428
431
  - spec/attr_enumerable/reverse_attr_spec.rb