tbpgr_utils 0.0.145 → 0.0.146
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +32 -0
- data/lib/attr_enumerable/attr_enumerable_helper.rb +2 -1
- data/lib/attr_enumerable/map_attr.rb +14 -15
- data/lib/attr_enumerable/reduce_attr.rb +15 -0
- data/lib/tbpgr_utils/version.rb +1 -1
- data/spec/attr_enumerable/reduce_attr_spec.rb +93 -0
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 685cf30cf78c7d89c085051844c66933962c9337
|
4
|
+
data.tar.gz: 74615983b07af1fbf62181e22524ffec5d4eeb40
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 01c345c0389c294a237dcdbaaf5ea787c88eb60fc921b3076929936a8b7dcfd9335e434cf1b31ae43423aca27b1de6db5e72702e160d175c816fe86fb576cdcf
|
7
|
+
data.tar.gz: c6ee4cf4d0dfd2b46a24bcb2a8702dd582267c63d1fec82b39c521f674dc846bcd6c68f3a4fd37d8df25c8076e22d59f5df863a6f9bfa9365f3b2f04b8b3ae58
|
data/README.md
CHANGED
@@ -71,6 +71,7 @@ Or install it yourself as:
|
|
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
73
|
|[AttrEnumerable.map_attr](#attrenumerablemap_attr) |define map_xxx. it returns Class attributes(collection)'s Array map each value |
|
74
|
+
|[AttrEnumerable.reduce_attr](#attrenumerablereduce_attr) |define reduce_xxx. it returns Class attributes(collection)'s Array reduce each value |
|
74
75
|
|[AttributesHashable.to_hash](#attributeshashableto_hash) |define to_hash method for get instance_values |
|
75
76
|
|[AttributesInitializable::ClassMethods.attr_accessor_init](#attributesinitializableclassmethodsattr_accessor_init) |generate attr_accessor + initializer |
|
76
77
|
|[AttributesInitializable::ClassMethods.attr_reader_init](#attributesinitializableclassmethodsattr_reader_init) |generate attr_reader + initializer |
|
@@ -1469,6 +1470,36 @@ print persons.map_age { |v|v += 1 } # => [85, 104]
|
|
1469
1470
|
|
1470
1471
|
[back to list](#list)
|
1471
1472
|
|
1473
|
+
### AttrEnumerable.reduce_attr
|
1474
|
+
~~~ruby
|
1475
|
+
require 'attr_enumerable'
|
1476
|
+
|
1477
|
+
class Person
|
1478
|
+
attr_reader :name, :age
|
1479
|
+
def initialize(name, age)
|
1480
|
+
@name, @age = name, age
|
1481
|
+
end
|
1482
|
+
end
|
1483
|
+
|
1484
|
+
class Persons
|
1485
|
+
attr_reader :persons
|
1486
|
+
include AttrEnumerable
|
1487
|
+
def initialize(persons = [])
|
1488
|
+
@persons = persons
|
1489
|
+
end
|
1490
|
+
|
1491
|
+
def <<(person)
|
1492
|
+
@persons << person
|
1493
|
+
end
|
1494
|
+
end
|
1495
|
+
|
1496
|
+
persons = Persons.new([Person.new("tanaka", 84), Person.new("suzuki", 103)])
|
1497
|
+
print persons.reduce_name('') { |a, e|a = "#{a}#{e.upcase}"; a } # => 'TANAKASUZUKI'
|
1498
|
+
print persons.reduce_age { |a, e|a += e + 1; a } # => 189
|
1499
|
+
~~~
|
1500
|
+
|
1501
|
+
[back to list](#list)
|
1502
|
+
|
1472
1503
|
### AttributesHashable.to_hash
|
1473
1504
|
~~~ruby
|
1474
1505
|
require 'attributes_initializable'
|
@@ -4171,6 +4202,7 @@ if you are Sublime Text2 user, you can use snippet for TbpgrUtils.
|
|
4171
4202
|
https://github.com/tbpgr/tbpgr_utils_snippets
|
4172
4203
|
|
4173
4204
|
## History
|
4205
|
+
* version 0.0.146 : add AttrEnumerable.reduce_attr
|
4174
4206
|
* version 0.0.145 : add AttrEnumerable.map_attr
|
4175
4207
|
* version 0.0.144 : add AttrEnumerable.last_attr
|
4176
4208
|
* version 0.0.143 : add AttrEnumerable.include_attr?
|
@@ -14,7 +14,8 @@ module AttrEnumerable
|
|
14
14
|
{ regexp: /^first_(.*)$/, call_method: :first_attr },
|
15
15
|
{ regexp: /^include_(.*)\?$/, call_method: :include_attribute? },
|
16
16
|
{ regexp: /^last_(.*)$/, call_method: :last_attr },
|
17
|
-
{ regexp: /^map_(.*)$/, call_method: :map_attr }
|
17
|
+
{ regexp: /^map_(.*)$/, call_method: :map_attr },
|
18
|
+
{ regexp: /^reduce_(.*)$/, call_method: :reduce_attr }
|
18
19
|
]
|
19
20
|
|
20
21
|
# call attr enumerable method.
|
@@ -1,15 +1,14 @@
|
|
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
|
-
|
11
|
-
|
12
|
-
attrs
|
13
|
-
|
14
|
-
|
15
|
-
end
|
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
|
+
fail ArgumentError, "invalid attribute #{attribute}" unless include_attr?(col.first, attribute)
|
11
|
+
attrs = col.map { |v|v.send(attribute) }
|
12
|
+
attrs.reduce([]) { |r, v|r << yield(v); r }
|
13
|
+
end
|
14
|
+
end
|
@@ -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 reduce_attr(attribute, method_name, *args, &block)
|
8
|
+
col = collection
|
9
|
+
init_value = args.size == 0 ? 0 : args.first
|
10
|
+
return init_value if col.empty?
|
11
|
+
fail ArgumentError, "invalid attribute #{attribute}" unless include_attr?(col.first, attribute)
|
12
|
+
attrs = col.map { |e|e.send(attribute) }
|
13
|
+
attrs.reduce(init_value) { |a, e|a = yield(a, e); a }
|
14
|
+
end
|
15
|
+
end
|
data/lib/tbpgr_utils/version.rb
CHANGED
@@ -0,0 +1,93 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require 'spec_helper'
|
3
|
+
require 'attr_enumerable'
|
4
|
+
|
5
|
+
describe AttrEnumerable do
|
6
|
+
context :reduce_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: :reduce_name,
|
38
|
+
block: lambda { |r, x|r ||= ''; r = "#{r}#{x.upcase}"; r },
|
39
|
+
expected: '0TANAKATANAKASUZUKI',
|
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: :reduce_age,
|
52
|
+
block: lambda { |r, x|r ||= 0; r += x + 1; r },
|
53
|
+
expected: 127,
|
54
|
+
},
|
55
|
+
{
|
56
|
+
case_no: 3,
|
57
|
+
case_title: 'empty case',
|
58
|
+
klass: AttrEnumerablePersons.new([]),
|
59
|
+
method: :reduce_name,
|
60
|
+
block: lambda { |x|x += 1 },
|
61
|
+
expected: 0,
|
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], 0) { |a, e|a = c[:block].call(a, e); a }
|
76
|
+
|
77
|
+
# -- then --
|
78
|
+
expect(actual).to eq(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.
|
4
|
+
version: 0.0.146
|
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-
|
11
|
+
date: 2014-06-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -108,6 +108,7 @@ files:
|
|
108
108
|
- lib/attr_enumerable/include_attr.rb
|
109
109
|
- lib/attr_enumerable/last_attr.rb
|
110
110
|
- lib/attr_enumerable/map_attr.rb
|
111
|
+
- lib/attr_enumerable/reduce_attr.rb
|
111
112
|
- lib/attr_enumerable/reverse_attr.rb
|
112
113
|
- lib/attributes_hashable.rb
|
113
114
|
- lib/attributes_initializable.rb
|
@@ -275,6 +276,7 @@ files:
|
|
275
276
|
- spec/attr_enumerable/include_attr_spec.rb
|
276
277
|
- spec/attr_enumerable/last_attr_spec.rb
|
277
278
|
- spec/attr_enumerable/map_attr_spec.rb
|
279
|
+
- spec/attr_enumerable/reduce_attr_spec.rb
|
278
280
|
- spec/attr_enumerable/reverse_attr_spec.rb
|
279
281
|
- spec/attributes_hashable_spec.rb
|
280
282
|
- spec/attributes_initializable_spec.rb
|
@@ -449,6 +451,7 @@ test_files:
|
|
449
451
|
- spec/attr_enumerable/include_attr_spec.rb
|
450
452
|
- spec/attr_enumerable/last_attr_spec.rb
|
451
453
|
- spec/attr_enumerable/map_attr_spec.rb
|
454
|
+
- spec/attr_enumerable/reduce_attr_spec.rb
|
452
455
|
- spec/attr_enumerable/reverse_attr_spec.rb
|
453
456
|
- spec/attributes_hashable_spec.rb
|
454
457
|
- spec/attributes_initializable_spec.rb
|