attire 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +3 -2
- data/lib/attire/attr_init.rb +23 -3
- data/lib/attire/initializer.rb +17 -2
- metadata +8 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6534d8adf28c8358c5da8bec3a8b98191335a640
|
4
|
+
data.tar.gz: 6a82d12b2d96b8644de663e072741dc6d41832f6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1c49f34f6c80ab98424ecb603222d5dd0e97e7762e0fc7be6a50ba590eeef4dbf3d1a27f42de6d89152957e873bc0e00aef19e07c37cbd3fd0afed086f26dd03
|
7
|
+
data.tar.gz: 41cb44dddd2db57edda7e5a7fbc720beb65f9175bab7362b22024169a9024669e602c30b0bd3b85837c2ae59eb5f69e9253e0f43d2691e70d5166df8cc6be949
|
data/README.md
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
# Attire
|
2
2
|
|
3
|
+
[![Build Status](https://travis-ci.org/mushishi78/attire.svg?branch=master)](https://travis-ci.org/mushishi78/attire)
|
3
4
|
[![Gem Version](https://badge.fury.io/rb/attire.svg)](http://badge.fury.io/rb/attire)
|
4
5
|
|
5
6
|
Convenience methods to remove some boiler plate in defining classes. Inspired by [attr_extras](https://github.com/barsoom/attr_extras).
|
@@ -23,10 +24,10 @@ private
|
|
23
24
|
attr_reader :foo, :bar, :fizz, :pop
|
24
25
|
```
|
25
26
|
|
26
|
-
|
27
|
+
Optional, splat and blocks arguments can also be defined:
|
27
28
|
|
28
29
|
``` ruby
|
29
|
-
attr_init :'*args', :'&block'
|
30
|
+
attr_init :'opts = {}', :'*args', :'&block'
|
30
31
|
```
|
31
32
|
|
32
33
|
If a block is provided, it will be evaluated after initialization:
|
data/lib/attire/attr_init.rb
CHANGED
@@ -13,6 +13,7 @@ module Attire
|
|
13
13
|
def apply
|
14
14
|
type_check
|
15
15
|
extract_splat_and_block_names
|
16
|
+
optional_arguments_check
|
16
17
|
instance_initialize = initializer.method(:instance_initialize)
|
17
18
|
|
18
19
|
klass.send(:define_method, :initialize) do |*values, &value_block|
|
@@ -27,8 +28,8 @@ module Attire
|
|
27
28
|
attr_reader :klass, :names, :splat_name, :block_name, :after_initialize
|
28
29
|
|
29
30
|
def type_check
|
30
|
-
return if names.all? { |n| [Symbol, Hash].include?(n.class) }
|
31
|
-
fail ArgumentError, 'Must be Symbol or Hash.'
|
31
|
+
return if names.all? { |n| [Symbol, String, Hash].include?(n.class) }
|
32
|
+
fail ArgumentError, 'Must be Symbol, String or Hash.'
|
32
33
|
end
|
33
34
|
|
34
35
|
def extract_splat_and_block_names
|
@@ -49,7 +50,11 @@ module Attire
|
|
49
50
|
end
|
50
51
|
|
51
52
|
def getter_names
|
52
|
-
getter_names = names.map
|
53
|
+
getter_names = names.map do |arg|
|
54
|
+
next arg.keys if arg.respond_to?(:keys)
|
55
|
+
next optional_name(arg) if optional?(arg)
|
56
|
+
arg
|
57
|
+
end
|
53
58
|
getter_names += [splat_name, block_name]
|
54
59
|
getter_names.flatten.compact
|
55
60
|
end
|
@@ -70,5 +75,20 @@ module Attire
|
|
70
75
|
return if names.none? { |n| n.to_s.start_with?('&', '*') }
|
71
76
|
fail ArgumentError, 'Splat and Block arguments must be last'
|
72
77
|
end
|
78
|
+
|
79
|
+
def optional_arguments_check
|
80
|
+
start = names.find_index { |n| optional?(n) }
|
81
|
+
return unless start
|
82
|
+
return if names[start..-1].all? { |n| n.is_a?(Hash) || optional?(n) }
|
83
|
+
fail ArgumentError, 'Required arguments must come before optional'
|
84
|
+
end
|
85
|
+
|
86
|
+
def optional?(name)
|
87
|
+
name.to_s.include?('=')
|
88
|
+
end
|
89
|
+
|
90
|
+
def optional_name(name)
|
91
|
+
name.to_s.split('=')[0].strip
|
92
|
+
end
|
73
93
|
end
|
74
94
|
end
|
data/lib/attire/initializer.rb
CHANGED
@@ -21,7 +21,9 @@ module Attire
|
|
21
21
|
|
22
22
|
def set_ivars
|
23
23
|
names.zip(values).each do |name, value|
|
24
|
-
|
24
|
+
next set_hash(name, value) if name.is_a?(Hash)
|
25
|
+
next set_optional(name, value) if optional?(name)
|
26
|
+
set_ivar(name, value)
|
25
27
|
end
|
26
28
|
set_splat
|
27
29
|
set_block
|
@@ -35,6 +37,12 @@ module Attire
|
|
35
37
|
end
|
36
38
|
end
|
37
39
|
|
40
|
+
def set_optional(name, value)
|
41
|
+
name, default = name.to_s.split('=').map(&:strip)
|
42
|
+
value = eval(default) if value.nil?
|
43
|
+
set_ivar(name, value)
|
44
|
+
end
|
45
|
+
|
38
46
|
def set_splat
|
39
47
|
return unless splat_name
|
40
48
|
value_splat = values[names.length..values.length] || []
|
@@ -60,7 +68,10 @@ module Attire
|
|
60
68
|
end
|
61
69
|
|
62
70
|
def min_arity
|
63
|
-
|
71
|
+
last_required = names.reverse_each.find_index do |n|
|
72
|
+
!(optional?(n) || n.is_a?(Hash))
|
73
|
+
end
|
74
|
+
last_required ? names.length - last_required : 0
|
64
75
|
end
|
65
76
|
|
66
77
|
def max_arity
|
@@ -70,5 +81,9 @@ module Attire
|
|
70
81
|
def arity_range
|
71
82
|
@arity_range ||= (min_arity..max_arity)
|
72
83
|
end
|
84
|
+
|
85
|
+
def optional?(name)
|
86
|
+
name.to_s.include?('=')
|
87
|
+
end
|
73
88
|
end
|
74
89
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: attire
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Max White
|
@@ -14,20 +14,20 @@ dependencies:
|
|
14
14
|
name: rspec
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- - ~>
|
17
|
+
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
19
|
version: '3.1'
|
20
|
-
- -
|
20
|
+
- - ">="
|
21
21
|
- !ruby/object:Gem::Version
|
22
22
|
version: 3.1.0
|
23
23
|
type: :development
|
24
24
|
prerelease: false
|
25
25
|
version_requirements: !ruby/object:Gem::Requirement
|
26
26
|
requirements:
|
27
|
-
- - ~>
|
27
|
+
- - "~>"
|
28
28
|
- !ruby/object:Gem::Version
|
29
29
|
version: '3.1'
|
30
|
-
- -
|
30
|
+
- - ">="
|
31
31
|
- !ruby/object:Gem::Version
|
32
32
|
version: 3.1.0
|
33
33
|
description:
|
@@ -51,17 +51,17 @@ require_paths:
|
|
51
51
|
- lib
|
52
52
|
required_ruby_version: !ruby/object:Gem::Requirement
|
53
53
|
requirements:
|
54
|
-
- -
|
54
|
+
- - ">="
|
55
55
|
- !ruby/object:Gem::Version
|
56
56
|
version: '0'
|
57
57
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
58
58
|
requirements:
|
59
|
-
- -
|
59
|
+
- - ">="
|
60
60
|
- !ruby/object:Gem::Version
|
61
61
|
version: '0'
|
62
62
|
requirements: []
|
63
63
|
rubyforge_project:
|
64
|
-
rubygems_version: 2.
|
64
|
+
rubygems_version: 2.2.2
|
65
65
|
signing_key:
|
66
66
|
specification_version: 4
|
67
67
|
summary: Convenience methods to remove some boiler plate in defining classes.
|