tbpgr_utils 0.0.147 → 0.0.148
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/select_attr.rb +14 -0
- data/lib/tbpgr_utils/version.rb +1 -1
- data/spec/attr_enumerable/select_attr_spec.rb +85 -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: 775c44d3b88c5e2de18b5e4df26145c929a199f8
|
4
|
+
data.tar.gz: b466cd6b263de3bb235dec124247002d020f31a0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e112e33ff471fc67b9c504e515095386fbe152b2f06dc000360d23debed1dced0d418bc519e0eaa1ffe3362792ec8f9a36aa4c214503bde00286a989f1f79ba4
|
7
|
+
data.tar.gz: ece124fdc9760a006623e7680f69ffad65f6f6b9f8202011ac7557e03e52b1d7e31942a0becfa2166a40eb3d52bfd947094886497b247a72509ea79b970aec2f
|
data/README.md
CHANGED
@@ -73,6 +73,7 @@ Or install it yourself as:
|
|
73
73
|
|[AttrEnumerable.map_attr](#attrenumerablemap_attr) |define map_xxx. it returns Class attributes(collection)'s Array map each value |
|
74
74
|
|[AttrEnumerable.reduce_attr](#attrenumerablereduce_attr) |define reduce_xxx. it returns Class attributes(collection)'s Array reduce each value |
|
75
75
|
|[AttrEnumerable.sample_attr](#attrenumerablesample_attr) |define sample_xxx. it returns Class attributes(collection)'s Array sample value |
|
76
|
+
|[AttrEnumerable.select_attr](#attrenumerableselect_attr) |define select_xxx. it returns Class attributes(collection)'s Array select value |
|
76
77
|
|[AttributesHashable.to_hash](#attributeshashableto_hash) |define to_hash method for get instance_values |
|
77
78
|
|[AttributesInitializable::ClassMethods.attr_accessor_init](#attributesinitializableclassmethodsattr_accessor_init) |generate attr_accessor + initializer |
|
78
79
|
|[AttributesInitializable::ClassMethods.attr_reader_init](#attributesinitializableclassmethodsattr_reader_init) |generate attr_reader + initializer |
|
@@ -1532,6 +1533,36 @@ print persons.sample_age(2) # => [84, 103] or [103, 84]
|
|
1532
1533
|
|
1533
1534
|
[back to list](#list)
|
1534
1535
|
|
1536
|
+
### AttrEnumerable.select_attr
|
1537
|
+
~~~ruby
|
1538
|
+
require 'attr_enumerable'
|
1539
|
+
|
1540
|
+
class Person
|
1541
|
+
attr_reader :name, :age
|
1542
|
+
def initialize(name, age)
|
1543
|
+
@name, @age = name, age
|
1544
|
+
end
|
1545
|
+
end
|
1546
|
+
|
1547
|
+
class Persons
|
1548
|
+
attr_reader :persons
|
1549
|
+
include AttrEnumerable
|
1550
|
+
def initialize(persons = [])
|
1551
|
+
@persons = persons
|
1552
|
+
end
|
1553
|
+
|
1554
|
+
def <<(person)
|
1555
|
+
@persons << person
|
1556
|
+
end
|
1557
|
+
end
|
1558
|
+
|
1559
|
+
persons = Persons.new([Person.new("tanaka", 84), Person.new("tanaka", 20), Person.new("suzuki", 20)])
|
1560
|
+
print persons.select_name { |v|v == 'tanaka' } # => ['tanaka' ,'tanaka']
|
1561
|
+
print persons.select_age { |v|v == 20 } # => [20 ,20]
|
1562
|
+
~~~
|
1563
|
+
|
1564
|
+
[back to list](#list)
|
1565
|
+
|
1535
1566
|
### AttributesHashable.to_hash
|
1536
1567
|
~~~ruby
|
1537
1568
|
require 'attributes_initializable'
|
@@ -4234,6 +4265,7 @@ if you are Sublime Text2 user, you can use snippet for TbpgrUtils.
|
|
4234
4265
|
https://github.com/tbpgr/tbpgr_utils_snippets
|
4235
4266
|
|
4236
4267
|
## History
|
4268
|
+
* version 0.0.148 : add AttrEnumerable.select_attr
|
4237
4269
|
* version 0.0.147 : add AttrEnumerable.sample_attr
|
4238
4270
|
* version 0.0.146 : add AttrEnumerable.reduce_attr
|
4239
4271
|
* version 0.0.145 : add AttrEnumerable.map_attr
|
@@ -16,7 +16,8 @@ module AttrEnumerable
|
|
16
16
|
{ regexp: /^last_(.*)$/, call_method: :last_attr },
|
17
17
|
{ regexp: /^map_(.*)$/, call_method: :map_attr },
|
18
18
|
{ regexp: /^reduce_(.*)$/, call_method: :reduce_attr },
|
19
|
-
{ regexp: /^sample_(.*)$/, call_method: :sample_attr }
|
19
|
+
{ regexp: /^sample_(.*)$/, call_method: :sample_attr },
|
20
|
+
{ regexp: /^select_(.*)$/, call_method: :select_attr }
|
20
21
|
]
|
21
22
|
|
22
23
|
# call attr enumerable method.
|
@@ -0,0 +1,14 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require 'attr_enumerable/attr_enumerable_helper'
|
3
|
+
|
4
|
+
# AttrEnumerable
|
5
|
+
module AttrEnumerable
|
6
|
+
private
|
7
|
+
def select_attr(attribute, method_name, *args, &block)
|
8
|
+
col = collection
|
9
|
+
# count = args.size == 0 ? 1 : Integer(args.first)
|
10
|
+
fail ArgumentError, "invalid attribute #{attribute}" unless include_attr?(col.first, attribute)
|
11
|
+
attrs = col.map { |e|e.send(attribute) }
|
12
|
+
attrs.select { |e| yield(e) }
|
13
|
+
end
|
14
|
+
end
|
data/lib/tbpgr_utils/version.rb
CHANGED
@@ -0,0 +1,85 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require 'spec_helper'
|
3
|
+
require 'attr_enumerable'
|
4
|
+
|
5
|
+
describe AttrEnumerable do
|
6
|
+
context :select_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: :select_name,
|
38
|
+
block: proc { |v|v == 'tanaka' },
|
39
|
+
expected: ['tanaka', 'tanaka'],
|
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: :select_age,
|
52
|
+
block: proc { |v|v == 20 },
|
53
|
+
expected: [20, 20],
|
54
|
+
}
|
55
|
+
]
|
56
|
+
|
57
|
+
cases.each do |c|
|
58
|
+
it "|case_no=#{c[:case_no]}|case_title=#{c[:case_title]}" do
|
59
|
+
begin
|
60
|
+
case_before c
|
61
|
+
|
62
|
+
# -- given --
|
63
|
+
# nothing
|
64
|
+
ary = c[:klass]
|
65
|
+
|
66
|
+
# -- when --
|
67
|
+
actual = ary.send(c[:method], &c[:block])
|
68
|
+
|
69
|
+
# -- then --
|
70
|
+
expect(actual).to match_array(c[:expected])
|
71
|
+
ensure
|
72
|
+
case_after c
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
def case_before(c)
|
77
|
+
# implement each case before
|
78
|
+
end
|
79
|
+
|
80
|
+
def case_after(c)
|
81
|
+
# implement each case after
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
85
|
+
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.148
|
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-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -111,6 +111,7 @@ files:
|
|
111
111
|
- lib/attr_enumerable/reduce_attr.rb
|
112
112
|
- lib/attr_enumerable/reverse_attr.rb
|
113
113
|
- lib/attr_enumerable/sample_attr.rb
|
114
|
+
- lib/attr_enumerable/select_attr.rb
|
114
115
|
- lib/attributes_hashable.rb
|
115
116
|
- lib/attributes_initializable.rb
|
116
117
|
- lib/end_erb.rb
|
@@ -280,6 +281,7 @@ files:
|
|
280
281
|
- spec/attr_enumerable/reduce_attr_spec.rb
|
281
282
|
- spec/attr_enumerable/reverse_attr_spec.rb
|
282
283
|
- spec/attr_enumerable/sample_attr_spec.rb
|
284
|
+
- spec/attr_enumerable/select_attr_spec.rb
|
283
285
|
- spec/attributes_hashable_spec.rb
|
284
286
|
- spec/attributes_initializable_spec.rb
|
285
287
|
- spec/eval_helper/attr_accessor_init_code_spec.rb
|
@@ -456,6 +458,7 @@ test_files:
|
|
456
458
|
- spec/attr_enumerable/reduce_attr_spec.rb
|
457
459
|
- spec/attr_enumerable/reverse_attr_spec.rb
|
458
460
|
- spec/attr_enumerable/sample_attr_spec.rb
|
461
|
+
- spec/attr_enumerable/select_attr_spec.rb
|
459
462
|
- spec/attributes_hashable_spec.rb
|
460
463
|
- spec/attributes_initializable_spec.rb
|
461
464
|
- spec/eval_helper/attr_accessor_init_code_spec.rb
|