tbpgr_utils 0.0.138 → 0.0.139
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.rb +1 -0
- data/lib/attr_enumerable/attr_enumerable_helper.rb +41 -40
- data/lib/attr_enumerable/compact_attr.rb +12 -0
- data/lib/tbpgr_utils/version.rb +1 -1
- data/spec/attr_enumerable/compact_attr_spec.rb +89 -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: 2eb75c4b5853cf7c53add6ef52f538786b6d6dea
|
4
|
+
data.tar.gz: 6975b29af92ace33c6a5535c140bb8dd27d325e4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5f1fa6a0334cf5c0d84ba404ad3de34601aabbd51ac37465a9c874f070a56b8cfa4b9021e108541f87a6dc4d1a7fae4d1c90452cebe5ee6ce41ee5a875ed00e2
|
7
|
+
data.tar.gz: 9378cbebade247ad277beddf19b487c2b01750ea4141e0e8d6fb3bfb85a48a430e2846306124ca7d391f65d36e0e328c35118c99c4ab06e77a5ddd2bee147883
|
data/README.md
CHANGED
@@ -61,6 +61,7 @@ Or install it yourself as:
|
|
61
61
|
|[TbpgrUtils Array#together_with_index](#arraytogether_with_index) |loop all arrays by block with index |
|
62
62
|
|[TbpgrUtils Array#uniq_size](#arrayuniq_size) |return uniq size |
|
63
63
|
|[AttrEnumerable.at_attr](#attrenumerableat_attr) |define at_xxx. it returns Class attributes(collection)'s at result. |
|
64
|
+
|[AttrEnumerable.compact_attr](#attrenumerablecompact_attr) |define compact_xxx. it returns Class attributes(collection)'s that exclude nil elements. |
|
64
65
|
|[AttrEnumerable.each_attr](#attrenumerableeach_attr) |define each_xxx. it call Class attributes(collection)'s attribute iterator |
|
65
66
|
|[AttrEnumerable.each_attr_with_index](#attrenumerableeach_attr_with_index) |define each_xxx_with_index. it call Class attributes(collection)'s attribute iterator with index |
|
66
67
|
|[AttrEnumerable.reverse_attr](#attrenumerablereverse_attr) |define reverse_xxx. it returns Class attributes(collection)'s reverse Array |
|
@@ -1139,6 +1140,38 @@ persons.at_name 0 # => nil
|
|
1139
1140
|
|
1140
1141
|
[back to list](#list)
|
1141
1142
|
|
1143
|
+
### AttrEnumerable.compact_attr
|
1144
|
+
~~~ruby
|
1145
|
+
require 'attr_enumerable'
|
1146
|
+
class Person
|
1147
|
+
attr_reader :name, :age
|
1148
|
+
def initialize(name, age)
|
1149
|
+
@name, @age = name, age
|
1150
|
+
end
|
1151
|
+
end
|
1152
|
+
|
1153
|
+
class Persons
|
1154
|
+
attr_reader :persons
|
1155
|
+
include AttrEnumerable
|
1156
|
+
def initialize(persons = [])
|
1157
|
+
@persons = persons
|
1158
|
+
end
|
1159
|
+
|
1160
|
+
def <<(person)
|
1161
|
+
@persons << person
|
1162
|
+
end
|
1163
|
+
end
|
1164
|
+
|
1165
|
+
persons = Persons.new([Person.new("tanaka", 84), Person.new(nil, 99), Person.new("suzuki", nil)])
|
1166
|
+
persons.compact_name # => ['tanaka', 'suzuki']
|
1167
|
+
persons.compact_age # => [84, 99]
|
1168
|
+
|
1169
|
+
persons = Persons.new([])
|
1170
|
+
persons.compact_name 0 # => []
|
1171
|
+
~~~
|
1172
|
+
|
1173
|
+
[back to list](#list)
|
1174
|
+
|
1142
1175
|
### AttrEnumerable.each_attr
|
1143
1176
|
~~~ruby
|
1144
1177
|
require 'attr_enumerable'
|
@@ -3940,6 +3973,7 @@ if you are Sublime Text2 user, you can use snippet for TbpgrUtils.
|
|
3940
3973
|
https://github.com/tbpgr/tbpgr_utils_snippets
|
3941
3974
|
|
3942
3975
|
## History
|
3976
|
+
* version 0.0.139 : add AttrEnumerable.compact_attr
|
3943
3977
|
* version 0.0.138 : add AttrEnumerable.at_attr
|
3944
3978
|
* version 0.0.137 : add AttrEnumerable.reverse_attr
|
3945
3979
|
* version 0.0.136 : add AttrEnumerable.each_attr_with_index
|
data/lib/attr_enumerable.rb
CHANGED
@@ -1,40 +1,41 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
require 'active_support/inflector'
|
3
|
-
|
4
|
-
# AttrEnumerable
|
5
|
-
module AttrEnumerable
|
6
|
-
ATTR_METHODS = [
|
7
|
-
{ regexp: /^each_(.*)_with_index$/, call_method: :each_attr_with_index },
|
8
|
-
{ regexp: /^each_(.*)$/, call_method: :each_attr },
|
9
|
-
{ regexp: /^reverse_(.*)$/, call_method: :reverse_attr },
|
10
|
-
{ regexp: /^at_(.*)$/, call_method: :at_attr }
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
end
|
1
|
+
# encoding: utf-8
|
2
|
+
require 'active_support/inflector'
|
3
|
+
|
4
|
+
# AttrEnumerable
|
5
|
+
module AttrEnumerable
|
6
|
+
ATTR_METHODS = [
|
7
|
+
{ regexp: /^each_(.*)_with_index$/, call_method: :each_attr_with_index },
|
8
|
+
{ regexp: /^each_(.*)$/, call_method: :each_attr },
|
9
|
+
{ regexp: /^reverse_(.*)$/, call_method: :reverse_attr },
|
10
|
+
{ regexp: /^at_(.*)$/, call_method: :at_attr },
|
11
|
+
{ regexp: /^compact_(.*)$/, call_method: :compact_attr }
|
12
|
+
]
|
13
|
+
|
14
|
+
# call attr enumerable method.
|
15
|
+
def method_missing(method_name, *args, &block)
|
16
|
+
attr_method = detect(method_name)
|
17
|
+
send(attr_method[:call_method], attr_method[:attribute], method_name, *args, &block)
|
18
|
+
rescue
|
19
|
+
super(method_name, *args, &block)
|
20
|
+
end
|
21
|
+
|
22
|
+
private
|
23
|
+
def detect(method_name)
|
24
|
+
ATTR_METHODS.each do |attr_method|
|
25
|
+
regexp = attr_method[:regexp]
|
26
|
+
if method_name.to_s =~ regexp
|
27
|
+
attribute = method_name.to_s.scan(regexp).first.first
|
28
|
+
return { call_method: attr_method[:call_method], attribute: attribute }
|
29
|
+
end
|
30
|
+
end
|
31
|
+
fail NoMethodError, "method is not exists #{method_name}"
|
32
|
+
end
|
33
|
+
|
34
|
+
def include_attr?(element, attribute)
|
35
|
+
element.instance_variables.include? :"@#{attribute}"
|
36
|
+
end
|
37
|
+
|
38
|
+
def collection
|
39
|
+
instance_variable_get("@#{self.class.name.underscore}")
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require 'attr_enumerable/attr_enumerable_helper'
|
3
|
+
|
4
|
+
# AttrEnumerable
|
5
|
+
module AttrEnumerable
|
6
|
+
private
|
7
|
+
def compact_attr(attribute, method_name, *args, &block)
|
8
|
+
return [] if collection.empty?
|
9
|
+
super(method_name, *args) unless include_attr?(collection.first, attribute)
|
10
|
+
collection.map(&attribute.to_sym).compact
|
11
|
+
end
|
12
|
+
end
|
data/lib/tbpgr_utils/version.rb
CHANGED
@@ -0,0 +1,89 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require 'spec_helper'
|
3
|
+
require 'attr_enumerable'
|
4
|
+
|
5
|
+
describe AttrEnumerable do
|
6
|
+
context :compact_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(nil, 99),
|
34
|
+
AttrEnumerablePerson.new('suzuki', nil)
|
35
|
+
]
|
36
|
+
),
|
37
|
+
method: :compact_name,
|
38
|
+
expected: ['tanaka', 'suzuki']
|
39
|
+
},
|
40
|
+
{
|
41
|
+
case_no: 2,
|
42
|
+
case_title: 'age case',
|
43
|
+
klass: AttrEnumerablePersons.new(
|
44
|
+
[
|
45
|
+
AttrEnumerablePerson.new('tanaka', 84),
|
46
|
+
AttrEnumerablePerson.new(nil, 99),
|
47
|
+
AttrEnumerablePerson.new('suzuki', nil)
|
48
|
+
]
|
49
|
+
),
|
50
|
+
method: :compact_age,
|
51
|
+
expected: [84, 99]
|
52
|
+
},
|
53
|
+
{
|
54
|
+
case_no: 3,
|
55
|
+
case_title: 'empty case',
|
56
|
+
klass: AttrEnumerablePersons.new([]),
|
57
|
+
method: :compact_name,
|
58
|
+
expected: []
|
59
|
+
}
|
60
|
+
]
|
61
|
+
|
62
|
+
cases.each do |c|
|
63
|
+
it "|case_no=#{c[:case_no]}|case_title=#{c[:case_title]}" do
|
64
|
+
begin
|
65
|
+
case_before c
|
66
|
+
|
67
|
+
# -- given --
|
68
|
+
# nothing
|
69
|
+
|
70
|
+
# -- when --
|
71
|
+
actual = c[:klass].send c[:method]
|
72
|
+
|
73
|
+
# -- then --
|
74
|
+
expect(actual).to eq(c[:expected])
|
75
|
+
ensure
|
76
|
+
case_after c
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
def case_before(c)
|
81
|
+
# implement each case before
|
82
|
+
end
|
83
|
+
|
84
|
+
def case_after(c)
|
85
|
+
# implement each case after
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
89
|
+
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.139
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- tbpgr
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-05-
|
11
|
+
date: 2014-05-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -99,6 +99,7 @@ files:
|
|
99
99
|
- lib/attr_enumerable.rb
|
100
100
|
- lib/attr_enumerable/at_attr.rb
|
101
101
|
- lib/attr_enumerable/attr_enumerable_helper.rb
|
102
|
+
- lib/attr_enumerable/compact_attr.rb
|
102
103
|
- lib/attr_enumerable/each_attr.rb
|
103
104
|
- lib/attr_enumerable/each_attr_with_index.rb
|
104
105
|
- lib/attr_enumerable/reverse_attr.rb
|
@@ -259,6 +260,7 @@ files:
|
|
259
260
|
- lib/test_toolbox/kernel.rb
|
260
261
|
- rubocop-todo.yml
|
261
262
|
- spec/attr_enumerable/at_attr_spec.rb
|
263
|
+
- spec/attr_enumerable/compact_attr_spec.rb
|
262
264
|
- spec/attr_enumerable/each_attr_spec.rb
|
263
265
|
- spec/attr_enumerable/each_attr_with_index_spec.rb
|
264
266
|
- spec/attr_enumerable/reverse_attr_spec.rb
|
@@ -426,6 +428,7 @@ specification_version: 4
|
|
426
428
|
summary: Utilities
|
427
429
|
test_files:
|
428
430
|
- spec/attr_enumerable/at_attr_spec.rb
|
431
|
+
- spec/attr_enumerable/compact_attr_spec.rb
|
429
432
|
- spec/attr_enumerable/each_attr_spec.rb
|
430
433
|
- spec/attr_enumerable/each_attr_with_index_spec.rb
|
431
434
|
- spec/attr_enumerable/reverse_attr_spec.rb
|