selfies 1.2.2 → 1.2.3
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/README.md +2 -2
- data/lib/selfies/self_init.rb +31 -29
- data/lib/selfies/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 542d2ed712992bff31b812fe7f1fce625e603cca
|
4
|
+
data.tar.gz: 731f48a656f0d3817792c5751581555f5cf6d4b4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: de4fc0461686a7f5ea01b802288193c29a1fd5f5ba5eee929661ff062ae6db2456b02a726d5ea1cab9cd6726baa68c7465deea6c530f81c706d86aeae074919e
|
7
|
+
data.tar.gz: 5124a761f0f2760eaca91e593cf764e0a2ec36800aa1218adb0e705b398c1ccb98d90588f1fc16cbd487c7b087e58ae222b63b76270c5adb1711a10df9e62bac
|
data/README.md
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
[](https://badge.fury.io/rb/selfies) [](https://gemnasium.com/github.com/mariodarco/selfies) [](https://circleci.com/gh/mariodarco/selfies/tree/master)
|
1
|
+
[](https://badge.fury.io/rb/selfies) [](https://gemnasium.com/github.com/mariodarco/selfies) [](https://circleci.com/gh/mariodarco/selfies/tree/master) [](https://codeclimate.com/github/mariodarco/selfies/maintainability) [](https://codeclimate.com/github/mariodarco/selfies/test_coverage)
|
2
2
|
|
3
3
|
# Selfies
|
4
4
|
**A collection of macros for quicker development**
|
@@ -250,7 +250,7 @@ end
|
|
250
250
|
- Find a suitable syntax that would allow to 'selfie' an instance method that has arguments;
|
251
251
|
|
252
252
|
***command line:***
|
253
|
-
- Write a command that can initialize a whole class, from creating the file to stub the initial setup
|
253
|
+
- Write a command that can initialize a whole class, from creating the file to stub the initial setup;
|
254
254
|
|
255
255
|
***more?:***
|
256
256
|
- If you also often write repetitive bolierplate, and would like some code to get smaller, drop me a line and we might be able to add more macros.
|
data/lib/selfies/self_init.rb
CHANGED
@@ -9,50 +9,52 @@ module Selfies
|
|
9
9
|
SelfInit.access_variables(class_name, accessor, variable_names)
|
10
10
|
|
11
11
|
define_method(:initialize) do |*args|
|
12
|
-
|
12
|
+
SelfInit.argument_guard(variables, variable_names.count, args.count)
|
13
13
|
|
14
14
|
variables.each_with_index do |variable, index|
|
15
|
-
variable_name, default = decouple(variable)
|
15
|
+
variable_name, default = SelfInit.decouple(variable)
|
16
16
|
value = variable_name == :args ? args[index..-1] : args[index]
|
17
17
|
instance_variable_set("@#{variable_name}", value || default)
|
18
18
|
end
|
19
19
|
end
|
20
|
-
|
21
|
-
define_method(:argument_count) do |all_variables, expected, given|
|
22
|
-
unless correct_argument_count?(all_variables, expected, given)
|
23
|
-
raise ArgumentError,
|
24
|
-
"wrong number of arguments (given #{given}, expected #{expected})"
|
25
|
-
end
|
26
|
-
end
|
27
|
-
|
28
|
-
define_method(:correct_argument_count?) do |all_variables, expected, given|
|
29
|
-
final_variable = all_variables.last
|
30
|
-
correct_argument_count = given == expected
|
31
|
-
if final_variable.is_a? Hash
|
32
|
-
correct_argument_count ||= given == expected - 1
|
33
|
-
elsif final_variable == :args
|
34
|
-
at_least = all_variables[0..all_variables.index(:args)].count
|
35
|
-
correct_argument_count ||= given >= at_least
|
36
|
-
end
|
37
|
-
|
38
|
-
correct_argument_count
|
39
|
-
end
|
40
|
-
|
41
|
-
define_method(:decouple) do |variable|
|
42
|
-
return [variable, nil] unless variable.is_a? Hash
|
43
|
-
|
44
|
-
variable.keys + variable.values
|
45
|
-
end
|
46
20
|
end
|
47
21
|
end
|
48
22
|
|
49
23
|
def self.access_variables(class_name, accessor, variable_names)
|
50
24
|
class_name.send(
|
51
25
|
(accessor ? :attr_accessor : :attr_reader),
|
52
|
-
*
|
26
|
+
*variable_names
|
53
27
|
)
|
54
28
|
end
|
55
29
|
|
30
|
+
def self.argument_guard(all_variables, expected, given)
|
31
|
+
unless correct_argument_count?(all_variables, expected, given)
|
32
|
+
raise(
|
33
|
+
ArgumentError,
|
34
|
+
"wrong number of arguments (given #{given}, expected #{expected})"
|
35
|
+
)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def self.correct_argument_count?(all_variables, expected, given)
|
40
|
+
final_variable = all_variables.last
|
41
|
+
correct_argument_count = given == expected
|
42
|
+
if final_variable.is_a? Hash
|
43
|
+
correct_argument_count ||= given == expected - 1
|
44
|
+
elsif final_variable == :args
|
45
|
+
at_least = all_variables[0..all_variables.index(:args)].count
|
46
|
+
correct_argument_count ||= given >= at_least
|
47
|
+
end
|
48
|
+
|
49
|
+
correct_argument_count
|
50
|
+
end
|
51
|
+
|
52
|
+
def self.decouple(variable)
|
53
|
+
return [variable, nil] unless variable.is_a? Hash
|
54
|
+
|
55
|
+
variable.keys + variable.values
|
56
|
+
end
|
57
|
+
|
56
58
|
def self.variable_names(variables)
|
57
59
|
variables.collect do |variable|
|
58
60
|
variable.is_a?(Hash) ? variable.keys.first : variable
|
data/lib/selfies/version.rb
CHANGED
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: 1.2.
|
4
|
+
version: 1.2.3
|
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-
|
11
|
+
date: 2017-10-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|