tbpgr_utils 0.0.149 → 0.0.150
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +34 -0
- data/lib/attr_enumerable/attr_enumerable_helper.rb +2 -1
- data/lib/attr_enumerable/slice_attr.rb +21 -0
- data/lib/tbpgr_utils/version.rb +1 -1
- data/spec/attr_enumerable/slice_attr_spec.rb +108 -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: 3d165c8e6eae4e8aa717f9612ec4f7a4c13ea08a
|
4
|
+
data.tar.gz: a95c7052c530172f9a0b479f16458688e86164e5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 29056a1ab6cab8ae286796b2526c3ba65f0b4abf33940f3dca0dc2bbf1d0f141c8b172c0099308d72384a9c756a9d60488574f370bfaef3a0443477ba4d86551
|
7
|
+
data.tar.gz: 8858c022a42c2d39171121f843e7631d419d5f7eade87857c6dc8b4638e8ac3772b6bbfc5a7298ecf7896253fbd9e85f29b6fe11f2b6c09b70eb0e887173a3fe
|
data/README.md
CHANGED
@@ -75,6 +75,7 @@ Or install it yourself as:
|
|
75
75
|
|[AttrEnumerable.sample_attr](#attrenumerablesample_attr) |define sample_xxx. it returns Class attributes(collection)'s Array sample value |
|
76
76
|
|[AttrEnumerable.select_attr](#attrenumerableselect_attr) |define select_xxx. it returns Class attributes(collection)'s Array select value |
|
77
77
|
|[AttrEnumerable.shuffle_attr](#attrenumerableshuffle_attr) |define shuffle_xxx. it returns Class attributes(collection)'s Array shuffle value |
|
78
|
+
|[AttrEnumerable.slice_attr](#attrenumerableslice_attr) |define slice_xxx. it returns Class attributes(collection)'s Array slice value |
|
78
79
|
|[AttributesHashable.to_hash](#attributeshashableto_hash) |define to_hash method for get instance_values |
|
79
80
|
|[AttributesInitializable::ClassMethods.attr_accessor_init](#attributesinitializableclassmethodsattr_accessor_init) |generate attr_accessor + initializer |
|
80
81
|
|[AttributesInitializable::ClassMethods.attr_reader_init](#attributesinitializableclassmethodsattr_reader_init) |generate attr_reader + initializer |
|
@@ -1592,6 +1593,38 @@ print persons.shuffle_name # => ['tanaka', 'suzuki'] or['suzuki', 'tanaka']
|
|
1592
1593
|
print persons.shuffle_age # => [84, 103] or [103, 84]
|
1593
1594
|
~~~
|
1594
1595
|
|
1596
|
+
|
1597
|
+
[back to list](#list)
|
1598
|
+
|
1599
|
+
### AttrEnumerable.slice_attr
|
1600
|
+
~~~ruby
|
1601
|
+
require 'attr_enumerable'
|
1602
|
+
|
1603
|
+
class Person
|
1604
|
+
attr_reader :name, :age
|
1605
|
+
def initialize(name, age)
|
1606
|
+
@name, @age = name, age
|
1607
|
+
end
|
1608
|
+
end
|
1609
|
+
|
1610
|
+
class Persons
|
1611
|
+
attr_reader :persons
|
1612
|
+
include AttrEnumerable
|
1613
|
+
def initialize(persons = [])
|
1614
|
+
@persons = persons
|
1615
|
+
end
|
1616
|
+
|
1617
|
+
def <<(person)
|
1618
|
+
@persons << person
|
1619
|
+
end
|
1620
|
+
end
|
1621
|
+
|
1622
|
+
persons = Persons.new([Person.new("tanaka", 84), Person.new("suzuki", 33), Person.new("suzuki", 103)])
|
1623
|
+
print persons.slice_name(1) # => 'suzuki'
|
1624
|
+
print persons.slice_age(0, 2) # => [84, 33]
|
1625
|
+
print persons.slice_age(1..2) # => [33, 103]
|
1626
|
+
~~~
|
1627
|
+
|
1595
1628
|
[back to list](#list)
|
1596
1629
|
|
1597
1630
|
### AttributesHashable.to_hash
|
@@ -4296,6 +4329,7 @@ if you are Sublime Text2 user, you can use snippet for TbpgrUtils.
|
|
4296
4329
|
https://github.com/tbpgr/tbpgr_utils_snippets
|
4297
4330
|
|
4298
4331
|
## History
|
4332
|
+
* version 0.0.150 : add AttrEnumerable.slice_attr
|
4299
4333
|
* version 0.0.149 : add AttrEnumerable.shuffle_attr
|
4300
4334
|
* version 0.0.148 : add AttrEnumerable.select_attr
|
4301
4335
|
* version 0.0.147 : add AttrEnumerable.sample_attr
|
@@ -18,7 +18,8 @@ module AttrEnumerable
|
|
18
18
|
{ regexp: /^reduce_(.*)$/, call_method: :reduce_attr },
|
19
19
|
{ regexp: /^sample_(.*)$/, call_method: :sample_attr },
|
20
20
|
{ regexp: /^select_(.*)$/, call_method: :select_attr },
|
21
|
-
{ regexp: /^shuffle_(.*)$/, call_method: :shuffle_attr }
|
21
|
+
{ regexp: /^shuffle_(.*)$/, call_method: :shuffle_attr },
|
22
|
+
{ regexp: /^slice_(.*)$/, call_method: :slice_attr }
|
22
23
|
]
|
23
24
|
|
24
25
|
# call attr enumerable method.
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require 'attr_enumerable/attr_enumerable_helper'
|
3
|
+
|
4
|
+
# AttrEnumerable
|
5
|
+
module AttrEnumerable
|
6
|
+
private
|
7
|
+
def slice_attr(attribute, method_name, *args, &block)
|
8
|
+
check_argument_length(*args)
|
9
|
+
col = collection
|
10
|
+
index_or_range = args.first
|
11
|
+
length = args.size > 1 ? args[1] : nil
|
12
|
+
fail ArgumentError, "invalid attribute #{attribute}" unless include_attr?(col.first, attribute)
|
13
|
+
attrs = col.map { |e|e.send(attribute) }
|
14
|
+
length ? attrs.slice(index_or_range, length) : attrs.slice(index_or_range)
|
15
|
+
end
|
16
|
+
|
17
|
+
def check_argument_length(*args)
|
18
|
+
return if args.size > 0
|
19
|
+
fail ArgumentError, 'wrong number of arguments (0 for 1..2) (ArgumentError)'
|
20
|
+
end
|
21
|
+
end
|
data/lib/tbpgr_utils/version.rb
CHANGED
@@ -0,0 +1,108 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require 'spec_helper'
|
3
|
+
require 'attr_enumerable'
|
4
|
+
|
5
|
+
describe AttrEnumerable do
|
6
|
+
context :slice_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: :slice_name,
|
38
|
+
index_or_range: 2,
|
39
|
+
length: nil,
|
40
|
+
expected_array: ['suzuki']
|
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: :slice_age,
|
53
|
+
index_or_range: 0,
|
54
|
+
length: 2,
|
55
|
+
expected_array: [84, 20]
|
56
|
+
},
|
57
|
+
{
|
58
|
+
case_no: 3,
|
59
|
+
case_title: 'name case',
|
60
|
+
klass: AttrEnumerablePersons.new(
|
61
|
+
[
|
62
|
+
AttrEnumerablePerson.new('tanaka', 84),
|
63
|
+
AttrEnumerablePerson.new('tanaka', 20),
|
64
|
+
AttrEnumerablePerson.new('suzuki', 20)
|
65
|
+
]
|
66
|
+
),
|
67
|
+
method: :slice_name,
|
68
|
+
index_or_range: 1..2,
|
69
|
+
length: nil,
|
70
|
+
expected_array: ['tanaka', 'suzuki']
|
71
|
+
},
|
72
|
+
]
|
73
|
+
|
74
|
+
cases.each do |c|
|
75
|
+
it "|case_no=#{c[:case_no]}|case_title=#{c[:case_title]}" do
|
76
|
+
begin
|
77
|
+
case_before c
|
78
|
+
|
79
|
+
# -- given --
|
80
|
+
# nothing
|
81
|
+
ary = c[:klass]
|
82
|
+
|
83
|
+
# -- when --
|
84
|
+
actual =
|
85
|
+
if c[:length]
|
86
|
+
ary.send(c[:method], c[:index_or_range], c[:length])
|
87
|
+
else
|
88
|
+
ary.send(c[:method], c[:index_or_range])
|
89
|
+
end
|
90
|
+
actual = Array(actual)
|
91
|
+
|
92
|
+
# -- then --
|
93
|
+
expect(actual).to match_array(c[:expected_array])
|
94
|
+
ensure
|
95
|
+
case_after c
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
def case_before(c)
|
100
|
+
# implement each case before
|
101
|
+
end
|
102
|
+
|
103
|
+
def case_after(c)
|
104
|
+
# implement each case after
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
108
|
+
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.150
|
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-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -113,6 +113,7 @@ files:
|
|
113
113
|
- lib/attr_enumerable/sample_attr.rb
|
114
114
|
- lib/attr_enumerable/select_attr.rb
|
115
115
|
- lib/attr_enumerable/shuffle_attr.rb
|
116
|
+
- lib/attr_enumerable/slice_attr.rb
|
116
117
|
- lib/attributes_hashable.rb
|
117
118
|
- lib/attributes_initializable.rb
|
118
119
|
- lib/end_erb.rb
|
@@ -284,6 +285,7 @@ files:
|
|
284
285
|
- spec/attr_enumerable/sample_attr_spec.rb
|
285
286
|
- spec/attr_enumerable/select_attr_spec.rb
|
286
287
|
- spec/attr_enumerable/shuffle_attr_spec.rb
|
288
|
+
- spec/attr_enumerable/slice_attr_spec.rb
|
287
289
|
- spec/attributes_hashable_spec.rb
|
288
290
|
- spec/attributes_initializable_spec.rb
|
289
291
|
- spec/eval_helper/attr_accessor_init_code_spec.rb
|
@@ -462,6 +464,7 @@ test_files:
|
|
462
464
|
- spec/attr_enumerable/sample_attr_spec.rb
|
463
465
|
- spec/attr_enumerable/select_attr_spec.rb
|
464
466
|
- spec/attr_enumerable/shuffle_attr_spec.rb
|
467
|
+
- spec/attr_enumerable/slice_attr_spec.rb
|
465
468
|
- spec/attributes_hashable_spec.rb
|
466
469
|
- spec/attributes_initializable_spec.rb
|
467
470
|
- spec/eval_helper/attr_accessor_init_code_spec.rb
|