otoroshi 0.0.7 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/otoroshi.rb +4 -5
- data/lib/otoroshi/exceptions.rb +68 -52
- data/lib/otoroshi/initializer.rb +127 -0
- data/lib/otoroshi/sanctuary.rb +100 -273
- metadata +3 -20
- data/lib/doc/Example.html +0 -244
- data/lib/doc/Otoroshi.html +0 -115
- data/lib/doc/Otoroshi/Sanctuary.html +0 -223
- data/lib/doc/Otoroshi/Sanctuary/ClassMethods.html +0 -1511
- data/lib/doc/_index.html +0 -137
- data/lib/doc/class_list.html +0 -51
- data/lib/doc/css/common.css +0 -1
- data/lib/doc/css/full_list.css +0 -58
- data/lib/doc/css/style.css +0 -496
- data/lib/doc/file.README.html +0 -290
- data/lib/doc/file_list.html +0 -56
- data/lib/doc/frames.html +0 -17
- data/lib/doc/index.html +0 -290
- data/lib/doc/js/app.js +0 -314
- data/lib/doc/js/full_list.js +0 -216
- data/lib/doc/js/jquery.js +0 -4
- data/lib/doc/method_list.html +0 -155
- data/lib/doc/top-level-namespace.html +0 -110
data/lib/doc/file.README.html
DELETED
@@ -1,290 +0,0 @@
|
|
1
|
-
<!DOCTYPE html>
|
2
|
-
<html>
|
3
|
-
<head>
|
4
|
-
<meta charset="UTF-8">
|
5
|
-
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
6
|
-
<title>
|
7
|
-
File: README
|
8
|
-
|
9
|
-
— Documentation by YARD 0.9.25
|
10
|
-
|
11
|
-
</title>
|
12
|
-
|
13
|
-
<link rel="stylesheet" href="css/style.css" type="text/css" />
|
14
|
-
|
15
|
-
<link rel="stylesheet" href="css/common.css" type="text/css" />
|
16
|
-
|
17
|
-
<script type="text/javascript">
|
18
|
-
pathId = "README";
|
19
|
-
relpath = '';
|
20
|
-
</script>
|
21
|
-
|
22
|
-
|
23
|
-
<script type="text/javascript" charset="utf-8" src="js/jquery.js"></script>
|
24
|
-
|
25
|
-
<script type="text/javascript" charset="utf-8" src="js/app.js"></script>
|
26
|
-
|
27
|
-
|
28
|
-
</head>
|
29
|
-
<body>
|
30
|
-
<div class="nav_wrap">
|
31
|
-
<iframe id="nav" src="file_list.html?1"></iframe>
|
32
|
-
<div id="resizer"></div>
|
33
|
-
</div>
|
34
|
-
|
35
|
-
<div id="main" tabindex="-1">
|
36
|
-
<div id="header">
|
37
|
-
<div id="menu">
|
38
|
-
|
39
|
-
<a href="_index.html">Index</a> »
|
40
|
-
<span class="title">File: README</span>
|
41
|
-
|
42
|
-
</div>
|
43
|
-
|
44
|
-
<div id="search">
|
45
|
-
|
46
|
-
<a class="full_list_link" id="class_list_link"
|
47
|
-
href="class_list.html">
|
48
|
-
|
49
|
-
<svg width="24" height="24">
|
50
|
-
<rect x="0" y="4" width="24" height="4" rx="1" ry="1"></rect>
|
51
|
-
<rect x="0" y="12" width="24" height="4" rx="1" ry="1"></rect>
|
52
|
-
<rect x="0" y="20" width="24" height="4" rx="1" ry="1"></rect>
|
53
|
-
</svg>
|
54
|
-
</a>
|
55
|
-
|
56
|
-
</div>
|
57
|
-
<div class="clear"></div>
|
58
|
-
</div>
|
59
|
-
|
60
|
-
<div id="content"><div id='filecontents'><h1 id="otoroshi">Otoroshi</h1>
|
61
|
-
|
62
|
-
<p><img src="https://upload.wikimedia.org/wikipedia/commons/4/41/Suushi_Otoroshi.jpg" alt="otoroshi" title="Sawaki Suushi"></p>
|
63
|
-
|
64
|
-
<blockquote>
|
65
|
-
<p>-- <cite>Illustration from Sawaki Suushi, 1737</cite></p>
|
66
|
-
</blockquote>
|
67
|
-
|
68
|
-
<p>Otoroshis are legendary creatures in Japanese folklore and mythology. They act as guardian of holy temples.</p>
|
69
|
-
|
70
|
-
<p>The <code>otoroshi</code> gem helps you defining and validating class properties.</p>
|
71
|
-
|
72
|
-
<p>See <a href="#refactor-example">an example of refactor</a> with Otoroshi.</p>
|
73
|
-
|
74
|
-
<h2 id="installation">Installation</h2>
|
75
|
-
|
76
|
-
<p>Add this line to your application's Gemfile:</p>
|
77
|
-
|
78
|
-
<pre class="code ruby"><code class="ruby"><span class='id identifier rubyid_gem'>gem</span> <span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>otoroshi</span><span class='tstring_end'>'</span></span>
|
79
|
-
</code></pre>
|
80
|
-
|
81
|
-
<p>And then execute:</p>
|
82
|
-
|
83
|
-
<pre class="code ruby"><code class="ruby">$ bundle
|
84
|
-
</code></pre>
|
85
|
-
|
86
|
-
<h2 id="usage">Usage</h2>
|
87
|
-
|
88
|
-
<p>Include <code>Otoroshi::Sanctuary</code> in a class to easily define arguments validation.</p>
|
89
|
-
|
90
|
-
<pre class="code ruby"><code class="ruby"><span class='kw'>class</span> <span class='const'>MyClass</span>
|
91
|
-
<span class='id identifier rubyid_include'>include</span> <span class='const'><span class='object_link'><a href="Otoroshi.html" title="Otoroshi (module)">Otoroshi</a></span></span><span class='op'>::</span><span class='const'><span class='object_link'><a href="Otoroshi/Sanctuary.html" title="Otoroshi::Sanctuary (module)">Sanctuary</a></span></span>
|
92
|
-
<span class='kw'>end</span>
|
93
|
-
</code></pre>
|
94
|
-
|
95
|
-
<h3 id="define-classs-properties">Define class's properties</h3>
|
96
|
-
|
97
|
-
<p>Use the <code>::property(name, type, options)</code> method to add a property.</p>
|
98
|
-
|
99
|
-
<ul>
|
100
|
-
<li><code>name</code>: name of the property (symbol or string)</li>
|
101
|
-
<li><code>type</code>: the class the future value should belongs to (class or array of classes, <code>Object</code> by default)</li>
|
102
|
-
<li>options:
|
103
|
-
|
104
|
-
<ul>
|
105
|
-
<li><code>validate</code>: a custom validation to apply to the future value (lambda, <code>->(_) { true }</code> by default)</li>
|
106
|
-
<li><code>allow_nil</code>: define if the future value can be set to nil (boolean, <code>false</code> by default)</li>
|
107
|
-
<li><code>default</code>: the default value for this property (should match the required type, <code>nil</code> by default)</li>
|
108
|
-
</ul></li>
|
109
|
-
</ul>
|
110
|
-
|
111
|
-
<p>Getters and a setters are automatically set:</p>
|
112
|
-
|
113
|
-
<pre class="code ruby"><code class="ruby"><span class='kw'>class</span> <span class='const'>Example</span>
|
114
|
-
<span class='id identifier rubyid_include'>include</span> <span class='const'><span class='object_link'><a href="Otoroshi.html" title="Otoroshi (module)">Otoroshi</a></span></span><span class='op'>::</span><span class='const'><span class='object_link'><a href="Otoroshi/Sanctuary.html" title="Otoroshi::Sanctuary (module)">Sanctuary</a></span></span>
|
115
|
-
|
116
|
-
<span class='id identifier rubyid_property'>property</span> <span class='symbol'>:foo</span><span class='comma'>,</span> <span class='const'>Integer</span>
|
117
|
-
<span class='id identifier rubyid_property'>property</span> <span class='symbol'>:bar</span><span class='comma'>,</span> <span class='const'>String</span>
|
118
|
-
<span class='kw'>end</span>
|
119
|
-
|
120
|
-
<span class='id identifier rubyid_instance'>instance</span> <span class='op'>=</span> <span class='const'>Example</span><span class='period'>.</span><span class='id identifier rubyid_new'>new</span><span class='lparen'>(</span><span class='label'>foo:</span> <span class='int'>42</span><span class='comma'>,</span> <span class='label'>bar:</span> <span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>hello</span><span class='tstring_end'>'</span></span><span class='rparen'>)</span>
|
121
|
-
|
122
|
-
<span class='id identifier rubyid_instance'>instance</span><span class='period'>.</span><span class='id identifier rubyid_foo'>foo</span> <span class='comment'># 42
|
123
|
-
</span><span class='id identifier rubyid_instance'>instance</span><span class='period'>.</span><span class='id identifier rubyid_bar'>bar</span> <span class='comment'># hello
|
124
|
-
</span>
|
125
|
-
<span class='id identifier rubyid_instance'>instance</span><span class='period'>.</span><span class='id identifier rubyid_foo'>foo</span> <span class='op'>=</span> <span class='int'>7</span>
|
126
|
-
<span class='id identifier rubyid_instance'>instance</span><span class='period'>.</span><span class='id identifier rubyid_bar'>bar</span> <span class='op'>=</span> <span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>world</span><span class='tstring_end'>'</span></span>
|
127
|
-
|
128
|
-
<span class='id identifier rubyid_instance'>instance</span><span class='period'>.</span><span class='id identifier rubyid_foo'>foo</span> <span class='comment'># 7
|
129
|
-
</span><span class='id identifier rubyid_instance'>instance</span><span class='period'>.</span><span class='id identifier rubyid_bar'>bar</span> <span class='comment'># world
|
130
|
-
</span></code></pre>
|
131
|
-
|
132
|
-
<p>Type validations run on initialization and assignment:</p>
|
133
|
-
|
134
|
-
<pre class="code ruby"><code class="ruby"><span class='kw'>class</span> <span class='const'>Example</span>
|
135
|
-
<span class='id identifier rubyid_include'>include</span> <span class='const'><span class='object_link'><a href="Otoroshi.html" title="Otoroshi (module)">Otoroshi</a></span></span><span class='op'>::</span><span class='const'><span class='object_link'><a href="Otoroshi/Sanctuary.html" title="Otoroshi::Sanctuary (module)">Sanctuary</a></span></span>
|
136
|
-
|
137
|
-
<span class='id identifier rubyid_property'>property</span> <span class='symbol'>:foo</span><span class='comma'>,</span> <span class='const'>Integer</span>
|
138
|
-
<span class='kw'>end</span>
|
139
|
-
|
140
|
-
<span class='const'>Example</span><span class='period'>.</span><span class='id identifier rubyid_new'>new</span> <span class='comment'># => ArgumentError, "foo does not match required type"
|
141
|
-
</span><span class='const'>Example</span><span class='period'>.</span><span class='id identifier rubyid_new'>new</span><span class='lparen'>(</span><span class='label'>foo:</span> <span class='float'>1.5</span><span class='rparen'>)</span> <span class='comment'># => ArgumentError, "foo does not match required type"
|
142
|
-
</span>
|
143
|
-
<span class='id identifier rubyid_instance'>instance</span><span class='period'>.</span><span class='id identifier rubyid_foo'>foo</span> <span class='op'>=</span> <span class='kw'>nil</span> <span class='comment'># => ArgumentError, "foo does not match required type"
|
144
|
-
</span><span class='id identifier rubyid_instance'>instance</span><span class='period'>.</span><span class='id identifier rubyid_foo'>foo</span> <span class='op'>=</span> <span class='float'>1.5</span> <span class='comment'># => ArgumentError, "foo does not match required type"
|
145
|
-
</span></code></pre>
|
146
|
-
|
147
|
-
<p>You can provide multiple authorized types:</p>
|
148
|
-
|
149
|
-
<pre class="code ruby"><code class="ruby"><span class='kw'>class</span> <span class='const'>Example</span>
|
150
|
-
<span class='id identifier rubyid_include'>include</span> <span class='const'><span class='object_link'><a href="Otoroshi.html" title="Otoroshi (module)">Otoroshi</a></span></span><span class='op'>::</span><span class='const'><span class='object_link'><a href="Otoroshi/Sanctuary.html" title="Otoroshi::Sanctuary (module)">Sanctuary</a></span></span>
|
151
|
-
|
152
|
-
<span class='id identifier rubyid_property'>property</span> <span class='symbol'>:foo</span><span class='comma'>,</span> <span class='lbracket'>[</span><span class='const'>Symbol</span><span class='comma'>,</span> <span class='const'>String</span><span class='rbracket'>]</span>
|
153
|
-
<span class='id identifier rubyid_property'>property</span> <span class='symbol'>:bar</span><span class='comma'>,</span> <span class='lbracket'>[</span><span class='const'>TrueClass</span><span class='comma'>,</span> <span class='const'>FalseClass</span><span class='rbracket'>]</span>
|
154
|
-
<span class='kw'>end</span>
|
155
|
-
|
156
|
-
<span class='const'>Example</span><span class='period'>.</span><span class='id identifier rubyid_new'>new</span><span class='lparen'>(</span><span class='label'>foo:</span> <span class='symbol'>:hello</span><span class='comma'>,</span> <span class='label'>bar:</span> <span class='kw'>true</span><span class='rparen'>)</span>
|
157
|
-
<span class='const'>Example</span><span class='period'>.</span><span class='id identifier rubyid_new'>new</span><span class='lparen'>(</span><span class='label'>foo:</span> <span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>hello</span><span class='tstring_end'>'</span></span><span class='comma'>,</span> <span class='label'>bar:</span> <span class='kw'>false</span><span class='rparen'>)</span>
|
158
|
-
</code></pre>
|
159
|
-
|
160
|
-
<p>You can avoid the second argument so any <code>Object</code> can be passed:</p>
|
161
|
-
|
162
|
-
<pre class="code ruby"><code class="ruby"><span class='kw'>class</span> <span class='const'>Example</span>
|
163
|
-
<span class='id identifier rubyid_include'>include</span> <span class='const'><span class='object_link'><a href="Otoroshi.html" title="Otoroshi (module)">Otoroshi</a></span></span><span class='op'>::</span><span class='const'><span class='object_link'><a href="Otoroshi/Sanctuary.html" title="Otoroshi::Sanctuary (module)">Sanctuary</a></span></span>
|
164
|
-
|
165
|
-
<span class='id identifier rubyid_property'>property</span> <span class='symbol'>:foo</span>
|
166
|
-
<span class='kw'>end</span>
|
167
|
-
|
168
|
-
<span class='const'>Example</span><span class='period'>.</span><span class='id identifier rubyid_new'>new</span><span class='lparen'>(</span><span class='label'>foo:</span> <span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>hello</span><span class='tstring_end'>'</span></span><span class='rparen'>)</span>
|
169
|
-
<span class='const'>Example</span><span class='period'>.</span><span class='id identifier rubyid_new'>new</span><span class='lparen'>(</span><span class='label'>foo:</span> <span class='int'>42</span><span class='rparen'>)</span>
|
170
|
-
<span class='const'>Example</span><span class='period'>.</span><span class='id identifier rubyid_new'>new</span><span class='lparen'>(</span><span class='label'>foo:</span> <span class='const'>User</span><span class='period'>.</span><span class='id identifier rubyid_find'>find</span><span class='lparen'>(</span><span class='int'>1</span><span class='rparen'>)</span><span class='rparen'>)</span>
|
171
|
-
</code></pre>
|
172
|
-
|
173
|
-
<p>You can add custom validations with the <code>validate:</code> option.</p>
|
174
|
-
|
175
|
-
<pre class="code ruby"><code class="ruby"><span class='kw'>class</span> <span class='const'>Example</span>
|
176
|
-
<span class='id identifier rubyid_include'>include</span> <span class='const'><span class='object_link'><a href="Otoroshi.html" title="Otoroshi (module)">Otoroshi</a></span></span><span class='op'>::</span><span class='const'><span class='object_link'><a href="Otoroshi/Sanctuary.html" title="Otoroshi::Sanctuary (module)">Sanctuary</a></span></span>
|
177
|
-
|
178
|
-
<span class='id identifier rubyid_property'>property</span> <span class='symbol'>:foo</span><span class='comma'>,</span> <span class='const'>Integer</span><span class='comma'>,</span> <span class='tlambda'>-></span><span class='lparen'>(</span><span class='id identifier rubyid_v'>v</span><span class='rparen'>)</span> <span class='tlambeg'>{</span> <span class='id identifier rubyid_v'>v</span> <span class='op'>></span> <span class='int'>0</span> <span class='rbrace'>}</span>
|
179
|
-
<span class='kw'>end</span>
|
180
|
-
|
181
|
-
<span class='const'>Example</span><span class='period'>.</span><span class='id identifier rubyid_new'>new</span><span class='lparen'>(</span><span class='label'>foo:</span> <span class='op'>-</span><span class='int'>1</span><span class='rparen'>)</span> <span class='comment'># => ArgumentError, "foo does not match validation"
|
182
|
-
</span>
|
183
|
-
<span class='id identifier rubyid_instance'>instance</span><span class='period'>.</span><span class='id identifier rubyid_foo'>foo</span> <span class='op'>=</span> <span class='op'>-</span><span class='int'>1</span> <span class='comment'># => ArgumentError, "foo does not match validation"
|
184
|
-
</span></code></pre>
|
185
|
-
|
186
|
-
<p>Set <code>allow_nil:</code> option to <code>true</code> if <code>nil</code> is authorized:</p>
|
187
|
-
|
188
|
-
<pre class="code ruby"><code class="ruby"><span class='kw'>class</span> <span class='const'>Example</span>
|
189
|
-
<span class='id identifier rubyid_include'>include</span> <span class='const'><span class='object_link'><a href="Otoroshi.html" title="Otoroshi (module)">Otoroshi</a></span></span><span class='op'>::</span><span class='const'><span class='object_link'><a href="Otoroshi/Sanctuary.html" title="Otoroshi::Sanctuary (module)">Sanctuary</a></span></span>
|
190
|
-
|
191
|
-
<span class='id identifier rubyid_property'>property</span> <span class='symbol'>:foo</span><span class='comma'>,</span> <span class='const'>Integer</span><span class='comma'>,</span> <span class='tlambda'>-></span><span class='lparen'>(</span><span class='id identifier rubyid_v'>v</span><span class='rparen'>)</span> <span class='tlambeg'>{</span> <span class='id identifier rubyid_v'>v</span> <span class='op'>></span> <span class='int'>0</span> <span class='rbrace'>}</span><span class='comma'>,</span> <span class='label'>allow_nil:</span> <span class='kw'>true</span>
|
192
|
-
<span class='kw'>end</span>
|
193
|
-
|
194
|
-
<span class='id identifier rubyid_instance'>instance</span> <span class='op'>=</span> <span class='const'>Example</span><span class='period'>.</span><span class='id identifier rubyid_new'>new</span>
|
195
|
-
<span class='id identifier rubyid_instance'>instance</span><span class='period'>.</span><span class='id identifier rubyid_foo'>foo</span> <span class='comment'># nil
|
196
|
-
</span>
|
197
|
-
<span class='id identifier rubyid_instance'>instance</span><span class='period'>.</span><span class='id identifier rubyid_foo'>foo</span> <span class='op'>=</span> <span class='int'>42</span>
|
198
|
-
<span class='id identifier rubyid_instance'>instance</span><span class='period'>.</span><span class='id identifier rubyid_foo'>foo</span> <span class='op'>=</span> <span class='kw'>nil</span>
|
199
|
-
<span class='id identifier rubyid_instance'>instance</span><span class='period'>.</span><span class='id identifier rubyid_foo'>foo</span> <span class='comment'># nil
|
200
|
-
</span><span class='id identifier rubyid_instance'>instance</span><span class='period'>.</span><span class='id identifier rubyid_foo'>foo</span> <span class='op'>=</span> <span class='op'>-</span><span class='int'>1</span> <span class='comment'># => ArgumentError, "foo does not match validation"
|
201
|
-
</span></code></pre>
|
202
|
-
|
203
|
-
<p>Set <code>default:</code> option to the default value. You can always set the value to <code>nil</code> if <code>allow_nil</code> is <code>true</code>.</p>
|
204
|
-
|
205
|
-
<pre class="code ruby"><code class="ruby"><span class='kw'>class</span> <span class='const'>Example</span>
|
206
|
-
<span class='id identifier rubyid_include'>include</span> <span class='const'><span class='object_link'><a href="Otoroshi.html" title="Otoroshi (module)">Otoroshi</a></span></span><span class='op'>::</span><span class='const'><span class='object_link'><a href="Otoroshi/Sanctuary.html" title="Otoroshi::Sanctuary (module)">Sanctuary</a></span></span>
|
207
|
-
|
208
|
-
<span class='id identifier rubyid_property'>property</span> <span class='symbol'>:foo</span><span class='comma'>,</span> <span class='const'>Integer</span><span class='comma'>,</span> <span class='tlambda'>-></span><span class='lparen'>(</span><span class='id identifier rubyid_v'>v</span><span class='rparen'>)</span> <span class='tlambeg'>{</span> <span class='id identifier rubyid_v'>v</span> <span class='op'>></span> <span class='int'>0</span> <span class='rbrace'>}</span><span class='comma'>,</span> <span class='label'>default:</span> <span class='int'>1</span><span class='comma'>,</span> <span class='label'>allow_nil:</span> <span class='kw'>true</span>
|
209
|
-
<span class='kw'>end</span>
|
210
|
-
|
211
|
-
<span class='id identifier rubyid_instance'>instance</span> <span class='op'>=</span> <span class='const'>Example</span><span class='period'>.</span><span class='id identifier rubyid_new'>new</span> <span class='comment'># no error
|
212
|
-
</span><span class='id identifier rubyid_instance'>instance</span><span class='period'>.</span><span class='id identifier rubyid_foo'>foo</span> <span class='comment'># 1
|
213
|
-
</span>
|
214
|
-
<span class='id identifier rubyid_instance'>instance</span> <span class='op'>=</span> <span class='const'>Example</span><span class='period'>.</span><span class='id identifier rubyid_new'>new</span><span class='lparen'>(</span><span class='label'>foo:</span> <span class='kw'>nil</span><span class='rparen'>)</span> <span class='comment'># no error
|
215
|
-
</span><span class='id identifier rubyid_instance'>instance</span><span class='period'>.</span><span class='id identifier rubyid_foo'>foo</span> <span class='comment'># nil
|
216
|
-
</span>
|
217
|
-
<span class='id identifier rubyid_instance'>instance</span><span class='period'>.</span><span class='id identifier rubyid_foo'>foo</span> <span class='op'>=</span> <span class='kw'>nil</span> <span class='comment'># no error
|
218
|
-
</span><span class='id identifier rubyid_instance'>instance</span><span class='period'>.</span><span class='id identifier rubyid_foo'>foo</span> <span class='comment'># nil
|
219
|
-
</span></code></pre>
|
220
|
-
|
221
|
-
<h2 id="refactor-example">Refactor Example</h2>
|
222
|
-
|
223
|
-
<h3 id="before-refactor-23-lines-765-characters-dedicated-to-properties">Before refactor (23 lines / 765 characters dedicated to properties)</h3>
|
224
|
-
|
225
|
-
<pre class="code ruby"><code class="ruby"><span class='kw'>class</span> <span class='const'>Importer</span>
|
226
|
-
<span class='id identifier rubyid_attr_reader'>attr_reader</span> <span class='symbol'>:file_path</span><span class='comma'>,</span> <span class='symbol'>:headers</span><span class='comma'>,</span> <span class='symbol'>:col_sep</span><span class='comma'>,</span> <span class='symbol'>:converters</span>
|
227
|
-
|
228
|
-
<span class='kw'>def</span> <span class='id identifier rubyid_initialize'>initialize</span><span class='lparen'>(</span><span class='label'>file_path:</span><span class='comma'>,</span> <span class='label'>headers:</span> <span class='kw'>false</span><span class='comma'>,</span> <span class='label'>col_sep:</span> <span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>,</span><span class='tstring_end'>'</span></span><span class='comma'>,</span> <span class='label'>converters:</span> <span class='kw'>nil</span><span class='rparen'>)</span>
|
229
|
-
<span class='kw'>self</span><span class='period'>.</span><span class='id identifier rubyid_file_path'>file_path</span> <span class='op'>=</span> <span class='id identifier rubyid_file_path'>file_path</span>
|
230
|
-
<span class='kw'>self</span><span class='period'>.</span><span class='id identifier rubyid_headers'>headers</span> <span class='op'>=</span> <span class='id identifier rubyid_headers'>headers</span>
|
231
|
-
<span class='kw'>self</span><span class='period'>.</span><span class='id identifier rubyid_col_sep'>col_sep</span> <span class='op'>=</span> <span class='id identifier rubyid_col_sep'>col_sep</span>
|
232
|
-
<span class='kw'>self</span><span class='period'>.</span><span class='id identifier rubyid_converters'>converters</span> <span class='op'>=</span> <span class='id identifier rubyid_converters'>converters</span>
|
233
|
-
<span class='kw'>end</span>
|
234
|
-
|
235
|
-
<span class='id identifier rubyid_private'>private</span>
|
236
|
-
|
237
|
-
<span class='comment'># private business methods...
|
238
|
-
</span>
|
239
|
-
<span class='kw'>def</span> <span class='id identifier rubyid_file_path='>file_path=</span><span class='lparen'>(</span><span class='id identifier rubyid_value'>value</span><span class='rparen'>)</span>
|
240
|
-
<span class='id identifier rubyid_raise'>raise</span> <span class='const'>ArgumentError</span> <span class='kw'>unless</span> <span class='id identifier rubyid_value'>value</span><span class='period'>.</span><span class='id identifier rubyid_is_?'>is_?</span><span class='lparen'>(</span><span class='const'>String</span><span class='rparen'>)</span> <span class='op'>&&</span> <span class='id identifier rubyid_value'>value</span><span class='period'>.</span><span class='id identifier rubyid_match?'>match?</span><span class='lparen'>(</span><span class='tstring'><span class='regexp_beg'>/</span><span class='tstring_content'>.+\.csv</span><span class='regexp_end'>/</span></span><span class='rparen'>)</span>
|
241
|
-
|
242
|
-
<span class='ivar'>@file_path</span> <span class='op'>=</span> <span class='id identifier rubyid_value'>value</span>
|
243
|
-
<span class='kw'>end</span>
|
244
|
-
|
245
|
-
<span class='kw'>def</span> <span class='id identifier rubyid_headers='>headers=</span><span class='lparen'>(</span><span class='id identifier rubyid_value'>value</span><span class='rparen'>)</span>
|
246
|
-
<span class='id identifier rubyid_raise'>raise</span> <span class='const'>ArgumentError</span> <span class='kw'>unless</span> <span class='lbracket'>[</span><span class='kw'>true</span><span class='comma'>,</span> <span class='kw'>false</span><span class='rbracket'>]</span><span class='period'>.</span><span class='id identifier rubyid_include?'>include?</span><span class='lparen'>(</span><span class='id identifier rubyid_value'>value</span><span class='rparen'>)</span>
|
247
|
-
|
248
|
-
<span class='ivar'>@headers</span> <span class='op'>=</span> <span class='id identifier rubyid_value'>value</span>
|
249
|
-
<span class='kw'>end</span>
|
250
|
-
|
251
|
-
<span class='kw'>def</span> <span class='id identifier rubyid_col_sep='>col_sep=</span><span class='lparen'>(</span><span class='id identifier rubyid_value'>value</span><span class='rparen'>)</span>
|
252
|
-
<span class='id identifier rubyid_raise'>raise</span> <span class='const'>ArgumentError</span> <span class='kw'>unless</span> <span class='id identifier rubyid_value'>value</span><span class='period'>.</span><span class='id identifier rubyid_is_?'>is_?</span><span class='lparen'>(</span><span class='const'>String</span><span class='rparen'>)</span> <span class='op'>&&</span> <span class='id identifier rubyid_value'>value</span><span class='period'>.</span><span class='id identifier rubyid_in?'>in?</span><span class='lparen'>(</span><span class='lbracket'>[</span><span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>,</span><span class='tstring_end'>'</span></span><span class='comma'>,</span> <span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>;</span><span class='tstring_end'>'</span></span><span class='comma'>,</span> <span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>\s</span><span class='tstring_end'>'</span></span><span class='comma'>,</span> <span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>\t</span><span class='tstring_end'>'</span></span><span class='comma'>,</span> <span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>|</span><span class='tstring_end'>'</span></span><span class='rbracket'>]</span><span class='rparen'>)</span>
|
253
|
-
|
254
|
-
<span class='ivar'>@col_sep</span> <span class='op'>=</span> <span class='id identifier rubyid_value'>value</span>
|
255
|
-
<span class='kw'>end</span>
|
256
|
-
|
257
|
-
<span class='kw'>def</span> <span class='id identifier rubyid_converters='>converters=</span><span class='lparen'>(</span><span class='id identifier rubyid_value'>value</span><span class='rparen'>)</span>
|
258
|
-
<span class='id identifier rubyid_raise'>raise</span> <span class='const'>ArgumentError</span> <span class='kw'>unless</span> <span class='id identifier rubyid_value'>value</span><span class='period'>.</span><span class='id identifier rubyid_is_?'>is_?</span><span class='lparen'>(</span><span class='const'>Symbol</span><span class='rparen'>)</span> <span class='op'>&&</span> <span class='id identifier rubyid_value'>value</span><span class='period'>.</span><span class='id identifier rubyid_in?'>in?</span><span class='lparen'>(</span><span class='qsymbols_beg'>%i[</span><span class='tstring_content'>integer</span><span class='words_sep'> </span><span class='tstring_content'>float</span><span class='words_sep'> </span><span class='tstring_content'>date</span><span class='tstring_end'>]</span></span><span class='rparen'>)</span>
|
259
|
-
|
260
|
-
<span class='ivar'>@converters</span> <span class='op'>=</span> <span class='id identifier rubyid_value'>value</span>
|
261
|
-
<span class='kw'>end</span>
|
262
|
-
<span class='kw'>end</span>
|
263
|
-
</code></pre>
|
264
|
-
|
265
|
-
<h3 id="after-refactor-with-otoroshi-5-lines-351-characters-dedicated-to-properties">After refactor with Otoroshi (5 lines / 351 characters dedicated to properties)</h3>
|
266
|
-
|
267
|
-
<pre class="code ruby"><code class="ruby"><span class='kw'>class</span> <span class='const'>Importer</span>
|
268
|
-
<span class='id identifier rubyid_include'>include</span> <span class='const'><span class='object_link'><a href="Otoroshi.html" title="Otoroshi (module)">Otoroshi</a></span></span><span class='op'>::</span><span class='const'><span class='object_link'><a href="Otoroshi/Sanctuary.html" title="Otoroshi::Sanctuary (module)">Sanctuary</a></span></span>
|
269
|
-
|
270
|
-
<span class='id identifier rubyid_property'>property</span> <span class='symbol'>:file_path</span><span class='comma'>,</span> <span class='const'>String</span><span class='comma'>,</span> <span class='label'>validate:</span> <span class='tlambda'>-></span><span class='lparen'>(</span><span class='id identifier rubyid_v'>v</span><span class='rparen'>)</span> <span class='tlambeg'>{</span> <span class='id identifier rubyid_v'>v</span><span class='period'>.</span><span class='id identifier rubyid_match?'>match?</span> <span class='tstring'><span class='regexp_beg'>/</span><span class='tstring_content'>.+\.csv</span><span class='regexp_end'>/</span></span> <span class='rbrace'>}</span>
|
271
|
-
<span class='id identifier rubyid_property'>property</span> <span class='symbol'>:headers</span><span class='comma'>,</span> <span class='lbracket'>[</span><span class='const'>TrueClass</span><span class='comma'>,</span> <span class='const'>FalseClass</span><span class='rbracket'>]</span><span class='comma'>,</span> <span class='label'>default:</span> <span class='kw'>false</span>
|
272
|
-
<span class='id identifier rubyid_property'>property</span> <span class='symbol'>:col_sep</span><span class='comma'>,</span> <span class='const'>String</span><span class='comma'>,</span> <span class='label'>validate:</span> <span class='tlambda'>-></span><span class='lparen'>(</span><span class='id identifier rubyid_v'>v</span><span class='rparen'>)</span> <span class='tlambeg'>{</span> <span class='id identifier rubyid_v'>v</span><span class='period'>.</span><span class='id identifier rubyid_in?'>in?</span> <span class='lbracket'>[</span><span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>,</span><span class='tstring_end'>'</span></span><span class='comma'>,</span> <span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>;</span><span class='tstring_end'>'</span></span><span class='comma'>,</span> <span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>\s</span><span class='tstring_end'>'</span></span><span class='comma'>,</span> <span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>\t</span><span class='tstring_end'>'</span></span><span class='comma'>,</span> <span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>|</span><span class='tstring_end'>'</span></span><span class='rbracket'>]</span> <span class='rbrace'>}</span><span class='comma'>,</span> <span class='label'>default:</span> <span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>,</span><span class='tstring_end'>'</span></span>
|
273
|
-
<span class='id identifier rubyid_property'>property</span> <span class='symbol'>:converters</span><span class='comma'>,</span> <span class='const'>Symbol</span><span class='comma'>,</span> <span class='label'>validate:</span> <span class='tlambda'>-></span><span class='lparen'>(</span><span class='id identifier rubyid_v'>v</span><span class='rparen'>)</span> <span class='tlambeg'>{</span> <span class='id identifier rubyid_v'>v</span><span class='period'>.</span><span class='id identifier rubyid_in?'>in?</span> <span class='qsymbols_beg'>%i[</span><span class='tstring_content'>integer</span><span class='words_sep'> </span><span class='tstring_content'>float</span><span class='words_sep'> </span><span class='tstring_content'>date</span><span class='tstring_end'>]</span></span> <span class='rbrace'>}</span><span class='comma'>,</span> <span class='label'>allow_nil:</span> <span class='kw'>true</span>
|
274
|
-
|
275
|
-
<span class='id identifier rubyid_private'>private</span>
|
276
|
-
|
277
|
-
<span class='comment'># private business methods...
|
278
|
-
</span><span class='kw'>end</span>
|
279
|
-
</code></pre>
|
280
|
-
</div></div>
|
281
|
-
|
282
|
-
<div id="footer">
|
283
|
-
Generated on Wed Oct 14 00:24:01 2020 by
|
284
|
-
<a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
|
285
|
-
0.9.25 (ruby-2.6.6).
|
286
|
-
</div>
|
287
|
-
|
288
|
-
</div>
|
289
|
-
</body>
|
290
|
-
</html>
|
data/lib/doc/file_list.html
DELETED
@@ -1,56 +0,0 @@
|
|
1
|
-
<!DOCTYPE html>
|
2
|
-
<html>
|
3
|
-
<head>
|
4
|
-
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
5
|
-
<meta charset="utf-8" />
|
6
|
-
|
7
|
-
<link rel="stylesheet" href="css/full_list.css" type="text/css" media="screen" />
|
8
|
-
|
9
|
-
<link rel="stylesheet" href="css/common.css" type="text/css" media="screen" />
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
<script type="text/javascript" charset="utf-8" src="js/jquery.js"></script>
|
14
|
-
|
15
|
-
<script type="text/javascript" charset="utf-8" src="js/full_list.js"></script>
|
16
|
-
|
17
|
-
|
18
|
-
<title>File List</title>
|
19
|
-
<base id="base_target" target="_parent" />
|
20
|
-
</head>
|
21
|
-
<body>
|
22
|
-
<div id="content">
|
23
|
-
<div class="fixed_header">
|
24
|
-
<h1 id="full_list_header">File List</h1>
|
25
|
-
<div id="full_list_nav">
|
26
|
-
|
27
|
-
<span><a target="_self" href="class_list.html">
|
28
|
-
Classes
|
29
|
-
</a></span>
|
30
|
-
|
31
|
-
<span><a target="_self" href="method_list.html">
|
32
|
-
Methods
|
33
|
-
</a></span>
|
34
|
-
|
35
|
-
<span><a target="_self" href="file_list.html">
|
36
|
-
Files
|
37
|
-
</a></span>
|
38
|
-
|
39
|
-
</div>
|
40
|
-
|
41
|
-
<div id="search">Search: <input type="text" /></div>
|
42
|
-
</div>
|
43
|
-
|
44
|
-
<ul id="full_list" class="file">
|
45
|
-
|
46
|
-
|
47
|
-
<li id="object_README" class="odd">
|
48
|
-
<div class="item"><span class="object_link"><a href="index.html" title="README">README</a></span></div>
|
49
|
-
</li>
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
</ul>
|
54
|
-
</div>
|
55
|
-
</body>
|
56
|
-
</html>
|
data/lib/doc/frames.html
DELETED
@@ -1,17 +0,0 @@
|
|
1
|
-
<!DOCTYPE html>
|
2
|
-
<html>
|
3
|
-
<head>
|
4
|
-
<meta charset="utf-8">
|
5
|
-
<title>Documentation by YARD 0.9.25</title>
|
6
|
-
</head>
|
7
|
-
<script type="text/javascript">
|
8
|
-
var match = unescape(window.location.hash).match(/^#!(.+)/);
|
9
|
-
var name = match ? match[1] : 'index.html';
|
10
|
-
name = name.replace(/^(\w+):\/\//, '').replace(/^\/\//, '');
|
11
|
-
window.top.location = name;
|
12
|
-
</script>
|
13
|
-
<noscript>
|
14
|
-
<h1>Oops!</h1>
|
15
|
-
<h2>YARD requires JavaScript!</h2>
|
16
|
-
</noscript>
|
17
|
-
</html>
|
data/lib/doc/index.html
DELETED
@@ -1,290 +0,0 @@
|
|
1
|
-
<!DOCTYPE html>
|
2
|
-
<html>
|
3
|
-
<head>
|
4
|
-
<meta charset="UTF-8">
|
5
|
-
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
6
|
-
<title>
|
7
|
-
File: README
|
8
|
-
|
9
|
-
— Documentation by YARD 0.9.25
|
10
|
-
|
11
|
-
</title>
|
12
|
-
|
13
|
-
<link rel="stylesheet" href="css/style.css" type="text/css" />
|
14
|
-
|
15
|
-
<link rel="stylesheet" href="css/common.css" type="text/css" />
|
16
|
-
|
17
|
-
<script type="text/javascript">
|
18
|
-
pathId = "README";
|
19
|
-
relpath = '';
|
20
|
-
</script>
|
21
|
-
|
22
|
-
|
23
|
-
<script type="text/javascript" charset="utf-8" src="js/jquery.js"></script>
|
24
|
-
|
25
|
-
<script type="text/javascript" charset="utf-8" src="js/app.js"></script>
|
26
|
-
|
27
|
-
|
28
|
-
</head>
|
29
|
-
<body>
|
30
|
-
<div class="nav_wrap">
|
31
|
-
<iframe id="nav" src="class_list.html?1"></iframe>
|
32
|
-
<div id="resizer"></div>
|
33
|
-
</div>
|
34
|
-
|
35
|
-
<div id="main" tabindex="-1">
|
36
|
-
<div id="header">
|
37
|
-
<div id="menu">
|
38
|
-
|
39
|
-
<a href="_index.html">Index</a> »
|
40
|
-
<span class="title">File: README</span>
|
41
|
-
|
42
|
-
</div>
|
43
|
-
|
44
|
-
<div id="search">
|
45
|
-
|
46
|
-
<a class="full_list_link" id="class_list_link"
|
47
|
-
href="class_list.html">
|
48
|
-
|
49
|
-
<svg width="24" height="24">
|
50
|
-
<rect x="0" y="4" width="24" height="4" rx="1" ry="1"></rect>
|
51
|
-
<rect x="0" y="12" width="24" height="4" rx="1" ry="1"></rect>
|
52
|
-
<rect x="0" y="20" width="24" height="4" rx="1" ry="1"></rect>
|
53
|
-
</svg>
|
54
|
-
</a>
|
55
|
-
|
56
|
-
</div>
|
57
|
-
<div class="clear"></div>
|
58
|
-
</div>
|
59
|
-
|
60
|
-
<div id="content"><div id='filecontents'><h1 id="otoroshi">Otoroshi</h1>
|
61
|
-
|
62
|
-
<p><img src="https://upload.wikimedia.org/wikipedia/commons/4/41/Suushi_Otoroshi.jpg" alt="otoroshi" title="Sawaki Suushi"></p>
|
63
|
-
|
64
|
-
<blockquote>
|
65
|
-
<p>-- <cite>Illustration from Sawaki Suushi, 1737</cite></p>
|
66
|
-
</blockquote>
|
67
|
-
|
68
|
-
<p>Otoroshis are legendary creatures in Japanese folklore and mythology. They act as guardian of holy temples.</p>
|
69
|
-
|
70
|
-
<p>The <code>otoroshi</code> gem helps you defining and validating class properties.</p>
|
71
|
-
|
72
|
-
<p>See <a href="#refactor-example">an example of refactor</a> with Otoroshi.</p>
|
73
|
-
|
74
|
-
<h2 id="installation">Installation</h2>
|
75
|
-
|
76
|
-
<p>Add this line to your application's Gemfile:</p>
|
77
|
-
|
78
|
-
<pre class="code ruby"><code class="ruby"><span class='id identifier rubyid_gem'>gem</span> <span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>otoroshi</span><span class='tstring_end'>'</span></span>
|
79
|
-
</code></pre>
|
80
|
-
|
81
|
-
<p>And then execute:</p>
|
82
|
-
|
83
|
-
<pre class="code ruby"><code class="ruby">$ bundle
|
84
|
-
</code></pre>
|
85
|
-
|
86
|
-
<h2 id="usage">Usage</h2>
|
87
|
-
|
88
|
-
<p>Include <code>Otoroshi::Sanctuary</code> in a class to easily define arguments validation.</p>
|
89
|
-
|
90
|
-
<pre class="code ruby"><code class="ruby"><span class='kw'>class</span> <span class='const'>MyClass</span>
|
91
|
-
<span class='id identifier rubyid_include'>include</span> <span class='const'><span class='object_link'><a href="Otoroshi.html" title="Otoroshi (module)">Otoroshi</a></span></span><span class='op'>::</span><span class='const'><span class='object_link'><a href="Otoroshi/Sanctuary.html" title="Otoroshi::Sanctuary (module)">Sanctuary</a></span></span>
|
92
|
-
<span class='kw'>end</span>
|
93
|
-
</code></pre>
|
94
|
-
|
95
|
-
<h3 id="define-classs-properties">Define class's properties</h3>
|
96
|
-
|
97
|
-
<p>Use the <code>::property(name, type, options)</code> method to add a property.</p>
|
98
|
-
|
99
|
-
<ul>
|
100
|
-
<li><code>name</code>: name of the property (symbol or string)</li>
|
101
|
-
<li><code>type</code>: the class the future value should belongs to (class or array of classes, <code>Object</code> by default)</li>
|
102
|
-
<li>options:
|
103
|
-
|
104
|
-
<ul>
|
105
|
-
<li><code>validate</code>: a custom validation to apply to the future value (lambda, <code>->(_) { true }</code> by default)</li>
|
106
|
-
<li><code>allow_nil</code>: define if the future value can be set to nil (boolean, <code>false</code> by default)</li>
|
107
|
-
<li><code>default</code>: the default value for this property (should match the required type, <code>nil</code> by default)</li>
|
108
|
-
</ul></li>
|
109
|
-
</ul>
|
110
|
-
|
111
|
-
<p>Getters and a setters are automatically set:</p>
|
112
|
-
|
113
|
-
<pre class="code ruby"><code class="ruby"><span class='kw'>class</span> <span class='const'>Example</span>
|
114
|
-
<span class='id identifier rubyid_include'>include</span> <span class='const'><span class='object_link'><a href="Otoroshi.html" title="Otoroshi (module)">Otoroshi</a></span></span><span class='op'>::</span><span class='const'><span class='object_link'><a href="Otoroshi/Sanctuary.html" title="Otoroshi::Sanctuary (module)">Sanctuary</a></span></span>
|
115
|
-
|
116
|
-
<span class='id identifier rubyid_property'>property</span> <span class='symbol'>:foo</span><span class='comma'>,</span> <span class='const'>Integer</span>
|
117
|
-
<span class='id identifier rubyid_property'>property</span> <span class='symbol'>:bar</span><span class='comma'>,</span> <span class='const'>String</span>
|
118
|
-
<span class='kw'>end</span>
|
119
|
-
|
120
|
-
<span class='id identifier rubyid_instance'>instance</span> <span class='op'>=</span> <span class='const'>Example</span><span class='period'>.</span><span class='id identifier rubyid_new'>new</span><span class='lparen'>(</span><span class='label'>foo:</span> <span class='int'>42</span><span class='comma'>,</span> <span class='label'>bar:</span> <span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>hello</span><span class='tstring_end'>'</span></span><span class='rparen'>)</span>
|
121
|
-
|
122
|
-
<span class='id identifier rubyid_instance'>instance</span><span class='period'>.</span><span class='id identifier rubyid_foo'>foo</span> <span class='comment'># 42
|
123
|
-
</span><span class='id identifier rubyid_instance'>instance</span><span class='period'>.</span><span class='id identifier rubyid_bar'>bar</span> <span class='comment'># hello
|
124
|
-
</span>
|
125
|
-
<span class='id identifier rubyid_instance'>instance</span><span class='period'>.</span><span class='id identifier rubyid_foo'>foo</span> <span class='op'>=</span> <span class='int'>7</span>
|
126
|
-
<span class='id identifier rubyid_instance'>instance</span><span class='period'>.</span><span class='id identifier rubyid_bar'>bar</span> <span class='op'>=</span> <span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>world</span><span class='tstring_end'>'</span></span>
|
127
|
-
|
128
|
-
<span class='id identifier rubyid_instance'>instance</span><span class='period'>.</span><span class='id identifier rubyid_foo'>foo</span> <span class='comment'># 7
|
129
|
-
</span><span class='id identifier rubyid_instance'>instance</span><span class='period'>.</span><span class='id identifier rubyid_bar'>bar</span> <span class='comment'># world
|
130
|
-
</span></code></pre>
|
131
|
-
|
132
|
-
<p>Type validations run on initialization and assignment:</p>
|
133
|
-
|
134
|
-
<pre class="code ruby"><code class="ruby"><span class='kw'>class</span> <span class='const'>Example</span>
|
135
|
-
<span class='id identifier rubyid_include'>include</span> <span class='const'><span class='object_link'><a href="Otoroshi.html" title="Otoroshi (module)">Otoroshi</a></span></span><span class='op'>::</span><span class='const'><span class='object_link'><a href="Otoroshi/Sanctuary.html" title="Otoroshi::Sanctuary (module)">Sanctuary</a></span></span>
|
136
|
-
|
137
|
-
<span class='id identifier rubyid_property'>property</span> <span class='symbol'>:foo</span><span class='comma'>,</span> <span class='const'>Integer</span>
|
138
|
-
<span class='kw'>end</span>
|
139
|
-
|
140
|
-
<span class='const'>Example</span><span class='period'>.</span><span class='id identifier rubyid_new'>new</span> <span class='comment'># => ArgumentError, "foo does not match required type"
|
141
|
-
</span><span class='const'>Example</span><span class='period'>.</span><span class='id identifier rubyid_new'>new</span><span class='lparen'>(</span><span class='label'>foo:</span> <span class='float'>1.5</span><span class='rparen'>)</span> <span class='comment'># => ArgumentError, "foo does not match required type"
|
142
|
-
</span>
|
143
|
-
<span class='id identifier rubyid_instance'>instance</span><span class='period'>.</span><span class='id identifier rubyid_foo'>foo</span> <span class='op'>=</span> <span class='kw'>nil</span> <span class='comment'># => ArgumentError, "foo does not match required type"
|
144
|
-
</span><span class='id identifier rubyid_instance'>instance</span><span class='period'>.</span><span class='id identifier rubyid_foo'>foo</span> <span class='op'>=</span> <span class='float'>1.5</span> <span class='comment'># => ArgumentError, "foo does not match required type"
|
145
|
-
</span></code></pre>
|
146
|
-
|
147
|
-
<p>You can provide multiple authorized types:</p>
|
148
|
-
|
149
|
-
<pre class="code ruby"><code class="ruby"><span class='kw'>class</span> <span class='const'>Example</span>
|
150
|
-
<span class='id identifier rubyid_include'>include</span> <span class='const'><span class='object_link'><a href="Otoroshi.html" title="Otoroshi (module)">Otoroshi</a></span></span><span class='op'>::</span><span class='const'><span class='object_link'><a href="Otoroshi/Sanctuary.html" title="Otoroshi::Sanctuary (module)">Sanctuary</a></span></span>
|
151
|
-
|
152
|
-
<span class='id identifier rubyid_property'>property</span> <span class='symbol'>:foo</span><span class='comma'>,</span> <span class='lbracket'>[</span><span class='const'>Symbol</span><span class='comma'>,</span> <span class='const'>String</span><span class='rbracket'>]</span>
|
153
|
-
<span class='id identifier rubyid_property'>property</span> <span class='symbol'>:bar</span><span class='comma'>,</span> <span class='lbracket'>[</span><span class='const'>TrueClass</span><span class='comma'>,</span> <span class='const'>FalseClass</span><span class='rbracket'>]</span>
|
154
|
-
<span class='kw'>end</span>
|
155
|
-
|
156
|
-
<span class='const'>Example</span><span class='period'>.</span><span class='id identifier rubyid_new'>new</span><span class='lparen'>(</span><span class='label'>foo:</span> <span class='symbol'>:hello</span><span class='comma'>,</span> <span class='label'>bar:</span> <span class='kw'>true</span><span class='rparen'>)</span>
|
157
|
-
<span class='const'>Example</span><span class='period'>.</span><span class='id identifier rubyid_new'>new</span><span class='lparen'>(</span><span class='label'>foo:</span> <span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>hello</span><span class='tstring_end'>'</span></span><span class='comma'>,</span> <span class='label'>bar:</span> <span class='kw'>false</span><span class='rparen'>)</span>
|
158
|
-
</code></pre>
|
159
|
-
|
160
|
-
<p>You can avoid the second argument so any <code>Object</code> can be passed:</p>
|
161
|
-
|
162
|
-
<pre class="code ruby"><code class="ruby"><span class='kw'>class</span> <span class='const'>Example</span>
|
163
|
-
<span class='id identifier rubyid_include'>include</span> <span class='const'><span class='object_link'><a href="Otoroshi.html" title="Otoroshi (module)">Otoroshi</a></span></span><span class='op'>::</span><span class='const'><span class='object_link'><a href="Otoroshi/Sanctuary.html" title="Otoroshi::Sanctuary (module)">Sanctuary</a></span></span>
|
164
|
-
|
165
|
-
<span class='id identifier rubyid_property'>property</span> <span class='symbol'>:foo</span>
|
166
|
-
<span class='kw'>end</span>
|
167
|
-
|
168
|
-
<span class='const'>Example</span><span class='period'>.</span><span class='id identifier rubyid_new'>new</span><span class='lparen'>(</span><span class='label'>foo:</span> <span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>hello</span><span class='tstring_end'>'</span></span><span class='rparen'>)</span>
|
169
|
-
<span class='const'>Example</span><span class='period'>.</span><span class='id identifier rubyid_new'>new</span><span class='lparen'>(</span><span class='label'>foo:</span> <span class='int'>42</span><span class='rparen'>)</span>
|
170
|
-
<span class='const'>Example</span><span class='period'>.</span><span class='id identifier rubyid_new'>new</span><span class='lparen'>(</span><span class='label'>foo:</span> <span class='const'>User</span><span class='period'>.</span><span class='id identifier rubyid_find'>find</span><span class='lparen'>(</span><span class='int'>1</span><span class='rparen'>)</span><span class='rparen'>)</span>
|
171
|
-
</code></pre>
|
172
|
-
|
173
|
-
<p>You can add custom validations with the <code>validate:</code> option.</p>
|
174
|
-
|
175
|
-
<pre class="code ruby"><code class="ruby"><span class='kw'>class</span> <span class='const'>Example</span>
|
176
|
-
<span class='id identifier rubyid_include'>include</span> <span class='const'><span class='object_link'><a href="Otoroshi.html" title="Otoroshi (module)">Otoroshi</a></span></span><span class='op'>::</span><span class='const'><span class='object_link'><a href="Otoroshi/Sanctuary.html" title="Otoroshi::Sanctuary (module)">Sanctuary</a></span></span>
|
177
|
-
|
178
|
-
<span class='id identifier rubyid_property'>property</span> <span class='symbol'>:foo</span><span class='comma'>,</span> <span class='const'>Integer</span><span class='comma'>,</span> <span class='tlambda'>-></span><span class='lparen'>(</span><span class='id identifier rubyid_v'>v</span><span class='rparen'>)</span> <span class='tlambeg'>{</span> <span class='id identifier rubyid_v'>v</span> <span class='op'>></span> <span class='int'>0</span> <span class='rbrace'>}</span>
|
179
|
-
<span class='kw'>end</span>
|
180
|
-
|
181
|
-
<span class='const'>Example</span><span class='period'>.</span><span class='id identifier rubyid_new'>new</span><span class='lparen'>(</span><span class='label'>foo:</span> <span class='op'>-</span><span class='int'>1</span><span class='rparen'>)</span> <span class='comment'># => ArgumentError, "foo does not match validation"
|
182
|
-
</span>
|
183
|
-
<span class='id identifier rubyid_instance'>instance</span><span class='period'>.</span><span class='id identifier rubyid_foo'>foo</span> <span class='op'>=</span> <span class='op'>-</span><span class='int'>1</span> <span class='comment'># => ArgumentError, "foo does not match validation"
|
184
|
-
</span></code></pre>
|
185
|
-
|
186
|
-
<p>Set <code>allow_nil:</code> option to <code>true</code> if <code>nil</code> is authorized:</p>
|
187
|
-
|
188
|
-
<pre class="code ruby"><code class="ruby"><span class='kw'>class</span> <span class='const'>Example</span>
|
189
|
-
<span class='id identifier rubyid_include'>include</span> <span class='const'><span class='object_link'><a href="Otoroshi.html" title="Otoroshi (module)">Otoroshi</a></span></span><span class='op'>::</span><span class='const'><span class='object_link'><a href="Otoroshi/Sanctuary.html" title="Otoroshi::Sanctuary (module)">Sanctuary</a></span></span>
|
190
|
-
|
191
|
-
<span class='id identifier rubyid_property'>property</span> <span class='symbol'>:foo</span><span class='comma'>,</span> <span class='const'>Integer</span><span class='comma'>,</span> <span class='tlambda'>-></span><span class='lparen'>(</span><span class='id identifier rubyid_v'>v</span><span class='rparen'>)</span> <span class='tlambeg'>{</span> <span class='id identifier rubyid_v'>v</span> <span class='op'>></span> <span class='int'>0</span> <span class='rbrace'>}</span><span class='comma'>,</span> <span class='label'>allow_nil:</span> <span class='kw'>true</span>
|
192
|
-
<span class='kw'>end</span>
|
193
|
-
|
194
|
-
<span class='id identifier rubyid_instance'>instance</span> <span class='op'>=</span> <span class='const'>Example</span><span class='period'>.</span><span class='id identifier rubyid_new'>new</span>
|
195
|
-
<span class='id identifier rubyid_instance'>instance</span><span class='period'>.</span><span class='id identifier rubyid_foo'>foo</span> <span class='comment'># nil
|
196
|
-
</span>
|
197
|
-
<span class='id identifier rubyid_instance'>instance</span><span class='period'>.</span><span class='id identifier rubyid_foo'>foo</span> <span class='op'>=</span> <span class='int'>42</span>
|
198
|
-
<span class='id identifier rubyid_instance'>instance</span><span class='period'>.</span><span class='id identifier rubyid_foo'>foo</span> <span class='op'>=</span> <span class='kw'>nil</span>
|
199
|
-
<span class='id identifier rubyid_instance'>instance</span><span class='period'>.</span><span class='id identifier rubyid_foo'>foo</span> <span class='comment'># nil
|
200
|
-
</span><span class='id identifier rubyid_instance'>instance</span><span class='period'>.</span><span class='id identifier rubyid_foo'>foo</span> <span class='op'>=</span> <span class='op'>-</span><span class='int'>1</span> <span class='comment'># => ArgumentError, "foo does not match validation"
|
201
|
-
</span></code></pre>
|
202
|
-
|
203
|
-
<p>Set <code>default:</code> option to the default value. You can always set the value to <code>nil</code> if <code>allow_nil</code> is <code>true</code>.</p>
|
204
|
-
|
205
|
-
<pre class="code ruby"><code class="ruby"><span class='kw'>class</span> <span class='const'>Example</span>
|
206
|
-
<span class='id identifier rubyid_include'>include</span> <span class='const'><span class='object_link'><a href="Otoroshi.html" title="Otoroshi (module)">Otoroshi</a></span></span><span class='op'>::</span><span class='const'><span class='object_link'><a href="Otoroshi/Sanctuary.html" title="Otoroshi::Sanctuary (module)">Sanctuary</a></span></span>
|
207
|
-
|
208
|
-
<span class='id identifier rubyid_property'>property</span> <span class='symbol'>:foo</span><span class='comma'>,</span> <span class='const'>Integer</span><span class='comma'>,</span> <span class='tlambda'>-></span><span class='lparen'>(</span><span class='id identifier rubyid_v'>v</span><span class='rparen'>)</span> <span class='tlambeg'>{</span> <span class='id identifier rubyid_v'>v</span> <span class='op'>></span> <span class='int'>0</span> <span class='rbrace'>}</span><span class='comma'>,</span> <span class='label'>default:</span> <span class='int'>1</span><span class='comma'>,</span> <span class='label'>allow_nil:</span> <span class='kw'>true</span>
|
209
|
-
<span class='kw'>end</span>
|
210
|
-
|
211
|
-
<span class='id identifier rubyid_instance'>instance</span> <span class='op'>=</span> <span class='const'>Example</span><span class='period'>.</span><span class='id identifier rubyid_new'>new</span> <span class='comment'># no error
|
212
|
-
</span><span class='id identifier rubyid_instance'>instance</span><span class='period'>.</span><span class='id identifier rubyid_foo'>foo</span> <span class='comment'># 1
|
213
|
-
</span>
|
214
|
-
<span class='id identifier rubyid_instance'>instance</span> <span class='op'>=</span> <span class='const'>Example</span><span class='period'>.</span><span class='id identifier rubyid_new'>new</span><span class='lparen'>(</span><span class='label'>foo:</span> <span class='kw'>nil</span><span class='rparen'>)</span> <span class='comment'># no error
|
215
|
-
</span><span class='id identifier rubyid_instance'>instance</span><span class='period'>.</span><span class='id identifier rubyid_foo'>foo</span> <span class='comment'># nil
|
216
|
-
</span>
|
217
|
-
<span class='id identifier rubyid_instance'>instance</span><span class='period'>.</span><span class='id identifier rubyid_foo'>foo</span> <span class='op'>=</span> <span class='kw'>nil</span> <span class='comment'># no error
|
218
|
-
</span><span class='id identifier rubyid_instance'>instance</span><span class='period'>.</span><span class='id identifier rubyid_foo'>foo</span> <span class='comment'># nil
|
219
|
-
</span></code></pre>
|
220
|
-
|
221
|
-
<h2 id="refactor-example">Refactor Example</h2>
|
222
|
-
|
223
|
-
<h3 id="before-refactor-23-lines-765-characters-dedicated-to-properties">Before refactor (23 lines / 765 characters dedicated to properties)</h3>
|
224
|
-
|
225
|
-
<pre class="code ruby"><code class="ruby"><span class='kw'>class</span> <span class='const'>Importer</span>
|
226
|
-
<span class='id identifier rubyid_attr_reader'>attr_reader</span> <span class='symbol'>:file_path</span><span class='comma'>,</span> <span class='symbol'>:headers</span><span class='comma'>,</span> <span class='symbol'>:col_sep</span><span class='comma'>,</span> <span class='symbol'>:converters</span>
|
227
|
-
|
228
|
-
<span class='kw'>def</span> <span class='id identifier rubyid_initialize'>initialize</span><span class='lparen'>(</span><span class='label'>file_path:</span><span class='comma'>,</span> <span class='label'>headers:</span> <span class='kw'>false</span><span class='comma'>,</span> <span class='label'>col_sep:</span> <span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>,</span><span class='tstring_end'>'</span></span><span class='comma'>,</span> <span class='label'>converters:</span> <span class='kw'>nil</span><span class='rparen'>)</span>
|
229
|
-
<span class='kw'>self</span><span class='period'>.</span><span class='id identifier rubyid_file_path'>file_path</span> <span class='op'>=</span> <span class='id identifier rubyid_file_path'>file_path</span>
|
230
|
-
<span class='kw'>self</span><span class='period'>.</span><span class='id identifier rubyid_headers'>headers</span> <span class='op'>=</span> <span class='id identifier rubyid_headers'>headers</span>
|
231
|
-
<span class='kw'>self</span><span class='period'>.</span><span class='id identifier rubyid_col_sep'>col_sep</span> <span class='op'>=</span> <span class='id identifier rubyid_col_sep'>col_sep</span>
|
232
|
-
<span class='kw'>self</span><span class='period'>.</span><span class='id identifier rubyid_converters'>converters</span> <span class='op'>=</span> <span class='id identifier rubyid_converters'>converters</span>
|
233
|
-
<span class='kw'>end</span>
|
234
|
-
|
235
|
-
<span class='id identifier rubyid_private'>private</span>
|
236
|
-
|
237
|
-
<span class='comment'># private business methods...
|
238
|
-
</span>
|
239
|
-
<span class='kw'>def</span> <span class='id identifier rubyid_file_path='>file_path=</span><span class='lparen'>(</span><span class='id identifier rubyid_value'>value</span><span class='rparen'>)</span>
|
240
|
-
<span class='id identifier rubyid_raise'>raise</span> <span class='const'>ArgumentError</span> <span class='kw'>unless</span> <span class='id identifier rubyid_value'>value</span><span class='period'>.</span><span class='id identifier rubyid_is_?'>is_?</span><span class='lparen'>(</span><span class='const'>String</span><span class='rparen'>)</span> <span class='op'>&&</span> <span class='id identifier rubyid_value'>value</span><span class='period'>.</span><span class='id identifier rubyid_match?'>match?</span><span class='lparen'>(</span><span class='tstring'><span class='regexp_beg'>/</span><span class='tstring_content'>.+\.csv</span><span class='regexp_end'>/</span></span><span class='rparen'>)</span>
|
241
|
-
|
242
|
-
<span class='ivar'>@file_path</span> <span class='op'>=</span> <span class='id identifier rubyid_value'>value</span>
|
243
|
-
<span class='kw'>end</span>
|
244
|
-
|
245
|
-
<span class='kw'>def</span> <span class='id identifier rubyid_headers='>headers=</span><span class='lparen'>(</span><span class='id identifier rubyid_value'>value</span><span class='rparen'>)</span>
|
246
|
-
<span class='id identifier rubyid_raise'>raise</span> <span class='const'>ArgumentError</span> <span class='kw'>unless</span> <span class='lbracket'>[</span><span class='kw'>true</span><span class='comma'>,</span> <span class='kw'>false</span><span class='rbracket'>]</span><span class='period'>.</span><span class='id identifier rubyid_include?'>include?</span><span class='lparen'>(</span><span class='id identifier rubyid_value'>value</span><span class='rparen'>)</span>
|
247
|
-
|
248
|
-
<span class='ivar'>@headers</span> <span class='op'>=</span> <span class='id identifier rubyid_value'>value</span>
|
249
|
-
<span class='kw'>end</span>
|
250
|
-
|
251
|
-
<span class='kw'>def</span> <span class='id identifier rubyid_col_sep='>col_sep=</span><span class='lparen'>(</span><span class='id identifier rubyid_value'>value</span><span class='rparen'>)</span>
|
252
|
-
<span class='id identifier rubyid_raise'>raise</span> <span class='const'>ArgumentError</span> <span class='kw'>unless</span> <span class='id identifier rubyid_value'>value</span><span class='period'>.</span><span class='id identifier rubyid_is_?'>is_?</span><span class='lparen'>(</span><span class='const'>String</span><span class='rparen'>)</span> <span class='op'>&&</span> <span class='id identifier rubyid_value'>value</span><span class='period'>.</span><span class='id identifier rubyid_in?'>in?</span><span class='lparen'>(</span><span class='lbracket'>[</span><span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>,</span><span class='tstring_end'>'</span></span><span class='comma'>,</span> <span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>;</span><span class='tstring_end'>'</span></span><span class='comma'>,</span> <span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>\s</span><span class='tstring_end'>'</span></span><span class='comma'>,</span> <span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>\t</span><span class='tstring_end'>'</span></span><span class='comma'>,</span> <span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>|</span><span class='tstring_end'>'</span></span><span class='rbracket'>]</span><span class='rparen'>)</span>
|
253
|
-
|
254
|
-
<span class='ivar'>@col_sep</span> <span class='op'>=</span> <span class='id identifier rubyid_value'>value</span>
|
255
|
-
<span class='kw'>end</span>
|
256
|
-
|
257
|
-
<span class='kw'>def</span> <span class='id identifier rubyid_converters='>converters=</span><span class='lparen'>(</span><span class='id identifier rubyid_value'>value</span><span class='rparen'>)</span>
|
258
|
-
<span class='id identifier rubyid_raise'>raise</span> <span class='const'>ArgumentError</span> <span class='kw'>unless</span> <span class='id identifier rubyid_value'>value</span><span class='period'>.</span><span class='id identifier rubyid_is_?'>is_?</span><span class='lparen'>(</span><span class='const'>Symbol</span><span class='rparen'>)</span> <span class='op'>&&</span> <span class='id identifier rubyid_value'>value</span><span class='period'>.</span><span class='id identifier rubyid_in?'>in?</span><span class='lparen'>(</span><span class='qsymbols_beg'>%i[</span><span class='tstring_content'>integer</span><span class='words_sep'> </span><span class='tstring_content'>float</span><span class='words_sep'> </span><span class='tstring_content'>date</span><span class='tstring_end'>]</span></span><span class='rparen'>)</span>
|
259
|
-
|
260
|
-
<span class='ivar'>@converters</span> <span class='op'>=</span> <span class='id identifier rubyid_value'>value</span>
|
261
|
-
<span class='kw'>end</span>
|
262
|
-
<span class='kw'>end</span>
|
263
|
-
</code></pre>
|
264
|
-
|
265
|
-
<h3 id="after-refactor-with-otoroshi-5-lines-351-characters-dedicated-to-properties">After refactor with Otoroshi (5 lines / 351 characters dedicated to properties)</h3>
|
266
|
-
|
267
|
-
<pre class="code ruby"><code class="ruby"><span class='kw'>class</span> <span class='const'>Importer</span>
|
268
|
-
<span class='id identifier rubyid_include'>include</span> <span class='const'><span class='object_link'><a href="Otoroshi.html" title="Otoroshi (module)">Otoroshi</a></span></span><span class='op'>::</span><span class='const'><span class='object_link'><a href="Otoroshi/Sanctuary.html" title="Otoroshi::Sanctuary (module)">Sanctuary</a></span></span>
|
269
|
-
|
270
|
-
<span class='id identifier rubyid_property'>property</span> <span class='symbol'>:file_path</span><span class='comma'>,</span> <span class='const'>String</span><span class='comma'>,</span> <span class='label'>validate:</span> <span class='tlambda'>-></span><span class='lparen'>(</span><span class='id identifier rubyid_v'>v</span><span class='rparen'>)</span> <span class='tlambeg'>{</span> <span class='id identifier rubyid_v'>v</span><span class='period'>.</span><span class='id identifier rubyid_match?'>match?</span> <span class='tstring'><span class='regexp_beg'>/</span><span class='tstring_content'>.+\.csv</span><span class='regexp_end'>/</span></span> <span class='rbrace'>}</span>
|
271
|
-
<span class='id identifier rubyid_property'>property</span> <span class='symbol'>:headers</span><span class='comma'>,</span> <span class='lbracket'>[</span><span class='const'>TrueClass</span><span class='comma'>,</span> <span class='const'>FalseClass</span><span class='rbracket'>]</span><span class='comma'>,</span> <span class='label'>default:</span> <span class='kw'>false</span>
|
272
|
-
<span class='id identifier rubyid_property'>property</span> <span class='symbol'>:col_sep</span><span class='comma'>,</span> <span class='const'>String</span><span class='comma'>,</span> <span class='label'>validate:</span> <span class='tlambda'>-></span><span class='lparen'>(</span><span class='id identifier rubyid_v'>v</span><span class='rparen'>)</span> <span class='tlambeg'>{</span> <span class='id identifier rubyid_v'>v</span><span class='period'>.</span><span class='id identifier rubyid_in?'>in?</span> <span class='lbracket'>[</span><span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>,</span><span class='tstring_end'>'</span></span><span class='comma'>,</span> <span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>;</span><span class='tstring_end'>'</span></span><span class='comma'>,</span> <span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>\s</span><span class='tstring_end'>'</span></span><span class='comma'>,</span> <span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>\t</span><span class='tstring_end'>'</span></span><span class='comma'>,</span> <span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>|</span><span class='tstring_end'>'</span></span><span class='rbracket'>]</span> <span class='rbrace'>}</span><span class='comma'>,</span> <span class='label'>default:</span> <span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>,</span><span class='tstring_end'>'</span></span>
|
273
|
-
<span class='id identifier rubyid_property'>property</span> <span class='symbol'>:converters</span><span class='comma'>,</span> <span class='const'>Symbol</span><span class='comma'>,</span> <span class='label'>validate:</span> <span class='tlambda'>-></span><span class='lparen'>(</span><span class='id identifier rubyid_v'>v</span><span class='rparen'>)</span> <span class='tlambeg'>{</span> <span class='id identifier rubyid_v'>v</span><span class='period'>.</span><span class='id identifier rubyid_in?'>in?</span> <span class='qsymbols_beg'>%i[</span><span class='tstring_content'>integer</span><span class='words_sep'> </span><span class='tstring_content'>float</span><span class='words_sep'> </span><span class='tstring_content'>date</span><span class='tstring_end'>]</span></span> <span class='rbrace'>}</span><span class='comma'>,</span> <span class='label'>allow_nil:</span> <span class='kw'>true</span>
|
274
|
-
|
275
|
-
<span class='id identifier rubyid_private'>private</span>
|
276
|
-
|
277
|
-
<span class='comment'># private business methods...
|
278
|
-
</span><span class='kw'>end</span>
|
279
|
-
</code></pre>
|
280
|
-
</div></div>
|
281
|
-
|
282
|
-
<div id="footer">
|
283
|
-
Generated on Wed Oct 14 00:24:01 2020 by
|
284
|
-
<a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
|
285
|
-
0.9.25 (ruby-2.6.6).
|
286
|
-
</div>
|
287
|
-
|
288
|
-
</div>
|
289
|
-
</body>
|
290
|
-
</html>
|