rfunk 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/rfunk/attribute.rb +37 -17
- data/lib/rfunk/attribute_type.rb +4 -0
- data/lib/rfunk/version.rb +1 -1
- data/lib/rfunk.rb +1 -0
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 19d4d78e7405dd0df4292227a632888722874ea1
|
4
|
+
data.tar.gz: a7c3e340bbc434670d8a524d8c30ff8a7923bab9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d74968aa0449b85a88e54e5bfc876cefe809937eb38a21c4e5b7038ce669de34aef258509f373bb1f4be2014d5e5fed1ffd71c464c89a42030b096340d58b6c3
|
7
|
+
data.tar.gz: 0ce78139b5124ee24939f52f2e58eea0ab963f642349e21827070d7b5d46d447b3f5dbb46fb5388eef05068e9df9e7cc011da7ef9259bf3913d1add235e942fc
|
data/lib/rfunk/attribute.rb
CHANGED
@@ -7,22 +7,13 @@ module RFunk
|
|
7
7
|
end
|
8
8
|
|
9
9
|
def initialize(options = {})
|
10
|
-
|
11
|
-
|
12
|
-
options.each { |key, value|
|
13
|
-
raise_not_found(key, attributes)
|
14
|
-
|
15
|
-
type = attributes[key]
|
16
|
-
|
17
|
-
raise_expected_type(key, value, type)
|
18
|
-
|
19
|
-
self.instance_variable_set(variable_name(key), value)
|
20
|
-
}
|
10
|
+
with_defaults
|
11
|
+
with_attributes(options)
|
21
12
|
end
|
22
13
|
|
23
14
|
module ClassMethods
|
24
|
-
def attribute(name, type)
|
25
|
-
add_attribute(name, type)
|
15
|
+
def attribute(name, type, options = {})
|
16
|
+
add_attribute(name, type, options)
|
26
17
|
|
27
18
|
self.send :define_method, name do |value = nil|
|
28
19
|
if value
|
@@ -36,15 +27,38 @@ module RFunk
|
|
36
27
|
|
37
28
|
private
|
38
29
|
|
39
|
-
def add_attribute(name, type)
|
30
|
+
def add_attribute(name, type, options)
|
40
31
|
attributes = self.instance_variable_get(ATTRIBUTES_VARIABLE_NAME) || {}
|
41
|
-
attributes[name] = type
|
32
|
+
attributes[name] = AttributeType.new(name, type, options)
|
42
33
|
self.instance_variable_set(ATTRIBUTES_VARIABLE_NAME, attributes)
|
43
34
|
end
|
44
35
|
end
|
45
36
|
|
46
37
|
private
|
47
38
|
|
39
|
+
def attributes
|
40
|
+
self.class.instance_variable_get(ATTRIBUTES_VARIABLE_NAME)
|
41
|
+
end
|
42
|
+
|
43
|
+
def with_defaults
|
44
|
+
attributes.each { |key, attribute|
|
45
|
+
value = attribute.options[:default]
|
46
|
+
set_variable(attribute, key, value) if value
|
47
|
+
}
|
48
|
+
end
|
49
|
+
|
50
|
+
def with_attributes(options)
|
51
|
+
options.each { |key, value|
|
52
|
+
raise_not_found(key, attributes)
|
53
|
+
set_variable(attributes[key], key, value)
|
54
|
+
}
|
55
|
+
end
|
56
|
+
|
57
|
+
def set_variable(attribute, key, value)
|
58
|
+
raise_expected_type(key, value, attribute.type)
|
59
|
+
self.instance_variable_set(variable_name(key), value)
|
60
|
+
end
|
61
|
+
|
48
62
|
def variable_name(name)
|
49
63
|
"@#{name}"
|
50
64
|
end
|
@@ -62,11 +76,17 @@ module RFunk
|
|
62
76
|
end
|
63
77
|
|
64
78
|
def raise_expected_type(name, value, type)
|
65
|
-
|
79
|
+
unless value.instance_of?(type)
|
80
|
+
message = "Expected a type of '#{type}' for attribute '#{name}'"
|
81
|
+
raise TypeError, message
|
82
|
+
end
|
66
83
|
end
|
67
84
|
|
68
85
|
def raise_not_found(key, attributes)
|
69
|
-
|
86
|
+
unless attributes.key?(key)
|
87
|
+
message = "Attribute with name '#{key}' does not exist. The only available attributes are '#{attributes.keys}'"
|
88
|
+
raise RFunk::NotFoundError, message
|
89
|
+
end
|
70
90
|
end
|
71
91
|
end
|
72
92
|
end
|
data/lib/rfunk/version.rb
CHANGED
data/lib/rfunk.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rfunk
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Alex Falkowski
|
@@ -61,6 +61,7 @@ extra_rdoc_files: []
|
|
61
61
|
files:
|
62
62
|
- lib/rfunk.rb
|
63
63
|
- lib/rfunk/attribute.rb
|
64
|
+
- lib/rfunk/attribute_type.rb
|
64
65
|
- lib/rfunk/either.rb
|
65
66
|
- lib/rfunk/failure.rb
|
66
67
|
- lib/rfunk/lazy.rb
|