tbpgr_utils 0.0.146 → 0.0.147
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 +33 -0
- data/lib/attr_enumerable/attr_enumerable_helper.rb +2 -1
- data/lib/attr_enumerable/sample_attr.rb +14 -0
- data/lib/tbpgr_utils/version.rb +1 -1
- data/spec/attr_enumerable/sample_attr_spec.rb +94 -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: 31237b7bc9c295d4734a79d9bee3081d0a96bc3d
|
4
|
+
data.tar.gz: a441751320658a5aaa092ef6abf3602a14aaf33b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0007b325faad1121c9fd0911c6b1d788b4aa9bfb08ab90786481fa4ddf442ea6c64ad5e09b895310e0275fa4363ca26039aa7f14d4649f182180503eda4039de
|
7
|
+
data.tar.gz: f9c9a7f3d746aa007f65a6f56bbe674f9d278d26fc5f5227c8df4730cd408e62f02b032994f0f748b56d861e184b006efdcf3ba567fb732014e266eb4ecb32d7
|
data/README.md
CHANGED
@@ -72,6 +72,7 @@ Or install it yourself as:
|
|
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
74
|
|[AttrEnumerable.reduce_attr](#attrenumerablereduce_attr) |define reduce_xxx. it returns Class attributes(collection)'s Array reduce each value |
|
75
|
+
|[AttrEnumerable.sample_attr](#attrenumerablesample_attr) |define sample_xxx. it returns Class attributes(collection)'s Array sample value |
|
75
76
|
|[AttributesHashable.to_hash](#attributeshashableto_hash) |define to_hash method for get instance_values |
|
76
77
|
|[AttributesInitializable::ClassMethods.attr_accessor_init](#attributesinitializableclassmethodsattr_accessor_init) |generate attr_accessor + initializer |
|
77
78
|
|[AttributesInitializable::ClassMethods.attr_reader_init](#attributesinitializableclassmethodsattr_reader_init) |generate attr_reader + initializer |
|
@@ -1500,6 +1501,37 @@ print persons.reduce_age { |a, e|a += e + 1; a } # => 189
|
|
1500
1501
|
|
1501
1502
|
[back to list](#list)
|
1502
1503
|
|
1504
|
+
### AttrEnumerable.sample_attr
|
1505
|
+
~~~ruby
|
1506
|
+
require 'attr_enumerable'
|
1507
|
+
|
1508
|
+
class Person
|
1509
|
+
attr_reader :name, :age
|
1510
|
+
def initialize(name, age)
|
1511
|
+
@name, @age = name, age
|
1512
|
+
end
|
1513
|
+
end
|
1514
|
+
|
1515
|
+
class Persons
|
1516
|
+
attr_reader :persons
|
1517
|
+
include AttrEnumerable
|
1518
|
+
def initialize(persons = [])
|
1519
|
+
@persons = persons
|
1520
|
+
end
|
1521
|
+
|
1522
|
+
def <<(person)
|
1523
|
+
@persons << person
|
1524
|
+
end
|
1525
|
+
end
|
1526
|
+
|
1527
|
+
persons = Persons.new([Person.new("tanaka", 84), Person.new("suzuki", 103)])
|
1528
|
+
print persons.sample_name # => 'tanaka' or 'suzuki'
|
1529
|
+
print persons.sample_name(2) # => ['tanaka', 'suzuki'] or ['suzuki', 'tanaka']
|
1530
|
+
print persons.sample_age(2) # => [84, 103] or [103, 84]
|
1531
|
+
~~~
|
1532
|
+
|
1533
|
+
[back to list](#list)
|
1534
|
+
|
1503
1535
|
### AttributesHashable.to_hash
|
1504
1536
|
~~~ruby
|
1505
1537
|
require 'attributes_initializable'
|
@@ -4202,6 +4234,7 @@ if you are Sublime Text2 user, you can use snippet for TbpgrUtils.
|
|
4202
4234
|
https://github.com/tbpgr/tbpgr_utils_snippets
|
4203
4235
|
|
4204
4236
|
## History
|
4237
|
+
* version 0.0.147 : add AttrEnumerable.sample_attr
|
4205
4238
|
* version 0.0.146 : add AttrEnumerable.reduce_attr
|
4206
4239
|
* version 0.0.145 : add AttrEnumerable.map_attr
|
4207
4240
|
* version 0.0.144 : add AttrEnumerable.last_attr
|
@@ -15,7 +15,8 @@ module AttrEnumerable
|
|
15
15
|
{ regexp: /^include_(.*)\?$/, call_method: :include_attribute? },
|
16
16
|
{ regexp: /^last_(.*)$/, call_method: :last_attr },
|
17
17
|
{ regexp: /^map_(.*)$/, call_method: :map_attr },
|
18
|
-
{ regexp: /^reduce_(.*)$/, call_method: :reduce_attr }
|
18
|
+
{ regexp: /^reduce_(.*)$/, call_method: :reduce_attr },
|
19
|
+
{ regexp: /^sample_(.*)$/, call_method: :sample_attr }
|
19
20
|
]
|
20
21
|
|
21
22
|
# 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 sample_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.sample(count)
|
13
|
+
end
|
14
|
+
end
|
data/lib/tbpgr_utils/version.rb
CHANGED
@@ -0,0 +1,94 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require 'spec_helper'
|
3
|
+
require 'attr_enumerable'
|
4
|
+
|
5
|
+
describe AttrEnumerable do
|
6
|
+
context :sample_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: :sample_name,
|
38
|
+
count: 2,
|
39
|
+
expected_array: ['tanaka', 'suzuki'],
|
40
|
+
expected_size: 2,
|
41
|
+
},
|
42
|
+
{
|
43
|
+
case_no: 2,
|
44
|
+
case_title: 'age case',
|
45
|
+
klass: AttrEnumerablePersons.new(
|
46
|
+
[
|
47
|
+
AttrEnumerablePerson.new('tanaka', 84),
|
48
|
+
AttrEnumerablePerson.new('tanaka', 20),
|
49
|
+
AttrEnumerablePerson.new('suzuki', 20)
|
50
|
+
]
|
51
|
+
),
|
52
|
+
method: :sample_age,
|
53
|
+
count: nil,
|
54
|
+
expected_array: [84, 20],
|
55
|
+
expected_size: 1,
|
56
|
+
},
|
57
|
+
]
|
58
|
+
|
59
|
+
cases.each do |c|
|
60
|
+
it "|case_no=#{c[:case_no]}|case_title=#{c[:case_title]}" do
|
61
|
+
begin
|
62
|
+
case_before c
|
63
|
+
|
64
|
+
# -- given --
|
65
|
+
# nothing
|
66
|
+
ary = c[:klass]
|
67
|
+
|
68
|
+
# -- when --
|
69
|
+
actual =
|
70
|
+
if c[:count]
|
71
|
+
ary.send(c[:method], c[:count])
|
72
|
+
else
|
73
|
+
ary.send(c[:method])
|
74
|
+
end
|
75
|
+
actual = Array(actual)
|
76
|
+
|
77
|
+
# -- then --
|
78
|
+
expect(actual.size).to eq(c[:expected_size])
|
79
|
+
actual.each { |e| expect(c[:expected_array]).to include(e) }
|
80
|
+
ensure
|
81
|
+
case_after c
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
def case_before(c)
|
86
|
+
# implement each case before
|
87
|
+
end
|
88
|
+
|
89
|
+
def case_after(c)
|
90
|
+
# implement each case after
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
94
|
+
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.147
|
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-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -110,6 +110,7 @@ files:
|
|
110
110
|
- lib/attr_enumerable/map_attr.rb
|
111
111
|
- lib/attr_enumerable/reduce_attr.rb
|
112
112
|
- lib/attr_enumerable/reverse_attr.rb
|
113
|
+
- lib/attr_enumerable/sample_attr.rb
|
113
114
|
- lib/attributes_hashable.rb
|
114
115
|
- lib/attributes_initializable.rb
|
115
116
|
- lib/end_erb.rb
|
@@ -278,6 +279,7 @@ files:
|
|
278
279
|
- spec/attr_enumerable/map_attr_spec.rb
|
279
280
|
- spec/attr_enumerable/reduce_attr_spec.rb
|
280
281
|
- spec/attr_enumerable/reverse_attr_spec.rb
|
282
|
+
- spec/attr_enumerable/sample_attr_spec.rb
|
281
283
|
- spec/attributes_hashable_spec.rb
|
282
284
|
- spec/attributes_initializable_spec.rb
|
283
285
|
- spec/eval_helper/attr_accessor_init_code_spec.rb
|
@@ -453,6 +455,7 @@ test_files:
|
|
453
455
|
- spec/attr_enumerable/map_attr_spec.rb
|
454
456
|
- spec/attr_enumerable/reduce_attr_spec.rb
|
455
457
|
- spec/attr_enumerable/reverse_attr_spec.rb
|
458
|
+
- spec/attr_enumerable/sample_attr_spec.rb
|
456
459
|
- spec/attributes_hashable_spec.rb
|
457
460
|
- spec/attributes_initializable_spec.rb
|
458
461
|
- spec/eval_helper/attr_accessor_init_code_spec.rb
|