tbpgr_utils 0.0.144 → 0.0.145

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: f68df29b7d9250f632215eed0f6828625de3e6b0
4
- data.tar.gz: 82ef8056f7390e8bc68af39b6d8ea7e417da41ed
3
+ metadata.gz: 01e73372a4d221e097a77fc4d92791b6cc30acd8
4
+ data.tar.gz: e7e16a2c4a9eeca587a0670754500f70eef7a63a
5
5
  SHA512:
6
- metadata.gz: 5a24a7b96922ded4523b04dd4da4ab6b1c598ef3ef27c7f324bfccfd00e970a8ddb948c9a67381743b527c4f513b7219deab0550fb0a3dcb88cc9fcb117f52f3
7
- data.tar.gz: 2a994844d07cef1651bdc984f33b48a78302e3e0d7505683e0ee385dc7ade9b64b059a81653ebeb042ab34588428934167d47ff66fed9a69bf2f2005f1a31556
6
+ metadata.gz: 98cd83a93c3cff1d5a7987b98de0341668e931ab35b902819c964fd466b41d4d1cd66f4fed9204696e32876542bbd151eadec3e48f2693ca14f790e7c5ac05cf
7
+ data.tar.gz: c2ee42b6e795f343b183b3725d896f1ef1cf070d4fa6971fb0bf2f0ab91b61eaf2746ec1b06ea792c0a25aafc3d5718ee0dc1744de9a1bd2db74b5397d8325a9
data/README.md CHANGED
@@ -70,6 +70,7 @@ Or install it yourself as:
70
70
  |[AttrEnumerable.include_attr?](#attrenumerableinclude_attr) |define include_xxx?. it returns Class attributes(collection)'s attribute include value |
71
71
  |[AttrEnumerable.last_attr](#attrenumerablelast_attr) |define last_xxx. it returns Class attributes(collection) last N elementt |
72
72
  |[AttrEnumerable.reverse_attr](#attrenumerablereverse_attr) |define reverse_xxx. it returns Class attributes(collection)'s reverse Array |
73
+ |[AttrEnumerable.map_attr](#attrenumerablemap_attr) |define map_xxx. it returns Class attributes(collection)'s Array map each value |
73
74
  |[AttributesHashable.to_hash](#attributeshashableto_hash) |define to_hash method for get instance_values |
74
75
  |[AttributesInitializable::ClassMethods.attr_accessor_init](#attributesinitializableclassmethodsattr_accessor_init) |generate attr_accessor + initializer |
75
76
  |[AttributesInitializable::ClassMethods.attr_reader_init](#attributesinitializableclassmethodsattr_reader_init) |generate attr_reader + initializer |
@@ -1438,6 +1439,36 @@ print persons.reverse_age # => [103, 84]
1438
1439
 
1439
1440
  [back to list](#list)
1440
1441
 
1442
+ ### AttrEnumerable.map_attr
1443
+ ~~~ruby
1444
+ require 'attr_enumerable'
1445
+
1446
+ class Person
1447
+ attr_reader :name, :age
1448
+ def initialize(name, age)
1449
+ @name, @age = name, age
1450
+ end
1451
+ end
1452
+
1453
+ class Persons
1454
+ attr_reader :persons
1455
+ include AttrEnumerable
1456
+ def initialize(persons = [])
1457
+ @persons = persons
1458
+ end
1459
+
1460
+ def <<(person)
1461
+ @persons << person
1462
+ end
1463
+ end
1464
+
1465
+ persons = Persons.new([Person.new("tanaka", 84), Person.new("suzuki", 103)])
1466
+ print persons.map_name { |v|v.upcase } # => ['TANAKA', 'SUZUKI']
1467
+ print persons.map_age { |v|v += 1 } # => [85, 104]
1468
+ ~~~
1469
+
1470
+ [back to list](#list)
1471
+
1441
1472
  ### AttributesHashable.to_hash
1442
1473
  ~~~ruby
1443
1474
  require 'attributes_initializable'
@@ -4140,6 +4171,7 @@ if you are Sublime Text2 user, you can use snippet for TbpgrUtils.
4140
4171
  https://github.com/tbpgr/tbpgr_utils_snippets
4141
4172
 
4142
4173
  ## History
4174
+ * version 0.0.145 : add AttrEnumerable.map_attr
4143
4175
  * version 0.0.144 : add AttrEnumerable.last_attr
4144
4176
  * version 0.0.143 : add AttrEnumerable.include_attr?
4145
4177
  * version 0.0.142 : add AttrEnumerable.first_attr
@@ -13,7 +13,8 @@ module AttrEnumerable
13
13
  { regexp: /^delete_(.*)$/, call_method: :delete_attr },
14
14
  { regexp: /^first_(.*)$/, call_method: :first_attr },
15
15
  { regexp: /^include_(.*)\?$/, call_method: :include_attribute? },
16
- { regexp: /^last_(.*)$/, call_method: :last_attr }
16
+ { regexp: /^last_(.*)$/, call_method: :last_attr },
17
+ { regexp: /^map_(.*)$/, call_method: :map_attr }
17
18
  ]
18
19
 
19
20
  # call attr enumerable method.
@@ -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 map_attr(attribute, method_name, *args, &block)
8
+ col = collection
9
+ return [] if col.empty?
10
+ first_size = args.size == 0 ? nil : Integer(args.first)
11
+ fail ArgumentError, "invalid attribute #{attribute}" unless include_attr?(col.first, attribute)
12
+ attrs = col.map { |v|v.send(attribute) }
13
+ attrs.reduce([]) { |r, v|r << yield(v); r }
14
+ end
15
+ end
@@ -2,5 +2,5 @@
2
2
 
3
3
  # Tbpgr Utilities
4
4
  module TbpgrUtils
5
- VERSION = '0.0.144'
5
+ VERSION = '0.0.145'
6
6
  end
@@ -0,0 +1,93 @@
1
+ # encoding: utf-8
2
+ require 'spec_helper'
3
+ require 'attr_enumerable'
4
+
5
+ describe AttrEnumerable do
6
+ context :map_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('tanaka', 20),
34
+ AttrEnumerablePerson.new('suzuki', 20)
35
+ ]
36
+ ),
37
+ method: :map_name,
38
+ block: lambda { |x|x.upcase },
39
+ expected: ['TANAKA' , 'TANAKA', 'SUZUKI'],
40
+ },
41
+ {
42
+ case_no: 2,
43
+ case_title: 'age case',
44
+ klass: AttrEnumerablePersons.new(
45
+ [
46
+ AttrEnumerablePerson.new('tanaka', 84),
47
+ AttrEnumerablePerson.new('tanaka', 20),
48
+ AttrEnumerablePerson.new('suzuki', 20)
49
+ ]
50
+ ),
51
+ method: :map_age,
52
+ block: lambda { |x|x += 1 },
53
+ expected: [85, 21, 21],
54
+ },
55
+ {
56
+ case_no: 3,
57
+ case_title: 'empty case',
58
+ klass: AttrEnumerablePersons.new([]),
59
+ method: :map_name,
60
+ block: lambda { |x|x += 1 },
61
+ expected: [],
62
+ }
63
+ ]
64
+
65
+ cases.each do |c|
66
+ it "|case_no=#{c[:case_no]}|case_title=#{c[:case_title]}" do
67
+ begin
68
+ case_before c
69
+
70
+ # -- given --
71
+ # nothing
72
+ ary = c[:klass]
73
+
74
+ # -- when --
75
+ actual = ary.send(c[:method]) { |x|c[:block].call(x) }
76
+
77
+ # -- then --
78
+ expect(Array(actual)).to match_array(c[:expected])
79
+ ensure
80
+ case_after c
81
+ end
82
+ end
83
+
84
+ def case_before(c)
85
+ # implement each case before
86
+ end
87
+
88
+ def case_after(c)
89
+ # implement each case after
90
+ end
91
+ end
92
+ end
93
+ 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.144
4
+ version: 0.0.145
5
5
  platform: ruby
6
6
  authors:
7
7
  - tbpgr
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-06-01 00:00:00.000000000 Z
11
+ date: 2014-06-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -107,6 +107,7 @@ files:
107
107
  - lib/attr_enumerable/first_attr.rb
108
108
  - lib/attr_enumerable/include_attr.rb
109
109
  - lib/attr_enumerable/last_attr.rb
110
+ - lib/attr_enumerable/map_attr.rb
110
111
  - lib/attr_enumerable/reverse_attr.rb
111
112
  - lib/attributes_hashable.rb
112
113
  - lib/attributes_initializable.rb
@@ -273,6 +274,7 @@ files:
273
274
  - spec/attr_enumerable/first_attr_spec.rb
274
275
  - spec/attr_enumerable/include_attr_spec.rb
275
276
  - spec/attr_enumerable/last_attr_spec.rb
277
+ - spec/attr_enumerable/map_attr_spec.rb
276
278
  - spec/attr_enumerable/reverse_attr_spec.rb
277
279
  - spec/attributes_hashable_spec.rb
278
280
  - spec/attributes_initializable_spec.rb
@@ -446,6 +448,7 @@ test_files:
446
448
  - spec/attr_enumerable/first_attr_spec.rb
447
449
  - spec/attr_enumerable/include_attr_spec.rb
448
450
  - spec/attr_enumerable/last_attr_spec.rb
451
+ - spec/attr_enumerable/map_attr_spec.rb
449
452
  - spec/attr_enumerable/reverse_attr_spec.rb
450
453
  - spec/attributes_hashable_spec.rb
451
454
  - spec/attributes_initializable_spec.rb