tbpgr_utils 0.0.141 → 0.0.142

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: 117dbd6a2063545d0db523fe4f88926b0a08501a
4
- data.tar.gz: aff26d7cd373acc6057c714471d5a179e430658a
3
+ metadata.gz: 3f02aa3b5339ad6969dba481978c51fb0c0efb15
4
+ data.tar.gz: 7f092ea5e5f8bdf00f2ae2423fc7a330191bcc8b
5
5
  SHA512:
6
- metadata.gz: 836802fb1e8b97e07928ca03ce7decf3a5fe3fd1a32f3fd6eab089d21ec9df9107e8d55a58abee1d019c3671b07ee7be1963ee6b3c448e88cc81f171e97623b6
7
- data.tar.gz: c9a334cf30567cfa2af433f0567df99e5669f6696e026cc3b8565572ac0cb6b948e119c6d15609e3b5cdd353ad6ffbbd7ef682e626382104ecd90edc4d0e6162
6
+ metadata.gz: 86ccbf864373a8a8f4568cfcf9c5846e0e9b36bbaf19a59fd85cee57b0d6abb617e8c635fe7faaef7d90f380f3513acfe2618d0b123852113b5e070dc18d286d
7
+ data.tar.gz: 477723d8c0d8894e41e7e4a013c325a619c9aa1ccb32a05ca3134c55a9b0ea9a30d685df2789db9a5e81f4887cf744971aef92c0ad9a172a9629879f004fcf73
data/README.md CHANGED
@@ -64,6 +64,7 @@ Or install it yourself as:
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
66
  |[AttrEnumerable.delete_attr](#attrenumerabledelete_attr) |define delete_xxx. it delete Class attributes(collection) that match delete condition |
67
+ |[AttrEnumerable.first_attr](#attrenumerablefirst_attr) |define first_xxx. it returns Class attributes(collection) first N elementthat |
67
68
  |[AttrEnumerable.each_attr](#attrenumerableeach_attr) |define each_xxx. it call Class attributes(collection)'s attribute iterator |
68
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 |
69
70
  |[AttrEnumerable.reverse_attr](#attrenumerablereverse_attr) |define reverse_xxx. it returns Class attributes(collection)'s reverse Array |
@@ -1304,6 +1305,40 @@ end
1304
1305
 
1305
1306
  [back to list](#list)
1306
1307
 
1308
+ ### AttrEnumerable.first_attr
1309
+ ~~~ruby
1310
+ require 'attr_enumerable'
1311
+
1312
+ class Person
1313
+ attr_reader :name, :age
1314
+ def initialize(name, age)
1315
+ @name, @age = name, age
1316
+ end
1317
+ end
1318
+
1319
+ class Persons
1320
+ attr_reader :persons
1321
+ include AttrEnumerable
1322
+ def initialize(persons = [])
1323
+ @persons = persons
1324
+ end
1325
+
1326
+ def <<(person)
1327
+ @persons << person
1328
+ end
1329
+ end
1330
+
1331
+ persons = Persons.new([Person.new("tanaka", 84), Person.new("tanaka", 20), Person.new("suzuki", 20)])
1332
+ print persons.first_name # => 'tanaka'
1333
+ print persons.first_name(2) # => ['tanaka', 'tanaka']
1334
+ print persons.first_age(4) # => [84, 20, 20]
1335
+
1336
+ persons = Persons.new([])
1337
+ print persons.first_age(4) # => []
1338
+ ~~~
1339
+
1340
+ [back to list](#list)
1341
+
1307
1342
  ### AttrEnumerable.reverse_attr
1308
1343
  ~~~ruby
1309
1344
  require 'attr_enumerable'
@@ -4037,6 +4072,7 @@ if you are Sublime Text2 user, you can use snippet for TbpgrUtils.
4037
4072
  https://github.com/tbpgr/tbpgr_utils_snippets
4038
4073
 
4039
4074
  ## History
4075
+ * version 0.0.142 : add AttrEnumerable.first_attr
4040
4076
  * version 0.0.141 : add AttrEnumerable.delete_attr
4041
4077
  * version 0.0.140 : add AttrEnumerable.concat_attr
4042
4078
  * version 0.0.139 : add AttrEnumerable.compact_attr
@@ -1,39 +1,44 @@
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
1
+ # encoding: utf-8
2
+ require 'active_support/inflector'
3
+
4
+ # AttrEnumerable
5
+ module AttrEnumerable
6
+ ATTR_METHODS = [
7
+ { regexp: /^each_(.*)_with_index$/, call_method: :each_attr_with_index },
8
+ { regexp: /^each_(.*)$/, call_method: :each_attr },
9
+ { regexp: /^reverse_(.*)$/, call_method: :reverse_attr },
10
+ { regexp: /^at_(.*)$/, call_method: :at_attr },
11
+ { regexp: /^compact_(.*)$/, call_method: :compact_attr },
12
+ { regexp: /^concat_(.*)$/, call_method: :concat_attr },
13
+ { regexp: /^delete_(.*)$/, call_method: :delete_attr },
14
+ { regexp: /^first_(.*)$/, call_method: :first_attr }
15
+ ]
16
+
17
+ # call attr enumerable method.
18
+ def method_missing(method_name, *args, &block)
19
+ target_method = detect(method_name)
20
+ send(target_method[:call_method], target_method[:attribute], method_name, *args, &block)
21
+ rescue
22
+ raise
23
+ end
24
+
25
+ private
26
+ def detect(method_name)
27
+ ATTR_METHODS.each do |target_method|
28
+ regexp = target_method[:regexp]
29
+ if method_name.to_s =~ regexp
30
+ attribute = method_name.to_s.scan(regexp).first.first
31
+ return { call_method: target_method[:call_method], attribute: attribute }
32
+ end
33
+ end
34
+ fail NoMethodError, "method is not exists #{method_name}"
35
+ end
36
+
37
+ def include_attr?(element, attribute)
38
+ element.instance_variables.include? :"@#{attribute}"
39
+ end
40
+
41
+ def collection
42
+ instance_variable_get("@#{self.class.name.underscore}")
43
+ end
44
+ 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 first_attr(attribute, method_name, *args, &block)
8
+ col = collection
9
+ return [] if col.empty?
10
+ first_size = args.size == 0 ? nil : Integer(args.first)
11
+ fail ArgumentError, "invalid attribute #{attribute}" unless include_attr?(col.first, attribute)
12
+ attrs = col.map { |v|v.send(attribute) }
13
+ first_size.nil? ? attrs.first : attrs.first(first_size)
14
+ end
15
+ end
@@ -1,7 +1,7 @@
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
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
@@ -2,5 +2,5 @@
2
2
 
3
3
  # Tbpgr Utilities
4
4
  module TbpgrUtils
5
- VERSION = '0.0.141'
5
+ VERSION = '0.0.142'
6
6
  end
@@ -0,0 +1,107 @@
1
+ # encoding: utf-8
2
+ require 'spec_helper'
3
+ require 'attr_enumerable'
4
+
5
+ describe AttrEnumerable do
6
+ context :first_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: :first_name,
38
+ first: nil,
39
+ expected: ['tanaka'],
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: :first_age,
52
+ first: 2,
53
+ expected: [84, 20],
54
+ },
55
+ {
56
+ case_no: 3,
57
+ case_title: 'over index 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: :first_age,
66
+ first: 4,
67
+ expected: [84, 20, 20],
68
+ },
69
+ {
70
+ case_no: 4,
71
+ case_title: 'empty case',
72
+ klass: AttrEnumerablePersons.new([]),
73
+ method: :first_name,
74
+ first: nil,
75
+ expected: [],
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 = c[:first] ? ary.send(c[:method], c[:first]) : ary.send(c[:method])
90
+
91
+ # -- then --
92
+ expect(Array(actual)).to match_array(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.141
4
+ version: 0.0.142
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-29 00:00:00.000000000 Z
11
+ date: 2014-05-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -104,6 +104,7 @@ files:
104
104
  - lib/attr_enumerable/delete_attr.rb
105
105
  - lib/attr_enumerable/each_attr.rb
106
106
  - lib/attr_enumerable/each_attr_with_index.rb
107
+ - lib/attr_enumerable/first_attr.rb
107
108
  - lib/attr_enumerable/reverse_attr.rb
108
109
  - lib/attributes_hashable.rb
109
110
  - lib/attributes_initializable.rb
@@ -267,6 +268,7 @@ files:
267
268
  - spec/attr_enumerable/delete_attr_spec.rb
268
269
  - spec/attr_enumerable/each_attr_spec.rb
269
270
  - spec/attr_enumerable/each_attr_with_index_spec.rb
271
+ - spec/attr_enumerable/first_attr_spec.rb
270
272
  - spec/attr_enumerable/reverse_attr_spec.rb
271
273
  - spec/attributes_hashable_spec.rb
272
274
  - spec/attributes_initializable_spec.rb
@@ -437,6 +439,7 @@ test_files:
437
439
  - spec/attr_enumerable/delete_attr_spec.rb
438
440
  - spec/attr_enumerable/each_attr_spec.rb
439
441
  - spec/attr_enumerable/each_attr_with_index_spec.rb
442
+ - spec/attr_enumerable/first_attr_spec.rb
440
443
  - spec/attr_enumerable/reverse_attr_spec.rb
441
444
  - spec/attributes_hashable_spec.rb
442
445
  - spec/attributes_initializable_spec.rb