selfies 0.4.0 → 0.5.0
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.
- checksums.yaml +4 -4
- data/.gitignore +0 -2
- data/CODE_OF_CONDUCT.md +1 -1
- data/Gemfile +1 -2
- data/README.md +81 -40
- data/Rakefile +3 -3
- data/bin/console +3 -5
- data/lib/selfies/self_init.rb +9 -1
- data/lib/selfies/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 85d6373067c8c9773cbf11a7c4a5cd4f522c3cde
|
4
|
+
data.tar.gz: 03b21feacc2cb82878bc201ed807c2c83942d209
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 408b2541b8760bcea0c5e8e48ae626279b0b1294b391fbbfc221a5f08511a33b91a060ef0df08b2798edae25e7e3a04dd13a90125945876241e247dc844a2178
|
7
|
+
data.tar.gz: 656a2d54502b6353ab1b7c332b262ae964a7f34cfabcefbce1e3fcdef6b8eddae97e9ef814ad9541c6f407484487ff57204a0ec1399879e7d3d70a10eb4d7ca8
|
data/.gitignore
CHANGED
data/CODE_OF_CONDUCT.md
CHANGED
@@ -55,7 +55,7 @@ further defined and clarified by project maintainers.
|
|
55
55
|
## Enforcement
|
56
56
|
|
57
57
|
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
-
reported by contacting the project team at
|
58
|
+
reported by contacting the project team at mariodarco78@icloud.com All
|
59
59
|
complaints will be reviewed and investigated and will result in a response that
|
60
60
|
is deemed necessary and appropriate to the circumstances. The project team is
|
61
61
|
obligated to maintain confidentiality with regard to the reporter of an incident.
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -12,6 +12,45 @@ Another day at work, or on your personal project, and you need to create yet ano
|
|
12
12
|
|
13
13
|
This gets boring with the years. So boring that someone might decide to write some macros to reduce the boilerplate.
|
14
14
|
|
15
|
+
## TL;DR
|
16
|
+
|
17
|
+
You add this:
|
18
|
+
```ruby
|
19
|
+
gem 'selfies'
|
20
|
+
```
|
21
|
+
|
22
|
+
You write this:
|
23
|
+
```ruby
|
24
|
+
class Rectangle
|
25
|
+
attr_accessor_init :width, :height, scale: 100
|
26
|
+
selfie :area, :perimeter
|
27
|
+
|
28
|
+
def area
|
29
|
+
proportion(width * height)
|
30
|
+
end
|
31
|
+
|
32
|
+
def perimeter
|
33
|
+
proportion((width + height) * 2)
|
34
|
+
end
|
35
|
+
|
36
|
+
private
|
37
|
+
|
38
|
+
def proportion(value)
|
39
|
+
value.to_f / 100 * scale
|
40
|
+
end
|
41
|
+
end
|
42
|
+
```
|
43
|
+
|
44
|
+
You get this:
|
45
|
+
```ruby
|
46
|
+
>> Rectangle.area(8, 4)
|
47
|
+
=> 32.0
|
48
|
+
>> Rectangle.perimeter(5, 3)
|
49
|
+
=> 16.0
|
50
|
+
>> Rectangle.perimeter(5, 3, 50)
|
51
|
+
=> 8.0
|
52
|
+
```
|
53
|
+
|
15
54
|
## Disclaimer
|
16
55
|
In this project you will likely read me using both ```initialize``` and ```initialise```. I'm Italian, I learnt to code from American books and now live in UK, to me they both make sense. As a rule of thumb, it's the ```ize``` form in code and ```ise``` form everywhere else. But might mix them so bear with me. Thanks!
|
17
56
|
|
@@ -60,21 +99,21 @@ class Search
|
|
60
99
|
end
|
61
100
|
```
|
62
101
|
|
63
|
-
|
102
|
+
It is possible to specify a default for the last argument only:
|
64
103
|
|
65
104
|
This code:
|
66
105
|
```ruby
|
67
106
|
class Search
|
68
|
-
|
107
|
+
attr_reader_init :term, :page, limit: 5
|
69
108
|
end
|
70
109
|
```
|
71
110
|
|
72
111
|
Is equivalent to:
|
73
112
|
```ruby
|
74
113
|
class Search
|
75
|
-
|
114
|
+
attr_reader :term, :page, :limit
|
76
115
|
|
77
|
-
def initialize(term, page, limit)
|
116
|
+
def initialize(term, page, limit = 5)
|
78
117
|
@term = term
|
79
118
|
@page = page
|
80
119
|
@limit = limit
|
@@ -82,32 +121,60 @@ class Search
|
|
82
121
|
end
|
83
122
|
```
|
84
123
|
|
85
|
-
|
124
|
+
```ruby
|
125
|
+
>> search = Search.new('foo', 1)
|
126
|
+
>> search.limit
|
127
|
+
=> 5
|
128
|
+
```
|
129
|
+
|
130
|
+
You can use any variable name that would make for a valid Ruby variable;
|
131
|
+
Only exception is ```:args``` which is used by ```selfies``` to define a splat operator:
|
86
132
|
|
87
133
|
This code:
|
88
134
|
```ruby
|
89
135
|
class Search
|
90
|
-
|
136
|
+
attr_reader_init :term, :args
|
91
137
|
end
|
92
138
|
```
|
93
139
|
|
94
140
|
Is equivalent to:
|
95
141
|
```ruby
|
96
142
|
class Search
|
97
|
-
|
143
|
+
attr_reader :term, :args
|
98
144
|
|
99
|
-
def initialize(term,
|
145
|
+
def initialize(term, *args)
|
100
146
|
@term = term
|
101
|
-
@
|
102
|
-
@limit = limit
|
147
|
+
@args = args
|
103
148
|
end
|
104
149
|
end
|
105
150
|
```
|
106
151
|
|
107
152
|
```ruby
|
108
|
-
>> search = Search.new('foo',
|
109
|
-
>> search.
|
110
|
-
=>
|
153
|
+
>> search = Search.new('foo', 2, 10)
|
154
|
+
>> search.args
|
155
|
+
=> [2, 10]
|
156
|
+
```
|
157
|
+
|
158
|
+
***attr_accessor_init***: same as ```attr_reader_init```, but generates accessors for the given attributes
|
159
|
+
|
160
|
+
This code:
|
161
|
+
```ruby
|
162
|
+
class Search
|
163
|
+
attr_accessor_init :term, :page, :limit
|
164
|
+
end
|
165
|
+
```
|
166
|
+
|
167
|
+
Is equivalent to:
|
168
|
+
```ruby
|
169
|
+
class Search
|
170
|
+
attr_accessor :term, :page, :limit
|
171
|
+
|
172
|
+
def initialize(term, page, limit)
|
173
|
+
@term = term
|
174
|
+
@page = page
|
175
|
+
@limit = limit
|
176
|
+
end
|
177
|
+
end
|
111
178
|
```
|
112
179
|
|
113
180
|
***selfie***: can be used to automatically create a class method that reference to the instance method of the same class
|
@@ -174,36 +241,10 @@ class Search
|
|
174
241
|
end
|
175
242
|
```
|
176
243
|
|
177
|
-
## TL;DR
|
178
|
-
|
179
|
-
You write this:
|
180
|
-
```ruby
|
181
|
-
class Rectangle
|
182
|
-
attr_accessor_init :width, :height
|
183
|
-
selfie :area, :perimeter
|
184
|
-
|
185
|
-
def area
|
186
|
-
width * height
|
187
|
-
end
|
188
|
-
|
189
|
-
def perimeter
|
190
|
-
(width + height) * 2
|
191
|
-
end
|
192
|
-
end
|
193
|
-
```
|
194
|
-
|
195
|
-
You get this:
|
196
|
-
```ruby
|
197
|
-
>> Rectangle.area(8, 4)
|
198
|
-
=> 32
|
199
|
-
>> Rectangle.perimeter(5, 3)
|
200
|
-
=> 16
|
201
|
-
```
|
202
|
-
|
203
244
|
## Next Steps
|
204
245
|
|
205
246
|
***attr_reader_init*** and ***attr_accessor_init:***
|
206
|
-
-
|
247
|
+
- Improve message when raising ArgumentError;
|
207
248
|
|
208
249
|
***selfie:***
|
209
250
|
- Find a suitable syntax that would allow to 'selfie' an instance method that has arguments;
|
data/Rakefile
CHANGED
data/bin/console
CHANGED
@@ -1,7 +1,6 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
|
-
|
3
|
-
require
|
4
|
-
require "selfies"
|
2
|
+
require 'bundler/setup'
|
3
|
+
require 'selfies'
|
5
4
|
|
6
5
|
# You can add fixtures and/or initialization code here to make experimenting
|
7
6
|
# with your gem easier. You can also use a different console, if you like.
|
@@ -9,6 +8,5 @@ require "selfies"
|
|
9
8
|
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
9
|
# require "pry"
|
11
10
|
# Pry.start
|
12
|
-
|
13
|
-
require "irb"
|
11
|
+
require 'irb'
|
14
12
|
IRB.start(__FILE__)
|
data/lib/selfies/self_init.rb
CHANGED
@@ -18,16 +18,24 @@ module Selfies
|
|
18
18
|
|
19
19
|
variables.each_with_index do |variable, index|
|
20
20
|
variable_name, default = decouple(variable)
|
21
|
-
|
21
|
+
if variable_name == :args
|
22
|
+
instance_variable_set("@#{variable_name}", args[index..-1] || default)
|
23
|
+
else
|
24
|
+
instance_variable_set("@#{variable_name}", args[index] || default)
|
25
|
+
end
|
22
26
|
end
|
23
27
|
end
|
24
28
|
|
25
29
|
private_class_method
|
26
30
|
|
27
31
|
define_method(:correct_argument_count?) do |variables, expected, given|
|
32
|
+
|
28
33
|
correct_argument_count = given == expected
|
29
34
|
if variables.last.is_a? Hash
|
30
35
|
correct_argument_count ||= given == expected - 1
|
36
|
+
elsif variables.last == :args
|
37
|
+
at_least = variables[0..variables.index(:args)].count
|
38
|
+
correct_argument_count ||= given >= at_least
|
31
39
|
end
|
32
40
|
|
33
41
|
correct_argument_count
|
data/lib/selfies/version.rb
CHANGED