tbpgr_utils 0.0.148 → 0.0.149

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: 775c44d3b88c5e2de18b5e4df26145c929a199f8
4
- data.tar.gz: b466cd6b263de3bb235dec124247002d020f31a0
3
+ metadata.gz: 616e1df4f4e621ef26207d843e83b1384cd99dfe
4
+ data.tar.gz: ec71527866a6ec6bc5cc78f7212d39de370e0bf6
5
5
  SHA512:
6
- metadata.gz: e112e33ff471fc67b9c504e515095386fbe152b2f06dc000360d23debed1dced0d418bc519e0eaa1ffe3362792ec8f9a36aa4c214503bde00286a989f1f79ba4
7
- data.tar.gz: ece124fdc9760a006623e7680f69ffad65f6f6b9f8202011ac7557e03e52b1d7e31942a0becfa2166a40eb3d52bfd947094886497b247a72509ea79b970aec2f
6
+ metadata.gz: c31d0c5e28ae6f3040b664b3b6b9ef0e757d17535105819164a2466f0d6128065b5b54be719b8cb44550e67bd1e8b7a684df8f2289e8f335c865971717028f31
7
+ data.tar.gz: 8e825b4c53958f9f22cf6f586dc2aeabb692a8fca0bb7e8127c23801749691d914d13b7315ba6fd86d6fcb8b29e87af26d6893f92c57d634454f00fc319a29fc
data/README.md CHANGED
@@ -74,6 +74,7 @@ Or install it yourself as:
74
74
  |[AttrEnumerable.reduce_attr](#attrenumerablereduce_attr) |define reduce_xxx. it returns Class attributes(collection)'s Array reduce each value |
75
75
  |[AttrEnumerable.sample_attr](#attrenumerablesample_attr) |define sample_xxx. it returns Class attributes(collection)'s Array sample value |
76
76
  |[AttrEnumerable.select_attr](#attrenumerableselect_attr) |define select_xxx. it returns Class attributes(collection)'s Array select value |
77
+ |[AttrEnumerable.shuffle_attr](#attrenumerableshuffle_attr) |define shuffle_xxx. it returns Class attributes(collection)'s Array shuffle value |
77
78
  |[AttributesHashable.to_hash](#attributeshashableto_hash) |define to_hash method for get instance_values |
78
79
  |[AttributesInitializable::ClassMethods.attr_accessor_init](#attributesinitializableclassmethodsattr_accessor_init) |generate attr_accessor + initializer |
79
80
  |[AttributesInitializable::ClassMethods.attr_reader_init](#attributesinitializableclassmethodsattr_reader_init) |generate attr_reader + initializer |
@@ -1563,6 +1564,36 @@ print persons.select_age { |v|v == 20 } # => [20 ,20]
1563
1564
 
1564
1565
  [back to list](#list)
1565
1566
 
1567
+ ### AttrEnumerable.shuffle_attr
1568
+ ~~~ruby
1569
+ require 'attr_enumerable'
1570
+
1571
+ class Person
1572
+ attr_reader :name, :age
1573
+ def initialize(name, age)
1574
+ @name, @age = name, age
1575
+ end
1576
+ end
1577
+
1578
+ class Persons
1579
+ attr_reader :persons
1580
+ include AttrEnumerable
1581
+ def initialize(persons = [])
1582
+ @persons = persons
1583
+ end
1584
+
1585
+ def <<(person)
1586
+ @persons << person
1587
+ end
1588
+ end
1589
+
1590
+ persons = Persons.new([Person.new("tanaka", 84), Person.new("suzuki", 103)])
1591
+ print persons.shuffle_name # => ['tanaka', 'suzuki'] or['suzuki', 'tanaka']
1592
+ print persons.shuffle_age # => [84, 103] or [103, 84]
1593
+ ~~~
1594
+
1595
+ [back to list](#list)
1596
+
1566
1597
  ### AttributesHashable.to_hash
1567
1598
  ~~~ruby
1568
1599
  require 'attributes_initializable'
@@ -4265,6 +4296,7 @@ if you are Sublime Text2 user, you can use snippet for TbpgrUtils.
4265
4296
  https://github.com/tbpgr/tbpgr_utils_snippets
4266
4297
 
4267
4298
  ## History
4299
+ * version 0.0.149 : add AttrEnumerable.shuffle_attr
4268
4300
  * version 0.0.148 : add AttrEnumerable.select_attr
4269
4301
  * version 0.0.147 : add AttrEnumerable.sample_attr
4270
4302
  * version 0.0.146 : add AttrEnumerable.reduce_attr
@@ -17,7 +17,8 @@ module AttrEnumerable
17
17
  { regexp: /^map_(.*)$/, call_method: :map_attr },
18
18
  { regexp: /^reduce_(.*)$/, call_method: :reduce_attr },
19
19
  { regexp: /^sample_(.*)$/, call_method: :sample_attr },
20
- { regexp: /^select_(.*)$/, call_method: :select_attr }
20
+ { regexp: /^select_(.*)$/, call_method: :select_attr },
21
+ { regexp: /^shuffle_(.*)$/, call_method: :shuffle_attr }
21
22
  ]
22
23
 
23
24
  # call attr enumerable method.
@@ -0,0 +1,14 @@
1
+ # encoding: utf-8
2
+ require 'attr_enumerable/attr_enumerable_helper'
3
+
4
+ # AttrEnumerable
5
+ module AttrEnumerable
6
+ private
7
+ def shuffle_attr(attribute, method_name, *args, &block)
8
+ col = collection
9
+ count = args.size == 0 ? 1 : Integer(args.first)
10
+ fail ArgumentError, "invalid attribute #{attribute}" unless include_attr?(col.first, attribute)
11
+ attrs = col.map { |e|e.send(attribute) }
12
+ attrs.shuffle
13
+ end
14
+ end
@@ -2,5 +2,5 @@
2
2
 
3
3
  # Tbpgr Utilities
4
4
  module TbpgrUtils
5
- VERSION = '0.0.148'
5
+ VERSION = '0.0.149'
6
6
  end
@@ -0,0 +1,83 @@
1
+ # encoding: utf-8
2
+ require 'spec_helper'
3
+ require 'attr_enumerable'
4
+
5
+ describe AttrEnumerable do
6
+ context :shuffle_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: :shuffle_name,
38
+ expected_array: ['tanaka', 'tanaka', 'suzuki']
39
+ },
40
+ {
41
+ case_no: 2,
42
+ case_title: 'age case',
43
+ klass: AttrEnumerablePersons.new(
44
+ [
45
+ AttrEnumerablePerson.new('tanaka', 84),
46
+ AttrEnumerablePerson.new('tanaka', 20),
47
+ AttrEnumerablePerson.new('suzuki', 20)
48
+ ]
49
+ ),
50
+ method: :shuffle_age,
51
+ expected_array: [84, 20, 20]
52
+ },
53
+ ]
54
+
55
+ cases.each do |c|
56
+ it "|case_no=#{c[:case_no]}|case_title=#{c[:case_title]}" do
57
+ begin
58
+ case_before c
59
+
60
+ # -- given --
61
+ # nothing
62
+ ary = c[:klass]
63
+
64
+ # -- when --
65
+ actual = ary.send(c[:method])
66
+
67
+ # -- then --
68
+ expect(actual).to match_array(c[:expected_array])
69
+ ensure
70
+ case_after c
71
+ end
72
+ end
73
+
74
+ def case_before(c)
75
+ # implement each case before
76
+ end
77
+
78
+ def case_after(c)
79
+ # implement each case after
80
+ end
81
+ end
82
+ end
83
+ 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.148
4
+ version: 0.0.149
5
5
  platform: ruby
6
6
  authors:
7
7
  - tbpgr
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-06-05 00:00:00.000000000 Z
11
+ date: 2014-06-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -112,6 +112,7 @@ files:
112
112
  - lib/attr_enumerable/reverse_attr.rb
113
113
  - lib/attr_enumerable/sample_attr.rb
114
114
  - lib/attr_enumerable/select_attr.rb
115
+ - lib/attr_enumerable/shuffle_attr.rb
115
116
  - lib/attributes_hashable.rb
116
117
  - lib/attributes_initializable.rb
117
118
  - lib/end_erb.rb
@@ -282,6 +283,7 @@ files:
282
283
  - spec/attr_enumerable/reverse_attr_spec.rb
283
284
  - spec/attr_enumerable/sample_attr_spec.rb
284
285
  - spec/attr_enumerable/select_attr_spec.rb
286
+ - spec/attr_enumerable/shuffle_attr_spec.rb
285
287
  - spec/attributes_hashable_spec.rb
286
288
  - spec/attributes_initializable_spec.rb
287
289
  - spec/eval_helper/attr_accessor_init_code_spec.rb
@@ -459,6 +461,7 @@ test_files:
459
461
  - spec/attr_enumerable/reverse_attr_spec.rb
460
462
  - spec/attr_enumerable/sample_attr_spec.rb
461
463
  - spec/attr_enumerable/select_attr_spec.rb
464
+ - spec/attr_enumerable/shuffle_attr_spec.rb
462
465
  - spec/attributes_hashable_spec.rb
463
466
  - spec/attributes_initializable_spec.rb
464
467
  - spec/eval_helper/attr_accessor_init_code_spec.rb