tbpgr_utils 0.0.140 → 0.0.141
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 +32 -0
- data/lib/attr_enumerable/attr_enumerable_helper.rb +39 -42
- data/lib/attr_enumerable/delete_attr.rb +15 -0
- data/lib/attr_enumerable.rb +7 -11
- data/lib/tbpgr_utils/version.rb +1 -1
- data/spec/attr_enumerable/delete_attr_spec.rb +114 -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: 117dbd6a2063545d0db523fe4f88926b0a08501a
|
4
|
+
data.tar.gz: aff26d7cd373acc6057c714471d5a179e430658a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 836802fb1e8b97e07928ca03ce7decf3a5fe3fd1a32f3fd6eab089d21ec9df9107e8d55a58abee1d019c3671b07ee7be1963ee6b3c448e88cc81f171e97623b6
|
7
|
+
data.tar.gz: c9a334cf30567cfa2af433f0567df99e5669f6696e026cc3b8565572ac0cb6b948e119c6d15609e3b5cdd353ad6ffbbd7ef682e626382104ecd90edc4d0e6162
|
data/README.md
CHANGED
@@ -63,6 +63,7 @@ Or install it yourself as:
|
|
63
63
|
|[AttrEnumerable.at_attr](#attrenumerableat_attr) |define at_xxx. it returns Class attributes(collection)'s at result. |
|
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
|
+
|[AttrEnumerable.delete_attr](#attrenumerabledelete_attr) |define delete_xxx. it delete Class attributes(collection) that match delete condition |
|
66
67
|
|[AttrEnumerable.each_attr](#attrenumerableeach_attr) |define each_xxx. it call Class attributes(collection)'s attribute iterator |
|
67
68
|
|[AttrEnumerable.each_attr_with_index](#attrenumerableeach_attr_with_index) |define each_xxx_with_index. it call Class attributes(collection)'s attribute iterator with index |
|
68
69
|
|[AttrEnumerable.reverse_attr](#attrenumerablereverse_attr) |define reverse_xxx. it returns Class attributes(collection)'s reverse Array |
|
@@ -1205,6 +1206,36 @@ persons.concat_name(["sato", "matsumoto"]) # => ["sato", "matsumoto"]
|
|
1205
1206
|
|
1206
1207
|
[back to list](#list)
|
1207
1208
|
|
1209
|
+
### AttrEnumerable.delete_attr
|
1210
|
+
~~~ruby
|
1211
|
+
require 'attr_enumerable'
|
1212
|
+
class Person
|
1213
|
+
attr_reader :name, :age
|
1214
|
+
def initialize(name, age)
|
1215
|
+
@name, @age = name, age
|
1216
|
+
end
|
1217
|
+
end
|
1218
|
+
|
1219
|
+
class Persons
|
1220
|
+
attr_reader :persons
|
1221
|
+
include AttrEnumerable
|
1222
|
+
def initialize(persons = [])
|
1223
|
+
@persons = persons
|
1224
|
+
end
|
1225
|
+
|
1226
|
+
def <<(person)
|
1227
|
+
@persons << person
|
1228
|
+
end
|
1229
|
+
end
|
1230
|
+
|
1231
|
+
persons = Persons.new([Person.new("tanaka", 84), Person.new("tanaka", 99), Person.new("suzuki", 99)])
|
1232
|
+
persons.delete_name("tanaka") # => persons = Persons.new(Person.new("suzuki", 99)])
|
1233
|
+
persons = Persons.new([Person.new("tanaka", 84), Person.new("tanaka", 99), Person.new("suzuki", 99)])
|
1234
|
+
persons.delete_age(99) # => persons = Persons.new([Person.new("tanaka", 84)])
|
1235
|
+
~~~
|
1236
|
+
|
1237
|
+
[back to list](#list)
|
1238
|
+
|
1208
1239
|
### AttrEnumerable.each_attr
|
1209
1240
|
~~~ruby
|
1210
1241
|
require 'attr_enumerable'
|
@@ -4006,6 +4037,7 @@ if you are Sublime Text2 user, you can use snippet for TbpgrUtils.
|
|
4006
4037
|
https://github.com/tbpgr/tbpgr_utils_snippets
|
4007
4038
|
|
4008
4039
|
## History
|
4040
|
+
* version 0.0.141 : add AttrEnumerable.delete_attr
|
4009
4041
|
* version 0.0.140 : add AttrEnumerable.concat_attr
|
4010
4042
|
* version 0.0.139 : add AttrEnumerable.compact_attr
|
4011
4043
|
* version 0.0.138 : add AttrEnumerable.at_attr
|
@@ -1,42 +1,39 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
require 'active_support/inflector'
|
3
|
-
|
4
|
-
# AttrEnumerable
|
5
|
-
module AttrEnumerable
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
{ regexp:
|
10
|
-
|
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
|
-
instance_variable_get("@#{self.class.name.underscore}")
|
41
|
-
end
|
42
|
-
end
|
1
|
+
# encoding: utf-8
|
2
|
+
require 'active_support/inflector'
|
3
|
+
|
4
|
+
# AttrEnumerable
|
5
|
+
module AttrEnumerable
|
6
|
+
TARGET_METHOD_FILES = Dir.glob("#{File.dirname(__FILE__)}/*_attr*.rb").map { |file|File.basename(file, '.rb') }
|
7
|
+
TARGET_METHODS = TARGET_METHOD_FILES.map do |v|
|
8
|
+
regexp = v.gsub('attr', '(.*)')
|
9
|
+
{ regexp: /^#{regexp}$/, call_method: v.to_sym }
|
10
|
+
end.reverse
|
11
|
+
|
12
|
+
# call attr enumerable method.
|
13
|
+
def method_missing(method_name, *args, &block)
|
14
|
+
target_method = detect(method_name)
|
15
|
+
send(target_method[:call_method], target_method[:attribute], method_name, *args, &block)
|
16
|
+
rescue
|
17
|
+
super(method_name, *args, &block)
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
def detect(method_name)
|
22
|
+
TARGET_METHODS.each do |target_method|
|
23
|
+
regexp = target_method[:regexp]
|
24
|
+
if method_name.to_s =~ regexp
|
25
|
+
attribute = method_name.to_s.scan(regexp).first.first
|
26
|
+
return { call_method: target_method[:call_method], attribute: attribute }
|
27
|
+
end
|
28
|
+
end
|
29
|
+
fail NoMethodError, "method is not exists #{method_name}"
|
30
|
+
end
|
31
|
+
|
32
|
+
def include_attr?(element, attribute)
|
33
|
+
element.instance_variables.include? :"@#{attribute}"
|
34
|
+
end
|
35
|
+
|
36
|
+
def collection
|
37
|
+
instance_variable_get("@#{self.class.name.underscore}")
|
38
|
+
end
|
39
|
+
end
|
@@ -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 delete_attr(attribute, method_name, *args, &block)
|
8
|
+
del_target = args.first
|
9
|
+
return [] if collection.empty?
|
10
|
+
super(method_name, *args) unless include_attr?(collection.first, attribute)
|
11
|
+
del_targets = collection.select { |v|v.send(attribute) == del_target }
|
12
|
+
del_targets.each { |each_target|collection.delete(each_target) }
|
13
|
+
del_targets
|
14
|
+
end
|
15
|
+
end
|
data/lib/attr_enumerable.rb
CHANGED
@@ -1,11 +1,7 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
|
3
|
-
# AttrEnumerable
|
4
|
-
module AttrEnumerable
|
5
|
-
|
6
|
-
require
|
7
|
-
|
8
|
-
require 'attr_enumerable/each_attr'
|
9
|
-
require 'attr_enumerable/each_attr_with_index'
|
10
|
-
require 'attr_enumerable/reverse_attr'
|
11
|
-
end
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
# AttrEnumerable
|
4
|
+
module AttrEnumerable
|
5
|
+
REQUIRES = Dir.glob("#{File.dirname(__FILE__)}/attr_enumerable/*_attr*.rb").map { |file|File.basename(file, '.rb') }
|
6
|
+
REQUIRES.each { |require_lib|require "attr_enumerable/#{require_lib}" }
|
7
|
+
end
|
data/lib/tbpgr_utils/version.rb
CHANGED
@@ -0,0 +1,114 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require 'spec_helper'
|
3
|
+
require 'attr_enumerable'
|
4
|
+
|
5
|
+
describe AttrEnumerable do
|
6
|
+
context :delete_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: :delete_name,
|
38
|
+
delete: 'tanaka',
|
39
|
+
expected_return: ['tanaka', 'tanaka'],
|
40
|
+
expected_size: 1
|
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: :delete_age,
|
53
|
+
delete: 20,
|
54
|
+
expected_return: [20, 20],
|
55
|
+
expected_size: 1
|
56
|
+
},
|
57
|
+
{
|
58
|
+
case_no: 3,
|
59
|
+
case_title: 'no delete target 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: :delete_name,
|
68
|
+
delete: 'sato',
|
69
|
+
expected_return: [],
|
70
|
+
expected_size: 3
|
71
|
+
},
|
72
|
+
{
|
73
|
+
case_no: 4,
|
74
|
+
case_title: 'empty case',
|
75
|
+
klass: AttrEnumerablePersons.new([]),
|
76
|
+
method: :delete_name,
|
77
|
+
delete: 'tanaka',
|
78
|
+
expected_return: [],
|
79
|
+
expected_size: 0
|
80
|
+
}
|
81
|
+
]
|
82
|
+
|
83
|
+
cases.each do |c|
|
84
|
+
it "|case_no=#{c[:case_no]}|case_title=#{c[:case_title]}" do
|
85
|
+
begin
|
86
|
+
case_before c
|
87
|
+
|
88
|
+
# -- given --
|
89
|
+
# nothing
|
90
|
+
ary = c[:klass]
|
91
|
+
|
92
|
+
# -- when --
|
93
|
+
actual = ary.send c[:method], c[:delete]
|
94
|
+
attribute = c[:method].to_s.gsub('delete_', '').to_sym
|
95
|
+
actual = actual.map { |v|v.send(attribute) }
|
96
|
+
|
97
|
+
# -- then --
|
98
|
+
expect(actual).to match_array(c[:expected_return])
|
99
|
+
expect(ary.attr_enumerable_persons.size).to eq(c[:expected_size])
|
100
|
+
ensure
|
101
|
+
case_after c
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
def case_before(c)
|
106
|
+
# implement each case before
|
107
|
+
end
|
108
|
+
|
109
|
+
def case_after(c)
|
110
|
+
# implement each case after
|
111
|
+
end
|
112
|
+
end
|
113
|
+
end
|
114
|
+
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.141
|
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-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -101,6 +101,7 @@ files:
|
|
101
101
|
- lib/attr_enumerable/attr_enumerable_helper.rb
|
102
102
|
- lib/attr_enumerable/compact_attr.rb
|
103
103
|
- lib/attr_enumerable/concat_attr.rb
|
104
|
+
- lib/attr_enumerable/delete_attr.rb
|
104
105
|
- lib/attr_enumerable/each_attr.rb
|
105
106
|
- lib/attr_enumerable/each_attr_with_index.rb
|
106
107
|
- lib/attr_enumerable/reverse_attr.rb
|
@@ -263,6 +264,7 @@ files:
|
|
263
264
|
- spec/attr_enumerable/at_attr_spec.rb
|
264
265
|
- spec/attr_enumerable/compact_attr_spec.rb
|
265
266
|
- spec/attr_enumerable/concat_attr_spec.rb
|
267
|
+
- spec/attr_enumerable/delete_attr_spec.rb
|
266
268
|
- spec/attr_enumerable/each_attr_spec.rb
|
267
269
|
- spec/attr_enumerable/each_attr_with_index_spec.rb
|
268
270
|
- spec/attr_enumerable/reverse_attr_spec.rb
|
@@ -432,6 +434,7 @@ test_files:
|
|
432
434
|
- spec/attr_enumerable/at_attr_spec.rb
|
433
435
|
- spec/attr_enumerable/compact_attr_spec.rb
|
434
436
|
- spec/attr_enumerable/concat_attr_spec.rb
|
437
|
+
- spec/attr_enumerable/delete_attr_spec.rb
|
435
438
|
- spec/attr_enumerable/each_attr_spec.rb
|
436
439
|
- spec/attr_enumerable/each_attr_with_index_spec.rb
|
437
440
|
- spec/attr_enumerable/reverse_attr_spec.rb
|