tbpgr_utils 0.0.143 → 0.0.144
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +38 -2
- data/lib/attr_enumerable/attr_enumerable_helper.rb +2 -1
- data/lib/attr_enumerable/last_attr.rb +15 -0
- data/lib/tbpgr_utils/version.rb +1 -1
- data/spec/attr_enumerable/last_attr_spec.rb +107 -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: f68df29b7d9250f632215eed0f6828625de3e6b0
|
4
|
+
data.tar.gz: 82ef8056f7390e8bc68af39b6d8ea7e417da41ed
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5a24a7b96922ded4523b04dd4da4ab6b1c598ef3ef27c7f324bfccfd00e970a8ddb948c9a67381743b527c4f513b7219deab0550fb0a3dcb88cc9fcb117f52f3
|
7
|
+
data.tar.gz: 2a994844d07cef1651bdc984f33b48a78302e3e0d7505683e0ee385dc7ade9b64b059a81653ebeb042ab34588428934167d47ff66fed9a69bf2f2005f1a31556
|
data/README.md
CHANGED
@@ -64,10 +64,11 @@ Or install it yourself as:
|
|
64
64
|
|[AttrEnumerable.compact_attr](#attrenumerablecompact_attr) |define compact_xxx. it returns Class attributes(collection)'s that exclude nil elements. |
|
65
65
|
|[AttrEnumerable.concat_attr](#attrenumerableconcat_attr) |define concat_xxx. it returns Class attributes(collection) and argument array |
|
66
66
|
|[AttrEnumerable.delete_attr](#attrenumerabledelete_attr) |define delete_xxx. it delete Class attributes(collection) that match delete condition |
|
67
|
-
|[AttrEnumerable.first_attr](#attrenumerablefirst_attr) |define first_xxx. it returns Class attributes(collection) first N
|
67
|
+
|[AttrEnumerable.first_attr](#attrenumerablefirst_attr) |define first_xxx. it returns Class attributes(collection) first N element |
|
68
68
|
|[AttrEnumerable.each_attr](#attrenumerableeach_attr) |define each_xxx. it call Class attributes(collection)'s attribute iterator |
|
69
69
|
|[AttrEnumerable.each_attr_with_index](#attrenumerableeach_attr_with_index) |define each_xxx_with_index. it call Class attributes(collection)'s attribute iterator with index |
|
70
70
|
|[AttrEnumerable.include_attr?](#attrenumerableinclude_attr) |define include_xxx?. it returns Class attributes(collection)'s attribute include value |
|
71
|
+
|[AttrEnumerable.last_attr](#attrenumerablelast_attr) |define last_xxx. it returns Class attributes(collection) last N elementt |
|
71
72
|
|[AttrEnumerable.reverse_attr](#attrenumerablereverse_attr) |define reverse_xxx. it returns Class attributes(collection)'s reverse Array |
|
72
73
|
|[AttributesHashable.to_hash](#attributeshashableto_hash) |define to_hash method for get instance_values |
|
73
74
|
|[AttributesInitializable::ClassMethods.attr_accessor_init](#attributesinitializableclassmethodsattr_accessor_init) |generate attr_accessor + initializer |
|
@@ -1304,7 +1305,6 @@ persons.each_age_with_index do |age, i|
|
|
1304
1305
|
end
|
1305
1306
|
~~~
|
1306
1307
|
|
1307
|
-
|
1308
1308
|
[back to list](#list)
|
1309
1309
|
|
1310
1310
|
### AttrEnumerable.first_attr
|
@@ -1370,6 +1370,41 @@ print persons.include_name?('sato') # => false
|
|
1370
1370
|
print persons.include_age?(84) # => true
|
1371
1371
|
~~~
|
1372
1372
|
|
1373
|
+
|
1374
|
+
[back to list](#list)
|
1375
|
+
|
1376
|
+
### AttrEnumerable.first_attr
|
1377
|
+
~~~ruby
|
1378
|
+
require 'attr_enumerable'
|
1379
|
+
|
1380
|
+
class Person
|
1381
|
+
attr_reader :name, :age
|
1382
|
+
def initialize(name, age)
|
1383
|
+
@name, @age = name, age
|
1384
|
+
end
|
1385
|
+
end
|
1386
|
+
|
1387
|
+
class Persons
|
1388
|
+
attr_reader :persons
|
1389
|
+
include AttrEnumerable
|
1390
|
+
def initialize(persons = [])
|
1391
|
+
@persons = persons
|
1392
|
+
end
|
1393
|
+
|
1394
|
+
def <<(person)
|
1395
|
+
@persons << person
|
1396
|
+
end
|
1397
|
+
end
|
1398
|
+
|
1399
|
+
persons = Persons.new([Person.new("tanaka", 84), Person.new("tanaka", 20), Person.new("suzuki", 20)])
|
1400
|
+
print persons.last_name # => 'suzuki'
|
1401
|
+
print persons.last_name(2) # => ['suzuki', 'tanaka']
|
1402
|
+
print persons.last_age(4) # => [84, 20, 20]
|
1403
|
+
|
1404
|
+
persons = Persons.new([])
|
1405
|
+
print persons.last_age(4) # => []
|
1406
|
+
~~~
|
1407
|
+
|
1373
1408
|
[back to list](#list)
|
1374
1409
|
|
1375
1410
|
### AttrEnumerable.reverse_attr
|
@@ -4105,6 +4140,7 @@ if you are Sublime Text2 user, you can use snippet for TbpgrUtils.
|
|
4105
4140
|
https://github.com/tbpgr/tbpgr_utils_snippets
|
4106
4141
|
|
4107
4142
|
## History
|
4143
|
+
* version 0.0.144 : add AttrEnumerable.last_attr
|
4108
4144
|
* version 0.0.143 : add AttrEnumerable.include_attr?
|
4109
4145
|
* version 0.0.142 : add AttrEnumerable.first_attr
|
4110
4146
|
* version 0.0.141 : add AttrEnumerable.delete_attr
|
@@ -12,7 +12,8 @@ module AttrEnumerable
|
|
12
12
|
{ regexp: /^concat_(.*)$/, call_method: :concat_attr },
|
13
13
|
{ regexp: /^delete_(.*)$/, call_method: :delete_attr },
|
14
14
|
{ regexp: /^first_(.*)$/, call_method: :first_attr },
|
15
|
-
{ regexp: /^include_(.*)\?$/, call_method: :include_attribute? }
|
15
|
+
{ regexp: /^include_(.*)\?$/, call_method: :include_attribute? },
|
16
|
+
{ regexp: /^last_(.*)$/, call_method: :last_attr }
|
16
17
|
]
|
17
18
|
|
18
19
|
# 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 last_attr(attribute, method_name, *args, &block)
|
8
|
+
col = collection
|
9
|
+
return [] if col.empty?
|
10
|
+
last_size = args.size == 0 ? nil : Integer(args.last)
|
11
|
+
fail ArgumentError, "invalid attribute #{attribute}" unless include_attr?(col.last, attribute)
|
12
|
+
attrs = col.map { |v|v.send(attribute) }
|
13
|
+
last_size.nil? ? attrs.last : attrs.last(last_size)
|
14
|
+
end
|
15
|
+
end
|
data/lib/tbpgr_utils/version.rb
CHANGED
@@ -0,0 +1,107 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require 'spec_helper'
|
3
|
+
require 'attr_enumerable'
|
4
|
+
|
5
|
+
describe AttrEnumerable do
|
6
|
+
context :last_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: :last_name,
|
38
|
+
last: nil,
|
39
|
+
expected: ['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: :last_age,
|
52
|
+
last: 2,
|
53
|
+
expected: [20, 20],
|
54
|
+
},
|
55
|
+
{
|
56
|
+
case_no: 3,
|
57
|
+
case_title: 'over index case',
|
58
|
+
klass: AttrEnumerablePersons.new(
|
59
|
+
[
|
60
|
+
AttrEnumerablePerson.new('tanaka', 84),
|
61
|
+
AttrEnumerablePerson.new('tanaka', 20),
|
62
|
+
AttrEnumerablePerson.new('suzuki', 20)
|
63
|
+
]
|
64
|
+
),
|
65
|
+
method: :last_age,
|
66
|
+
last: 4,
|
67
|
+
expected: [20, 20, 84],
|
68
|
+
},
|
69
|
+
{
|
70
|
+
case_no: 4,
|
71
|
+
case_title: 'empty case',
|
72
|
+
klass: AttrEnumerablePersons.new([]),
|
73
|
+
method: :last_name,
|
74
|
+
last: nil,
|
75
|
+
expected: [],
|
76
|
+
}
|
77
|
+
]
|
78
|
+
|
79
|
+
cases.each do |c|
|
80
|
+
it "|case_no=#{c[:case_no]}|case_title=#{c[:case_title]}" do
|
81
|
+
begin
|
82
|
+
case_before c
|
83
|
+
|
84
|
+
# -- given --
|
85
|
+
# nothing
|
86
|
+
ary = c[:klass]
|
87
|
+
|
88
|
+
# -- when --
|
89
|
+
actual = c[:last] ? ary.send(c[:method], c[:last]) : ary.send(c[:method])
|
90
|
+
|
91
|
+
# -- then --
|
92
|
+
expect(Array(actual)).to match_array(c[:expected])
|
93
|
+
ensure
|
94
|
+
case_after c
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
def case_before(c)
|
99
|
+
# implement each case before
|
100
|
+
end
|
101
|
+
|
102
|
+
def case_after(c)
|
103
|
+
# implement each case after
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
107
|
+
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.144
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- tbpgr
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-06-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -106,6 +106,7 @@ files:
|
|
106
106
|
- lib/attr_enumerable/each_attr_with_index.rb
|
107
107
|
- lib/attr_enumerable/first_attr.rb
|
108
108
|
- lib/attr_enumerable/include_attr.rb
|
109
|
+
- lib/attr_enumerable/last_attr.rb
|
109
110
|
- lib/attr_enumerable/reverse_attr.rb
|
110
111
|
- lib/attributes_hashable.rb
|
111
112
|
- lib/attributes_initializable.rb
|
@@ -271,6 +272,7 @@ files:
|
|
271
272
|
- spec/attr_enumerable/each_attr_with_index_spec.rb
|
272
273
|
- spec/attr_enumerable/first_attr_spec.rb
|
273
274
|
- spec/attr_enumerable/include_attr_spec.rb
|
275
|
+
- spec/attr_enumerable/last_attr_spec.rb
|
274
276
|
- spec/attr_enumerable/reverse_attr_spec.rb
|
275
277
|
- spec/attributes_hashable_spec.rb
|
276
278
|
- spec/attributes_initializable_spec.rb
|
@@ -443,6 +445,7 @@ test_files:
|
|
443
445
|
- spec/attr_enumerable/each_attr_with_index_spec.rb
|
444
446
|
- spec/attr_enumerable/first_attr_spec.rb
|
445
447
|
- spec/attr_enumerable/include_attr_spec.rb
|
448
|
+
- spec/attr_enumerable/last_attr_spec.rb
|
446
449
|
- spec/attr_enumerable/reverse_attr_spec.rb
|
447
450
|
- spec/attributes_hashable_spec.rb
|
448
451
|
- spec/attributes_initializable_spec.rb
|