selfies 0.3.0 → 0.4.0

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: 47550e2d9e6da031803c5ba23ef7d4336f0e3076
4
- data.tar.gz: c05c0beae48f5d515d65d41c1fab06752cbcf5fe
3
+ metadata.gz: d2779a51d2de784eefdf3f45ceea218f7d6fb7cc
4
+ data.tar.gz: 036c1e49e616d5417bcb13ede00d67ee2881f111
5
5
  SHA512:
6
- metadata.gz: 26ecd3374676bb10adc4e4c0e2f01dcd5cb1201fcd2212b0f7fa8dc3b48ab9ea753a3371642eb88c5961996a9fab593a82cc64daa1838f94143887e95e0def4b
7
- data.tar.gz: c1516a43e5384a7809a99e13d4ced86091034afbd01f52fd5bb2b36bb2faf851e3d8e71c290402db569f1f774130a291340849ced93818686638394b7584f81f
6
+ metadata.gz: 7ab254cd4b2e08f68f78507a566d3a54bd67ba27f7ffc280881166dd5589310b69ca93fc44d8fbd53c6e4d80f746adde3071ec0b33800eec160468d4fdba5a84
7
+ data.tar.gz: 01bce53ae70ed2faa87191dc6c58a57331d5131463d3e35efbbba8e51385ea833307d232b16acf0d19252bb9fcb7087130ffb965322aaac312ef6af81c957b6d
data/README.md CHANGED
@@ -82,6 +82,34 @@ class Search
82
82
  end
83
83
  ```
84
84
 
85
+ It is possible to specify a default for the last argument only:
86
+
87
+ This code:
88
+ ```ruby
89
+ class Search
90
+ attr_accessor_init :term, :page, limit: 5
91
+ end
92
+ ```
93
+
94
+ Is equivalent to:
95
+ ```ruby
96
+ class Search
97
+ attr_accessor :term, :page, :limit
98
+
99
+ def initialize(term, page, limit = 5)
100
+ @term = term
101
+ @page = page
102
+ @limit = limit
103
+ end
104
+ end
105
+ ```
106
+
107
+ ```ruby
108
+ >> search = Search.new('foo', 1)
109
+ >> search.limit
110
+ => 5
111
+ ```
112
+
85
113
  ***selfie***: can be used to automatically create a class method that reference to the instance method of the same class
86
114
 
87
115
  This code:
@@ -131,7 +159,6 @@ end
131
159
  ```
132
160
 
133
161
  If preferred, more methods can be 'selfied' in one liner:
134
-
135
162
  ```ruby
136
163
  class Search
137
164
  ...
@@ -176,7 +203,7 @@ You get this:
176
203
  ## Next Steps
177
204
 
178
205
  ***attr_reader_init*** and ***attr_accessor_init:***
179
- - Implement the possibility to pass defaults;
206
+ - Implement the possibility to pass indefinite number of arguments;
180
207
 
181
208
  ***selfie:***
182
209
  - Find a suitable syntax that would allow to 'selfie' an instance method that has arguments;
@@ -1,9 +1,10 @@
1
1
  module Selfies
2
2
  class SelfInit
3
- def self.generate(class_name, accessor, *variable_names)
4
- return false unless variable_names.any?
3
+ def self.generate(class_name, accessor, *variables)
4
+ return false unless variables.any?
5
5
 
6
6
  class_name.class_eval do
7
+ variable_names = SelfInit.variable_names(variables)
7
8
  if accessor
8
9
  attr_accessor *variable_names
9
10
  else
@@ -11,19 +12,41 @@ module Selfies
11
12
  end
12
13
 
13
14
  define_method(:initialize) do |*args|
14
- SelfInit.argument_check(variable_names.count, args.count)
15
+ unless correct_argument_count?(variables, variable_names.count, args.count)
16
+ raise ArgumentError, "wrong number of arguments (given #{args.count}, expected #{variable_names.count})"
17
+ end
18
+
19
+ variables.each_with_index do |variable, index|
20
+ variable_name, default = decouple(variable)
21
+ instance_variable_set("@#{variable_name}", args[index] || default)
22
+ end
23
+ end
24
+
25
+ private_class_method
15
26
 
16
- variable_names.each_with_index do |variable, index|
17
- instance_variable_set("@#{variable}", args[index])
27
+ define_method(:correct_argument_count?) do |variables, expected, given|
28
+ correct_argument_count = given == expected
29
+ if variables.last.is_a? Hash
30
+ correct_argument_count ||= given == expected - 1
18
31
  end
32
+
33
+ correct_argument_count
34
+ end
35
+
36
+ define_method(:decouple) do |variable|
37
+ return [variable, nil] if !variable.is_a? Hash
38
+
39
+ variable.keys + variable.values
19
40
  end
20
41
  end
21
42
  end
22
43
 
23
44
  private_class_method
24
45
 
25
- def self.argument_check(expected, given)
26
- raise ArgumentError, "wrong number of arguments (given #{given}, expected #{expected})" unless given == expected
46
+ def self.variable_names(variables)
47
+ variables.collect do |variable|
48
+ variable.is_a?(Hash) ? variable.keys.first : variable
49
+ end
27
50
  end
28
51
  end
29
52
  end
@@ -1,3 +1,3 @@
1
1
  module Selfies
2
- VERSION = '0.3.0'
2
+ VERSION = '0.4.0'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: selfies
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mario D’Arco
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-06-25 00:00:00.000000000 Z
11
+ date: 2017-06-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler