selfies 0.5.0 → 1.0.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 85d6373067c8c9773cbf11a7c4a5cd4f522c3cde
4
- data.tar.gz: 03b21feacc2cb82878bc201ed807c2c83942d209
3
+ metadata.gz: 2229c9e8739933e8b2f76dc485ff084a193d7b97
4
+ data.tar.gz: 0cdae8225e70509672478da1e7cc65bf816a81be
5
5
  SHA512:
6
- metadata.gz: 408b2541b8760bcea0c5e8e48ae626279b0b1294b391fbbfc221a5f08511a33b91a060ef0df08b2798edae25e7e3a04dd13a90125945876241e247dc844a2178
7
- data.tar.gz: 656a2d54502b6353ab1b7c332b262ae964a7f34cfabcefbce1e3fcdef6b8eddae97e9ef814ad9541c6f407484487ff57204a0ec1399879e7d3d70a10eb4d7ca8
6
+ metadata.gz: 6d754742137a6cf137c9f7100bd49e4b49c3ad5c1ac157d4f4ef8da34d333a44055461305cd7147915c82a0aade10e0481b8287819944ec347510592a7878dae
7
+ data.tar.gz: b49bd26ddfaeaf4ddbf3f5aeccd656e2135c6003c07bf7dd13150c789294c41f22a48e2773ffd64c0b16c4dc3d1aa14449f8228c8e112e3390979ae5b3c10d6b
data/README.md CHANGED
@@ -249,6 +249,9 @@ end
249
249
  ***selfie:***
250
250
  - Find a suitable syntax that would allow to 'selfie' an instance method that has arguments;
251
251
 
252
+ ***command line:***
253
+ - Write a command that can initialize a whole class, from creating the file to stub the initial setup (idea by Luke Hill)
254
+
252
255
  ***more?:***
253
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.
254
257
 
data/lib/kernel.rb CHANGED
@@ -10,11 +10,6 @@ module Kernel
10
10
  Selfies.generate_initializer(self, false, *variable_names)
11
11
  end
12
12
 
13
- def self_init(*variable_names)
14
- attr_reader_init(*variable_names)
15
- end
16
- deprecate :self_init, :attr_reader_init, 2017, 7
17
-
18
13
  def selfie(*method_names)
19
14
  Selfies.generate_class_methods(self, *method_names)
20
15
  end
@@ -5,36 +5,35 @@ module Selfies
5
5
 
6
6
  class_name.class_eval do
7
7
  variable_names = SelfInit.variable_names(variables)
8
- if accessor
9
- attr_accessor *variable_names
10
- else
11
- attr_reader *variable_names
12
- end
8
+
9
+ SelfInit.access_variables(class_name, accessor, variable_names)
13
10
 
14
11
  define_method(:initialize) do |*args|
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
12
+ argument_count(variables, variable_names.count, args.count)
18
13
 
19
14
  variables.each_with_index do |variable, index|
20
15
  variable_name, default = decouple(variable)
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
16
+ value = variable_name == :args ? args[index..-1] : args[index]
17
+ instance_variable_set("@#{variable_name}", value || default)
26
18
  end
27
19
  end
28
20
 
29
21
  private_class_method
30
22
 
31
- define_method(:correct_argument_count?) do |variables, expected, given|
23
+ define_method(:argument_count) do |all_variables, expected, given|
24
+ unless correct_argument_count?(all_variables, expected, given)
25
+ raise ArgumentError,
26
+ "wrong number of arguments (given #{given}, expected #{expected})"
27
+ end
28
+ end
32
29
 
30
+ define_method(:correct_argument_count?) do |all_variables, expected, given|
31
+ final_variable = all_variables.last
33
32
  correct_argument_count = given == expected
34
- if variables.last.is_a? Hash
33
+ if final_variable.is_a? Hash
35
34
  correct_argument_count ||= given == expected - 1
36
- elsif variables.last == :args
37
- at_least = variables[0..variables.index(:args)].count
35
+ elsif final_variable == :args
36
+ at_least = all_variables[0..all_variables.index(:args)].count
38
37
  correct_argument_count ||= given >= at_least
39
38
  end
40
39
 
@@ -42,7 +41,7 @@ module Selfies
42
41
  end
43
42
 
44
43
  define_method(:decouple) do |variable|
45
- return [variable, nil] if !variable.is_a? Hash
44
+ return [variable, nil] unless variable.is_a? Hash
46
45
 
47
46
  variable.keys + variable.values
48
47
  end
@@ -51,6 +50,13 @@ module Selfies
51
50
 
52
51
  private_class_method
53
52
 
53
+ def self.access_variables(class_name, accessor, variable_names)
54
+ class_name.send(
55
+ (accessor ? :attr_accessor : :attr_reader),
56
+ *(variable_names)
57
+ )
58
+ end
59
+
54
60
  def self.variable_names(variables)
55
61
  variables.collect do |variable|
56
62
  variable.is_a?(Hash) ? variable.keys.first : variable
@@ -1,3 +1,3 @@
1
1
  module Selfies
2
- VERSION = '0.5.0'
2
+ VERSION = '1.0.0'.freeze
3
3
  end
data/selfies.gemspec CHANGED
@@ -1,5 +1,6 @@
1
1
  # coding: utf-8
2
- lib = File.expand_path("../lib", __FILE__)
2
+
3
+ lib = File.expand_path('../lib', __FILE__)
3
4
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
5
  require 'selfies/version'
5
6
 
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.5.0
4
+ version: 1.0.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-30 00:00:00.000000000 Z
11
+ date: 2017-07-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler