tbpgr_utils 0.0.134 → 0.0.135
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 +36 -0
- data/lib/attr_enumerable.rb +29 -0
- data/lib/tbpgr_utils/version.rb +1 -1
- data/spec/attr_enumerable_spec.rb +81 -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: b98d24e3c4feef51fa216b88cb76d69910a0c86a
|
4
|
+
data.tar.gz: d7c95e332b3f975abf0dc358922655f7c07ae46f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cbc83ca317bcaa82610c48d0313deb245851a34ae1b181520d86e9fc9274d4d287060df589bedf87272355881db7b7cd971e1c1bc351628877ba80ed86e46cb8
|
7
|
+
data.tar.gz: 154ccd41ff57237b88ab9f7dbe00b7214afc2d147525180e243de7d20c44f2299f1fbc22404f3402c3564d7cb49788a6ffbb690f6c477e0f96f5ce16c8599121
|
data/README.md
CHANGED
@@ -60,6 +60,7 @@ Or install it yourself as:
|
|
60
60
|
|[TbpgrUtils Array#together_slice](#arraytogether_sliceor-tslice) |together version of Array#slice. together_slice has alias :tslice |
|
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
|
+
|[AttrEnumerable#each_attr](#attrenumerable_each_attr) |define each_xxx. it call Class attributes(collection)'s attribute iterator |
|
63
64
|
|[AttributesHashable.to_hash](#attributeshashableto_hash) |define to_hash method for get instance_values |
|
64
65
|
|[AttributesInitializable::ClassMethods.attr_accessor_init](#attributesinitializableclassmethodsattr_accessor_init) |generate attr_accessor + initializer |
|
65
66
|
|[AttributesInitializable::ClassMethods.attr_reader_init](#attributesinitializableclassmethodsattr_reader_init) |generate attr_reader + initializer |
|
@@ -1100,6 +1101,40 @@ require 'tbpgr_utils'
|
|
1100
1101
|
|
1101
1102
|
[back to list](#list)
|
1102
1103
|
|
1104
|
+
### AttrEnumerable.each_attr
|
1105
|
+
~~~ruby
|
1106
|
+
require 'attr_enumerable'
|
1107
|
+
class Person
|
1108
|
+
attr_reader :name, :age
|
1109
|
+
def initialize(name, age)
|
1110
|
+
@name, @age = name, age
|
1111
|
+
end
|
1112
|
+
end
|
1113
|
+
|
1114
|
+
class Persons
|
1115
|
+
attr_reader :persons
|
1116
|
+
include AttrEnumerable
|
1117
|
+
def initialize(persons = [])
|
1118
|
+
@persons = persons
|
1119
|
+
end
|
1120
|
+
|
1121
|
+
def <<(person)
|
1122
|
+
@persons << person
|
1123
|
+
end
|
1124
|
+
end
|
1125
|
+
|
1126
|
+
persons = Persons.new([Person.new("tanaka", 84), Person.new("suzuki", 103)])
|
1127
|
+
persons.each_name do |name|
|
1128
|
+
puts name # => "tanaka", "suzuki"
|
1129
|
+
end
|
1130
|
+
|
1131
|
+
persons.each_age do |age|
|
1132
|
+
puts age # => 84, 103
|
1133
|
+
end
|
1134
|
+
~~~
|
1135
|
+
|
1136
|
+
[back to list](#list)
|
1137
|
+
|
1103
1138
|
### AttributesHashable.to_hash
|
1104
1139
|
~~~ruby
|
1105
1140
|
require 'attributes_initializable'
|
@@ -3802,6 +3837,7 @@ if you are Sublime Text2 user, you can use snippet for TbpgrUtils.
|
|
3802
3837
|
https://github.com/tbpgr/tbpgr_utils_snippets
|
3803
3838
|
|
3804
3839
|
## History
|
3840
|
+
* version 0.0.135 : add AttrEnumerable.each_attr
|
3805
3841
|
* version 0.0.134 : add Integer#reverse_each_digit
|
3806
3842
|
* version 0.0.133 : add Integer#each_digit_with_index
|
3807
3843
|
* version 0.0.132 : add Integer#each_digit
|
@@ -0,0 +1,29 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require 'active_support/inflector'
|
3
|
+
|
4
|
+
# AttrEnumerable
|
5
|
+
module AttrEnumerable
|
6
|
+
# define each_xxx.
|
7
|
+
#
|
8
|
+
# xxx is attribute name.
|
9
|
+
# Class XxxxYyyys must have attributes xxxx_yyyys.
|
10
|
+
# xxxx_yyyys is collection of Class XxxxYyyy.
|
11
|
+
# XxxxYyyy has attributes.
|
12
|
+
def method_missing(method_name, *args, &block)
|
13
|
+
if method_name =~ /each_/
|
14
|
+
attribute = method_name.to_s.scan(/each_(.*)/).first.first
|
15
|
+
each_attr(attribute, &block)
|
16
|
+
else
|
17
|
+
super(method_name, *args, &block)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
def each_attr(attribute, &block)
|
23
|
+
collection = self.instance_variable_get("@#{self.class.name.underscore}")
|
24
|
+
collection.each do |element|
|
25
|
+
super(method_name, *args) unless element.instance_variables.include? :"@#{attribute}"
|
26
|
+
yield element.send(attribute)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
data/lib/tbpgr_utils/version.rb
CHANGED
@@ -0,0 +1,81 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require 'spec_helper'
|
3
|
+
require 'attr_enumerable'
|
4
|
+
|
5
|
+
describe AttrEnumerable do
|
6
|
+
context :each_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: :each_name,
|
37
|
+
expected: ['tanaka', 'suzuki']
|
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: :each_age,
|
49
|
+
expected: [84, 103]
|
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
|
+
ret = []
|
63
|
+
c[:klass].send c[:method] { |attribute| ret << attribute }
|
64
|
+
|
65
|
+
# -- then --
|
66
|
+
expect(ret).to eq(c[:expected])
|
67
|
+
ensure
|
68
|
+
case_after c
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
def case_before(c)
|
73
|
+
# implement each case before
|
74
|
+
end
|
75
|
+
|
76
|
+
def case_after(c)
|
77
|
+
# implement each case after
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
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.135
|
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-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -96,6 +96,7 @@ files:
|
|
96
96
|
- LICENSE.txt
|
97
97
|
- README.md
|
98
98
|
- Rakefile
|
99
|
+
- lib/attr_enumerable.rb
|
99
100
|
- lib/attributes_hashable.rb
|
100
101
|
- lib/attributes_initializable.rb
|
101
102
|
- lib/end_erb.rb
|
@@ -252,6 +253,7 @@ files:
|
|
252
253
|
- lib/test_toolbox.rb
|
253
254
|
- lib/test_toolbox/kernel.rb
|
254
255
|
- rubocop-todo.yml
|
256
|
+
- spec/attr_enumerable_spec.rb
|
255
257
|
- spec/attributes_hashable_spec.rb
|
256
258
|
- spec/attributes_initializable_spec.rb
|
257
259
|
- spec/eval_helper/attr_accessor_init_code_spec.rb
|
@@ -415,6 +417,7 @@ signing_key:
|
|
415
417
|
specification_version: 4
|
416
418
|
summary: Utilities
|
417
419
|
test_files:
|
420
|
+
- spec/attr_enumerable_spec.rb
|
418
421
|
- spec/attributes_hashable_spec.rb
|
419
422
|
- spec/attributes_initializable_spec.rb
|
420
423
|
- spec/eval_helper/attr_accessor_init_code_spec.rb
|