selfies 0.2.0 → 0.3.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: 8ba49d8320ff6acedb3e41fb564e3566333b50ca
4
- data.tar.gz: 2ab1f3809213567f3509b864670d349ea1d35d09
3
+ metadata.gz: 47550e2d9e6da031803c5ba23ef7d4336f0e3076
4
+ data.tar.gz: c05c0beae48f5d515d65d41c1fab06752cbcf5fe
5
5
  SHA512:
6
- metadata.gz: 6fe532606d20e991b83d8eed8240b91fdfbfde4db16fa096b270fa933539331407a753f324daaef7912387561743b81d953a9ff15ea547c179ee5c628b6c2bfc
7
- data.tar.gz: 29e8d0793f74a06ffc3a138aa94ef67a50f2632d93bc32fd9eab06c1cf8dc6ab681fb6dbd3c8d19bee557903afd2ac05e1f5afd78c7be7c55c823914e03b4d50
6
+ metadata.gz: 26ecd3374676bb10adc4e4c0e2f01dcd5cb1201fcd2212b0f7fa8dc3b48ab9ea753a3371642eb88c5961996a9fab593a82cc64daa1838f94143887e95e0def4b
7
+ data.tar.gz: c1516a43e5384a7809a99e13d4ced86091034afbd01f52fd5bb2b36bb2faf851e3d8e71c290402db569f1f774130a291340849ced93818686638394b7584f81f
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- [![CircleCI](https://circleci.com/gh/mariodarco/selfies/tree/master.svg?style=shield)](https://circleci.com/gh/mariodarco/selfies/tree/master)
1
+ [![Gem Version](https://badge.fury.io/rb/selfies.svg)](https://badge.fury.io/rb/selfies) [![CircleCI](https://circleci.com/gh/mariodarco/selfies/tree/master.svg?style=shield)](https://circleci.com/gh/mariodarco/selfies/tree/master)
2
2
 
3
3
  # Selfies
4
4
  **A collection of macros for quicker development**
@@ -38,12 +38,12 @@ $ gem install selfies
38
38
 
39
39
  ## Usage
40
40
 
41
- ***self_init***: can be used to automatically generate an initialiser for your class
41
+ ***attr_reader_init***: can be used to automatically generate an initialiser for your class
42
42
 
43
43
  This code:
44
44
  ```ruby
45
45
  class Search
46
- self_init :term, :page, :limit
46
+ attr_reader_init :term, :page, :limit
47
47
  end
48
48
  ```
49
49
 
@@ -60,6 +60,28 @@ class Search
60
60
  end
61
61
  ```
62
62
 
63
+ ***attr_accessor_init***: same as ```attr_reader_init```, but generates accessors for the given attributes
64
+
65
+ This code:
66
+ ```ruby
67
+ class Search
68
+ attr_accessor_init :term, :page, :limit
69
+ end
70
+ ```
71
+
72
+ Is equivalent to:
73
+ ```ruby
74
+ class Search
75
+ attr_accessor :term, :page, :limit
76
+
77
+ def initialize(term, page, limit)
78
+ @term = term
79
+ @page = page
80
+ @limit = limit
81
+ end
82
+ end
83
+ ```
84
+
63
85
  ***selfie***: can be used to automatically create a class method that reference to the instance method of the same class
64
86
 
65
87
  This code:
@@ -94,7 +116,7 @@ end
94
116
  Can be written as:
95
117
  ```ruby
96
118
  class Search
97
- self_init :term, :page, :limit
119
+ attr_reader_init :term, :page, :limit
98
120
 
99
121
  def execute!
100
122
  # does something
@@ -130,7 +152,7 @@ end
130
152
  You write this:
131
153
  ```ruby
132
154
  class Rectangle
133
- self_init :width, :height
155
+ attr_accessor_init :width, :height
134
156
  selfie :area, :perimeter
135
157
 
136
158
  def area
@@ -153,13 +175,10 @@ You get this:
153
175
 
154
176
  ## Next Steps
155
177
 
156
- ***self_init:***
178
+ ***attr_reader_init*** and ***attr_accessor_init:***
157
179
  - Implement the possibility to pass defaults;
158
- - Specify which parameters will get an attr_reader, attr_accessor or none;
159
- - Specify wich parameters on attr_reader are to consider private;
160
180
 
161
181
  ***selfie:***
162
- - Allow the possibility to declare all selfies at the top (if possible);
163
182
  - Find a suitable syntax that would allow to 'selfie' an instance method that has arguments;
164
183
 
165
184
  ***more?:***
data/lib/kernel.rb CHANGED
@@ -1,8 +1,19 @@
1
1
  # Open the Kernel modul to define methods for any class.
2
2
  module Kernel
3
+ extend Gem::Deprecate
4
+
5
+ def attr_accessor_init(*variable_names)
6
+ Selfies.generate_initializer(self, true, *variable_names)
7
+ end
8
+
9
+ def attr_reader_init(*variable_names)
10
+ Selfies.generate_initializer(self, false, *variable_names)
11
+ end
12
+
3
13
  def self_init(*variable_names)
4
- Selfies.generate_initializer(self, *variable_names)
14
+ attr_reader_init(*variable_names)
5
15
  end
16
+ deprecate :self_init, :attr_reader_init, 2017, 7
6
17
 
7
18
  def selfie(*method_names)
8
19
  Selfies.generate_class_methods(self, *method_names)
@@ -1,23 +1,29 @@
1
1
  module Selfies
2
2
  class SelfInit
3
- def self.generate(class_name, *variable_names)
3
+ def self.generate(class_name, accessor, *variable_names)
4
4
  return false unless variable_names.any?
5
5
 
6
- class_name.class_eval do
7
- attr_reader *variable_names
6
+ class_name.class_eval do
7
+ if accessor
8
+ attr_accessor *variable_names
9
+ else
10
+ attr_reader *variable_names
11
+ end
8
12
 
9
13
  define_method(:initialize) do |*args|
10
- argument_check(variable_names.count, args.count)
14
+ SelfInit.argument_check(variable_names.count, args.count)
11
15
 
12
16
  variable_names.each_with_index do |variable, index|
13
17
  instance_variable_set("@#{variable}", args[index])
14
18
  end
15
19
  end
16
-
17
- def argument_check(expected, given)
18
- raise ArgumentError, "wrong number of arguments (given #{given}, expected #{expected})" unless given == expected
19
- end
20
20
  end
21
21
  end
22
+
23
+ private_class_method
24
+
25
+ def self.argument_check(expected, given)
26
+ raise ArgumentError, "wrong number of arguments (given #{given}, expected #{expected})" unless given == expected
27
+ end
22
28
  end
23
29
  end
@@ -1,4 +1,5 @@
1
1
  module Selfies
2
+ # Generates a class method for any described instance methods.
2
3
  class Selfie
3
4
  def self.generate(class_name, *method_names)
4
5
  class_name.class_eval do
@@ -1,3 +1,3 @@
1
1
  module Selfies
2
- VERSION = '0.2.0'
2
+ VERSION = '0.3.0'
3
3
  end
data/lib/selfies.rb CHANGED
@@ -5,8 +5,8 @@ require 'selfies/version'
5
5
 
6
6
  # Index of the Selfies utilities
7
7
  module Selfies
8
- def self.generate_initializer(class_name, *variable_names)
9
- SelfInit.generate(class_name, *variable_names)
8
+ def self.generate_initializer(class_name, accessor, *variable_names)
9
+ SelfInit.generate(class_name, accessor, *variable_names)
10
10
  end
11
11
 
12
12
  def self.generate_class_methods(class_name, *method_names)
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.2.0
4
+ version: 0.3.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-23 00:00:00.000000000 Z
11
+ date: 2017-06-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler