tbpgr_utils 0.0.142 → 0.0.143
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 +34 -0
- data/lib/attr_enumerable/attr_enumerable_helper.rb +2 -1
- data/lib/attr_enumerable/include_attr.rb +15 -0
- data/lib/tbpgr_utils/version.rb +1 -1
- data/spec/attr_enumerable/include_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: 7f2ad86d6e0bf2139289b2ee3bd13e4bad264e74
|
4
|
+
data.tar.gz: ba38197ddda032a52f18aa60c0e14550653f4f37
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bcad9bf5a09738f545e2459bedad11ef47f5d0d995e4886b25de04d852ea93c67d9f31829be7e6e4fe139306178d32994c11f6df9bc55797499791d748e7f2ae
|
7
|
+
data.tar.gz: ccf2153b9c21e92d44f82146e6af9fd0aeea71235f497f9b370e0fe4c30132047bb48d1d7f2550813317461df7d77c556672998e9de8a29c28909a24aa909cc4
|
data/README.md
CHANGED
@@ -67,6 +67,7 @@ Or install it yourself as:
|
|
67
67
|
|[AttrEnumerable.first_attr](#attrenumerablefirst_attr) |define first_xxx. it returns Class attributes(collection) first N elementthat |
|
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
|
+
|[AttrEnumerable.include_attr?](#attrenumerableinclude_attr) |define include_xxx?. it returns Class attributes(collection)'s attribute include value |
|
70
71
|
|[AttrEnumerable.reverse_attr](#attrenumerablereverse_attr) |define reverse_xxx. it returns Class attributes(collection)'s reverse Array |
|
71
72
|
|[AttributesHashable.to_hash](#attributeshashableto_hash) |define to_hash method for get instance_values |
|
72
73
|
|[AttributesInitializable::ClassMethods.attr_accessor_init](#attributesinitializableclassmethodsattr_accessor_init) |generate attr_accessor + initializer |
|
@@ -1303,6 +1304,7 @@ persons.each_age_with_index do |age, i|
|
|
1303
1304
|
end
|
1304
1305
|
~~~
|
1305
1306
|
|
1307
|
+
|
1306
1308
|
[back to list](#list)
|
1307
1309
|
|
1308
1310
|
### AttrEnumerable.first_attr
|
@@ -1339,6 +1341,37 @@ print persons.first_age(4) # => []
|
|
1339
1341
|
|
1340
1342
|
[back to list](#list)
|
1341
1343
|
|
1344
|
+
### AttrEnumerable.include_attr
|
1345
|
+
~~~
|
1346
|
+
require 'attr_enumerable'
|
1347
|
+
|
1348
|
+
class Person
|
1349
|
+
attr_reader :name, :age
|
1350
|
+
def initialize(name, age)
|
1351
|
+
@name, @age = name, age
|
1352
|
+
end
|
1353
|
+
end
|
1354
|
+
|
1355
|
+
class Persons
|
1356
|
+
attr_reader :persons
|
1357
|
+
include AttrEnumerable
|
1358
|
+
def initialize(persons = [])
|
1359
|
+
@persons = persons
|
1360
|
+
end
|
1361
|
+
|
1362
|
+
def <<(person)
|
1363
|
+
@persons << person
|
1364
|
+
end
|
1365
|
+
end
|
1366
|
+
|
1367
|
+
persons = Persons.new([Person.new("tanaka", 84), Person.new("tanaka", 20), Person.new("suzuki", 20)])
|
1368
|
+
print persons.include_name?('tanaka') # => true
|
1369
|
+
print persons.include_name?('sato') # => false
|
1370
|
+
print persons.include_age?(84) # => true
|
1371
|
+
~~~
|
1372
|
+
|
1373
|
+
[back to list](#list)
|
1374
|
+
|
1342
1375
|
### AttrEnumerable.reverse_attr
|
1343
1376
|
~~~ruby
|
1344
1377
|
require 'attr_enumerable'
|
@@ -4072,6 +4105,7 @@ if you are Sublime Text2 user, you can use snippet for TbpgrUtils.
|
|
4072
4105
|
https://github.com/tbpgr/tbpgr_utils_snippets
|
4073
4106
|
|
4074
4107
|
## History
|
4108
|
+
* version 0.0.143 : add AttrEnumerable.include_attr?
|
4075
4109
|
* version 0.0.142 : add AttrEnumerable.first_attr
|
4076
4110
|
* version 0.0.141 : add AttrEnumerable.delete_attr
|
4077
4111
|
* version 0.0.140 : add AttrEnumerable.concat_attr
|
@@ -11,7 +11,8 @@ module AttrEnumerable
|
|
11
11
|
{ regexp: /^compact_(.*)$/, call_method: :compact_attr },
|
12
12
|
{ regexp: /^concat_(.*)$/, call_method: :concat_attr },
|
13
13
|
{ regexp: /^delete_(.*)$/, call_method: :delete_attr },
|
14
|
-
{ regexp: /^first_(.*)$/, call_method: :first_attr }
|
14
|
+
{ regexp: /^first_(.*)$/, call_method: :first_attr },
|
15
|
+
{ regexp: /^include_(.*)\?$/, call_method: :include_attribute? }
|
15
16
|
]
|
16
17
|
|
17
18
|
# 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 include_attribute?(attribute, method_name, *args, &block)
|
8
|
+
col = collection
|
9
|
+
return false if col.empty?
|
10
|
+
include_value = args.first
|
11
|
+
fail ArgumentError, "invalid attribute #{attribute}" unless include_attr?(col.first, attribute)
|
12
|
+
attrs = col.map { |v|v.send(attribute) }
|
13
|
+
attrs.include?(include_value)
|
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 :include_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: :include_name?,
|
38
|
+
value: 'tanaka',
|
39
|
+
expected: true,
|
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: :include_age?,
|
52
|
+
value: 84,
|
53
|
+
expected: true,
|
54
|
+
},
|
55
|
+
{
|
56
|
+
case_no: 3,
|
57
|
+
case_title: 'name not include 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: :include_name?,
|
66
|
+
value: 'sato',
|
67
|
+
expected: false,
|
68
|
+
},
|
69
|
+
{
|
70
|
+
case_no: 4,
|
71
|
+
case_title: 'empty case',
|
72
|
+
klass: AttrEnumerablePersons.new([]),
|
73
|
+
method: :include_name?,
|
74
|
+
value: 'tanaka',
|
75
|
+
expected: false,
|
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 = ary.send(c[:method], c[:value])
|
90
|
+
|
91
|
+
# -- then --
|
92
|
+
expect(actual).to eq(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.143
|
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-31 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -105,6 +105,7 @@ files:
|
|
105
105
|
- lib/attr_enumerable/each_attr.rb
|
106
106
|
- lib/attr_enumerable/each_attr_with_index.rb
|
107
107
|
- lib/attr_enumerable/first_attr.rb
|
108
|
+
- lib/attr_enumerable/include_attr.rb
|
108
109
|
- lib/attr_enumerable/reverse_attr.rb
|
109
110
|
- lib/attributes_hashable.rb
|
110
111
|
- lib/attributes_initializable.rb
|
@@ -269,6 +270,7 @@ files:
|
|
269
270
|
- spec/attr_enumerable/each_attr_spec.rb
|
270
271
|
- spec/attr_enumerable/each_attr_with_index_spec.rb
|
271
272
|
- spec/attr_enumerable/first_attr_spec.rb
|
273
|
+
- spec/attr_enumerable/include_attr_spec.rb
|
272
274
|
- spec/attr_enumerable/reverse_attr_spec.rb
|
273
275
|
- spec/attributes_hashable_spec.rb
|
274
276
|
- spec/attributes_initializable_spec.rb
|
@@ -440,6 +442,7 @@ test_files:
|
|
440
442
|
- spec/attr_enumerable/each_attr_spec.rb
|
441
443
|
- spec/attr_enumerable/each_attr_with_index_spec.rb
|
442
444
|
- spec/attr_enumerable/first_attr_spec.rb
|
445
|
+
- spec/attr_enumerable/include_attr_spec.rb
|
443
446
|
- spec/attr_enumerable/reverse_attr_spec.rb
|
444
447
|
- spec/attributes_hashable_spec.rb
|
445
448
|
- spec/attributes_initializable_spec.rb
|