tbpgr_utils 0.0.136 → 0.0.137
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +33 -0
- data/lib/attr_enumerable/attr_enumerable_helper.rb +4 -3
- data/lib/attr_enumerable/each_attr.rb +1 -1
- data/lib/attr_enumerable/each_attr_with_index.rb +1 -1
- data/lib/attr_enumerable/reverse_attr.rb +15 -0
- data/lib/attr_enumerable.rb +1 -0
- data/lib/tbpgr_utils/version.rb +1 -1
- data/spec/attr_enumerable/reverse_attr_spec.rb +80 -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: 355d0061397d2091ab112230f5cf80b9c233359a
|
4
|
+
data.tar.gz: 78dc4e5796f88f731af8317200ca721b3128867a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5bfc588eb1ca866bbdff7e77f4ee7ea93c2d123772f6f8c379087dbdd59425a08cb09a64ff8469eaa204251210cbae322e247c02e5f81aa9b255d70d7298a2bf
|
7
|
+
data.tar.gz: 5a3a00830e0693bf526f30f8320fd9f06aa8bda96b87e80ef76caffb943df8655c8e8da15c39fa93c29cede482c870acfc614e06fbb77849d57f786e4790e395
|
data/README.md
CHANGED
@@ -62,6 +62,7 @@ Or install it yourself as:
|
|
62
62
|
|[TbpgrUtils Array#uniq_size](#arrayuniq_size) |return uniq size |
|
63
63
|
|[AttrEnumerable.each_attr](#attrenumerableeach_attr) |define each_xxx. it call Class attributes(collection)'s attribute iterator |
|
64
64
|
|[AttrEnumerable.each_attr_with_index](#attrenumerableeach_attr_with_index) |define each_xxx_with_index. it call Class attributes(collection)'s attribute iterator with index |
|
65
|
+
|[AttrEnumerable.reverse_attr](#attrenumerablereverse_attr) |define reverse_xxx. it returns Class attributes(collection)'s reverse Array |
|
65
66
|
|[AttributesHashable.to_hash](#attributeshashableto_hash) |define to_hash method for get instance_values |
|
66
67
|
|[AttributesInitializable::ClassMethods.attr_accessor_init](#attributesinitializableclassmethodsattr_accessor_init) |generate attr_accessor + initializer |
|
67
68
|
|[AttributesInitializable::ClassMethods.attr_reader_init](#attributesinitializableclassmethodsattr_reader_init) |generate attr_reader + initializer |
|
@@ -1136,6 +1137,37 @@ end
|
|
1136
1137
|
|
1137
1138
|
[back to list](#list)
|
1138
1139
|
|
1140
|
+
### AttrEnumerable.reverse_attr
|
1141
|
+
~~~ruby
|
1142
|
+
require 'attr_enumerable'
|
1143
|
+
|
1144
|
+
class Person
|
1145
|
+
attr_reader :name, :age
|
1146
|
+
def initialize(name, age)
|
1147
|
+
@name, @age = name, age
|
1148
|
+
end
|
1149
|
+
end
|
1150
|
+
|
1151
|
+
class Persons
|
1152
|
+
attr_reader :persons
|
1153
|
+
include AttrEnumerable
|
1154
|
+
def initialize(persons = [])
|
1155
|
+
@persons = persons
|
1156
|
+
end
|
1157
|
+
|
1158
|
+
def <<(person)
|
1159
|
+
@persons << person
|
1160
|
+
end
|
1161
|
+
end
|
1162
|
+
|
1163
|
+
persons = Persons.new([Person.new("tanaka", 84), Person.new("suzuki", 103)])
|
1164
|
+
print persons.reverse_name # => ['suzuki', 'tanaka']
|
1165
|
+
|
1166
|
+
print persons.reverse_age # => [103, 84]
|
1167
|
+
~~~
|
1168
|
+
|
1169
|
+
[back to list](#list)
|
1170
|
+
|
1139
1171
|
### AttrEnumerable.each_attr
|
1140
1172
|
~~~ruby
|
1141
1173
|
require 'attr_enumerable'
|
@@ -3872,6 +3904,7 @@ if you are Sublime Text2 user, you can use snippet for TbpgrUtils.
|
|
3872
3904
|
https://github.com/tbpgr/tbpgr_utils_snippets
|
3873
3905
|
|
3874
3906
|
## History
|
3907
|
+
* version 0.0.137 : add AttrEnumerable.reverse_attr
|
3875
3908
|
* version 0.0.136 : add AttrEnumerable.each_attr_with_index
|
3876
3909
|
* version 0.0.135 : add AttrEnumerable.each_attr
|
3877
3910
|
* version 0.0.134 : add Integer#reverse_each_digit
|
@@ -5,14 +5,15 @@ require 'active_support/inflector'
|
|
5
5
|
module AttrEnumerable
|
6
6
|
ATTR_METHODS = [
|
7
7
|
{ regexp: /^each_(.*)_with_index$/, call_method: :each_attr_with_index },
|
8
|
-
{ regexp: /^each_(.*)$/, call_method: :each_attr }
|
8
|
+
{ regexp: /^each_(.*)$/, call_method: :each_attr },
|
9
|
+
{ regexp: /^reverse_(.*)$/, call_method: :reverse_attr }
|
9
10
|
]
|
10
11
|
|
11
12
|
# call attr enumerable method.
|
12
13
|
def method_missing(method_name, *args, &block)
|
13
14
|
attr_method = detect(method_name)
|
14
|
-
send(attr_method[:call_method], attr_method[:attribute], &block)
|
15
|
-
|
15
|
+
send(attr_method[:call_method], attr_method[:attribute], method_name, *args, &block)
|
16
|
+
rescue
|
16
17
|
super(method_name, *args, &block)
|
17
18
|
end
|
18
19
|
|
@@ -5,7 +5,7 @@ require 'attr_enumerable/attr_enumerable_helper'
|
|
5
5
|
# AttrEnumerable
|
6
6
|
module AttrEnumerable
|
7
7
|
private
|
8
|
-
def each_attr(attribute, &block)
|
8
|
+
def each_attr(attribute, method_name, *args, &block)
|
9
9
|
collection.each do |element|
|
10
10
|
super(method_name, *args) unless include_attr?(element, attribute)
|
11
11
|
yield element.send(attribute)
|
@@ -5,7 +5,7 @@ require 'attr_enumerable/attr_enumerable_helper'
|
|
5
5
|
# AttrEnumerable
|
6
6
|
module AttrEnumerable
|
7
7
|
private
|
8
|
-
def each_attr_with_index(attribute, &block)
|
8
|
+
def each_attr_with_index(attribute, method_name, *args, &block)
|
9
9
|
collection.each_with_index do |element, index|
|
10
10
|
super(method_name, *args) unless include_attr?(element, attribute)
|
11
11
|
yield element.send(attribute), index
|
@@ -0,0 +1,15 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require 'active_support/inflector'
|
3
|
+
require 'attr_enumerable/attr_enumerable_helper'
|
4
|
+
|
5
|
+
# AttrEnumerable
|
6
|
+
module AttrEnumerable
|
7
|
+
private
|
8
|
+
def reverse_attr(attribute, *args, &block)
|
9
|
+
collection.reduce([]) do |r, e|
|
10
|
+
super(method_name, *args) unless include_attr?(e, attribute)
|
11
|
+
r << e.send(attribute)
|
12
|
+
r
|
13
|
+
end.reverse
|
14
|
+
end
|
15
|
+
end
|
data/lib/attr_enumerable.rb
CHANGED
data/lib/tbpgr_utils/version.rb
CHANGED
@@ -0,0 +1,80 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require 'spec_helper'
|
3
|
+
require 'attr_enumerable'
|
4
|
+
|
5
|
+
describe AttrEnumerable do
|
6
|
+
context :reverse_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('suzuki', 103)
|
34
|
+
]
|
35
|
+
),
|
36
|
+
method: :reverse_name,
|
37
|
+
expected: ['suzuki', 'tanaka']
|
38
|
+
},
|
39
|
+
{
|
40
|
+
case_no: 2,
|
41
|
+
case_title: 'age case',
|
42
|
+
klass: AttrEnumerablePersons.new(
|
43
|
+
[
|
44
|
+
AttrEnumerablePerson.new('tanaka', 84),
|
45
|
+
AttrEnumerablePerson.new('suzuki', 103)
|
46
|
+
]
|
47
|
+
),
|
48
|
+
method: :reverse_age,
|
49
|
+
expected: [103, 84]
|
50
|
+
},
|
51
|
+
]
|
52
|
+
|
53
|
+
cases.each do |c|
|
54
|
+
it "|case_no=#{c[:case_no]}|case_title=#{c[:case_title]}" do
|
55
|
+
begin
|
56
|
+
case_before c
|
57
|
+
|
58
|
+
# -- given --
|
59
|
+
# nothing
|
60
|
+
|
61
|
+
# -- when --
|
62
|
+
actual = c[:klass].send c[:method]
|
63
|
+
|
64
|
+
# -- then --
|
65
|
+
expect(actual).to eq(c[:expected])
|
66
|
+
ensure
|
67
|
+
case_after c
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
def case_before(c)
|
72
|
+
# implement each case before
|
73
|
+
end
|
74
|
+
|
75
|
+
def case_after(c)
|
76
|
+
# implement each case after
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
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.137
|
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-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -100,6 +100,7 @@ files:
|
|
100
100
|
- lib/attr_enumerable/attr_enumerable_helper.rb
|
101
101
|
- lib/attr_enumerable/each_attr.rb
|
102
102
|
- lib/attr_enumerable/each_attr_with_index.rb
|
103
|
+
- lib/attr_enumerable/reverse_attr.rb
|
103
104
|
- lib/attributes_hashable.rb
|
104
105
|
- lib/attributes_initializable.rb
|
105
106
|
- lib/end_erb.rb
|
@@ -258,6 +259,7 @@ files:
|
|
258
259
|
- rubocop-todo.yml
|
259
260
|
- spec/attr_enumerable/each_attr_spec.rb
|
260
261
|
- spec/attr_enumerable/each_attr_with_index_spec.rb
|
262
|
+
- spec/attr_enumerable/reverse_attr_spec.rb
|
261
263
|
- spec/attributes_hashable_spec.rb
|
262
264
|
- spec/attributes_initializable_spec.rb
|
263
265
|
- spec/eval_helper/attr_accessor_init_code_spec.rb
|
@@ -423,6 +425,7 @@ summary: Utilities
|
|
423
425
|
test_files:
|
424
426
|
- spec/attr_enumerable/each_attr_spec.rb
|
425
427
|
- spec/attr_enumerable/each_attr_with_index_spec.rb
|
428
|
+
- spec/attr_enumerable/reverse_attr_spec.rb
|
426
429
|
- spec/attributes_hashable_spec.rb
|
427
430
|
- spec/attributes_initializable_spec.rb
|
428
431
|
- spec/eval_helper/attr_accessor_init_code_spec.rb
|