tbpgr_utils 0.0.3 → 0.0.4
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.
- data/Gemfile +1 -1
- data/README.md +47 -0
- data/lib/attributes_initializable.rb +36 -0
- data/lib/tbpgr_utils/version.rb +2 -2
- data/spec/attributes_initializable_spec.rb +80 -0
- data/tbpgr_utils.gemspec +2 -0
- metadata +22 -8
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -25,6 +25,7 @@ Or install it yourself as:
|
|
25
25
|
|Object#boolean?|data type check for boolean|
|
26
26
|
|Object#my_methods|return public/protected/private self define methods|
|
27
27
|
|String#justify_table|justify pipe format table string|
|
28
|
+
|AttributesInitializable::ClassMethods.attr_accessor_init|generate attr_accessors + initializer|
|
28
29
|
|
29
30
|
### Array#together
|
30
31
|
~~~ruby
|
@@ -100,7 +101,53 @@ output
|
|
100
101
|
|test |tester|aaaaaaaaaaaaaaaaaaaaaaatestest|
|
101
102
|
~~~
|
102
103
|
|
104
|
+
### AttributesInitializable::ClassMethods.attr_accessor_init
|
105
|
+
~~~ruby
|
106
|
+
require 'attributes_initializable'
|
107
|
+
|
108
|
+
class AccessorSample
|
109
|
+
include AttributesInitializable
|
110
|
+
attr_accessor_init :atr1, :atr2
|
111
|
+
end
|
112
|
+
|
113
|
+
atr_sample1 = AccessorSample.new :atr1 => 'atr1', :atr2 => 'atr2'
|
114
|
+
p atr_sample1.atr1 # => atr1
|
115
|
+
p atr_sample1.atr2 # => atr2
|
116
|
+
|
117
|
+
atr_sample2 = AccessorSample.new do |a|
|
118
|
+
a.atr1 = 'atr1'
|
119
|
+
a.atr2 = 'atr2'
|
120
|
+
end
|
121
|
+
p atr_sample2.atr1 # => atr1
|
122
|
+
p atr_sample2.atr2 # => atr2
|
123
|
+
~~~
|
124
|
+
|
125
|
+
same mean code is
|
126
|
+
~~~ruby
|
127
|
+
class AccessorSample
|
128
|
+
attr_accessor :atr1, :atr2
|
129
|
+
|
130
|
+
def initialize(values = nil, &block)
|
131
|
+
return yield self if block
|
132
|
+
@atr1 = values[:atr1]
|
133
|
+
@atr2 = values[:atr2]
|
134
|
+
end
|
135
|
+
end
|
136
|
+
|
137
|
+
atr_sample1 = AccessorSample.new :atr1 => 'atr1', :atr2 => 'atr2'
|
138
|
+
p atr_sample1.atr1 # => atr1
|
139
|
+
p atr_sample1.atr2 # => atr2
|
140
|
+
|
141
|
+
atr_sample2 = AccessorSample.new do |a|
|
142
|
+
a.atr1 = 'atr1'
|
143
|
+
a.atr2 = 'atr2'
|
144
|
+
end
|
145
|
+
p atr_sample2.atr1 # => atr1
|
146
|
+
p atr_sample2.atr2 # => atr2
|
147
|
+
~~~
|
148
|
+
|
103
149
|
## History
|
150
|
+
* version 0.0.4 : AttributesInitializable::ClassMethods.attr_accessor_init
|
104
151
|
* version 0.0.3 : add Object#any_of?
|
105
152
|
* version 0.0.2 : loop all arrays by block.
|
106
153
|
* version 0.0.1 : first release.
|
@@ -0,0 +1,36 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require 'active_support'
|
3
|
+
|
4
|
+
# attr_accessor + initialize
|
5
|
+
module AttributesInitializable
|
6
|
+
extend ActiveSupport::Concern
|
7
|
+
|
8
|
+
module ClassMethods
|
9
|
+
# generate attr_accessor + initialize
|
10
|
+
# params
|
11
|
+
# - symbols : attribute names
|
12
|
+
def attr_accessor_init(*symbols)
|
13
|
+
generate_accessors(symbols)
|
14
|
+
generate_initializer(symbols)
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
def generate_accessors(symbols)
|
20
|
+
accessors = symbols.reduce([]) do |accessors, sym|
|
21
|
+
fail TypeError, "invalid type #{sym.class}. you have to use Symbol" unless sym.class == Symbol
|
22
|
+
accessors << ":#{sym.to_s}"
|
23
|
+
end
|
24
|
+
class_eval "attr_accessor #{accessors.join(',')}"
|
25
|
+
end
|
26
|
+
|
27
|
+
def generate_initializer(symbols)
|
28
|
+
instance_eval do
|
29
|
+
define_method :initialize do |values = nil, &block|
|
30
|
+
return block.call self if block
|
31
|
+
symbols.each {|symbol|self.method("#{symbol.to_s}=").call values[symbol]}
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
data/lib/tbpgr_utils/version.rb
CHANGED
@@ -0,0 +1,80 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require "spec_helper"
|
3
|
+
require "attributes_initializable"
|
4
|
+
|
5
|
+
describe AttributesInitializable do
|
6
|
+
context :attr_accessor_init do
|
7
|
+
class AccessorSample
|
8
|
+
include AttributesInitializable
|
9
|
+
attr_accessor_init :atr1, :atr2
|
10
|
+
end
|
11
|
+
|
12
|
+
cases = [
|
13
|
+
{
|
14
|
+
case_no: 1,
|
15
|
+
case_title: "not block case",
|
16
|
+
klass: AccessorSample,
|
17
|
+
inputs: {
|
18
|
+
atr1: 'atr1',
|
19
|
+
atr2: 'atr2',
|
20
|
+
},
|
21
|
+
block: false,
|
22
|
+
expected: {
|
23
|
+
atr1: 'atr1',
|
24
|
+
atr2: 'atr2',
|
25
|
+
},
|
26
|
+
},
|
27
|
+
{
|
28
|
+
case_no: 2,
|
29
|
+
case_title: "block case",
|
30
|
+
klass: AccessorSample,
|
31
|
+
inputs: {
|
32
|
+
atr1: 'atr1',
|
33
|
+
atr2: 'atr2',
|
34
|
+
},
|
35
|
+
block: true,
|
36
|
+
expected: {
|
37
|
+
atr1: 'atr1',
|
38
|
+
atr2: 'atr2',
|
39
|
+
},
|
40
|
+
},
|
41
|
+
]
|
42
|
+
|
43
|
+
cases.each do |c|
|
44
|
+
it "|case_no=#{c[:case_no]}|case_title=#{c[:case_title]}" do
|
45
|
+
begin
|
46
|
+
case_before c
|
47
|
+
|
48
|
+
# -- given --
|
49
|
+
# nothing
|
50
|
+
|
51
|
+
# -- when --
|
52
|
+
accessor_sample = nil
|
53
|
+
if c[:block]
|
54
|
+
accessor_sample = c[:klass].new do |a|
|
55
|
+
a.atr1 = c[:inputs][:atr1]
|
56
|
+
a.atr2 = c[:inputs][:atr2]
|
57
|
+
end
|
58
|
+
else
|
59
|
+
accessor_sample = c[:klass].new :atr1 => 'atr1', :atr2 => 'atr2'
|
60
|
+
end
|
61
|
+
|
62
|
+
# -- then --
|
63
|
+
c[:expected].each do |key, value|
|
64
|
+
expect(accessor_sample.method(key).call).to eq(value)
|
65
|
+
end
|
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
|
data/tbpgr_utils.gemspec
CHANGED
@@ -18,6 +18,8 @@ Gem::Specification.new do |spec|
|
|
18
18
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
19
|
spec.require_paths = ["lib"]
|
20
20
|
|
21
|
+
spec.add_runtime_dependency "activesupport", "~> 4.0.1"
|
22
|
+
|
21
23
|
spec.add_development_dependency "bundler", "~> 1.3"
|
22
24
|
spec.add_development_dependency "rake"
|
23
25
|
spec.add_development_dependency "rspec", "~> 2.14.1"
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
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.4
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,22 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2014-01-
|
12
|
+
date: 2014-01-08 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: activesupport
|
16
|
+
requirement: &28935036 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 4.0.1
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *28935036
|
14
25
|
- !ruby/object:Gem::Dependency
|
15
26
|
name: bundler
|
16
|
-
requirement: &
|
27
|
+
requirement: &28934616 !ruby/object:Gem::Requirement
|
17
28
|
none: false
|
18
29
|
requirements:
|
19
30
|
- - ~>
|
@@ -21,10 +32,10 @@ dependencies:
|
|
21
32
|
version: '1.3'
|
22
33
|
type: :development
|
23
34
|
prerelease: false
|
24
|
-
version_requirements: *
|
35
|
+
version_requirements: *28934616
|
25
36
|
- !ruby/object:Gem::Dependency
|
26
37
|
name: rake
|
27
|
-
requirement: &
|
38
|
+
requirement: &28934316 !ruby/object:Gem::Requirement
|
28
39
|
none: false
|
29
40
|
requirements:
|
30
41
|
- - ! '>='
|
@@ -32,10 +43,10 @@ dependencies:
|
|
32
43
|
version: '0'
|
33
44
|
type: :development
|
34
45
|
prerelease: false
|
35
|
-
version_requirements: *
|
46
|
+
version_requirements: *28934316
|
36
47
|
- !ruby/object:Gem::Dependency
|
37
48
|
name: rspec
|
38
|
-
requirement: &
|
49
|
+
requirement: &28933788 !ruby/object:Gem::Requirement
|
39
50
|
none: false
|
40
51
|
requirements:
|
41
52
|
- - ~>
|
@@ -43,7 +54,7 @@ dependencies:
|
|
43
54
|
version: 2.14.1
|
44
55
|
type: :development
|
45
56
|
prerelease: false
|
46
|
-
version_requirements: *
|
57
|
+
version_requirements: *28933788
|
47
58
|
description: Utilities
|
48
59
|
email:
|
49
60
|
- tbpgr@tbpgr.jp
|
@@ -57,12 +68,14 @@ files:
|
|
57
68
|
- LICENSE.txt
|
58
69
|
- README.md
|
59
70
|
- Rakefile
|
71
|
+
- lib/attributes_initializable.rb
|
60
72
|
- lib/open_classes/array.rb
|
61
73
|
- lib/open_classes/object.rb
|
62
74
|
- lib/open_classes/string.rb
|
63
75
|
- lib/tbpgr_utils.rb
|
64
76
|
- lib/tbpgr_utils/version.rb
|
65
77
|
- spec/array_spec.rb
|
78
|
+
- spec/attributes_initializable_spec.rb
|
66
79
|
- spec/object_spec.rb
|
67
80
|
- spec/spec_helper.rb
|
68
81
|
- spec/string_spec.rb
|
@@ -94,6 +107,7 @@ specification_version: 3
|
|
94
107
|
summary: Utilities
|
95
108
|
test_files:
|
96
109
|
- spec/array_spec.rb
|
110
|
+
- spec/attributes_initializable_spec.rb
|
97
111
|
- spec/object_spec.rb
|
98
112
|
- spec/spec_helper.rb
|
99
113
|
- spec/string_spec.rb
|