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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a04fd972d51e9a1cca40f5ca22e5ce60c791fafb
4
- data.tar.gz: 5058986a81274eda20e8daeb224c164308086256
3
+ metadata.gz: 355d0061397d2091ab112230f5cf80b9c233359a
4
+ data.tar.gz: 78dc4e5796f88f731af8317200ca721b3128867a
5
5
  SHA512:
6
- metadata.gz: 963693f1d332754521be42cb9e0f28d507baa47b80eaf7ac5ec45c8885b3ebd494da3d50a3bfca46cb5e4fd5bae837d4cef96d1efbe76072a2d282f43f851437
7
- data.tar.gz: 033f99cf39352ac6a032052eeb3299912cf05bf8997fa71298f972009ee81eaf0e15568f9832d5d888eda511148c49f1732bc366b1d99f5f61411a763acc8be9
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
- rescue
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
@@ -4,4 +4,5 @@
4
4
  module AttrEnumerable
5
5
  require 'attr_enumerable/each_attr'
6
6
  require 'attr_enumerable/each_attr_with_index'
7
+ require 'attr_enumerable/reverse_attr'
7
8
  end
@@ -2,5 +2,5 @@
2
2
 
3
3
  # Tbpgr Utilities
4
4
  module TbpgrUtils
5
- VERSION = '0.0.136'
5
+ VERSION = '0.0.137'
6
6
  end
@@ -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.136
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-24 00:00:00.000000000 Z
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