rfunk 0.1.0 → 0.2.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: c539b96a759a551a6d9fbb57f06415dc09d0cb26
4
- data.tar.gz: b5f8bb018a680fcfff0efcc0b18f9e32f4ba4dd5
3
+ metadata.gz: 19d4d78e7405dd0df4292227a632888722874ea1
4
+ data.tar.gz: a7c3e340bbc434670d8a524d8c30ff8a7923bab9
5
5
  SHA512:
6
- metadata.gz: 292cf72c0c5f89b49d368453f5183b0063da281ee86531832d07c61183336fb2dae11604a463f4efd19b7671e5e61ec0df9eb2a923152a15d09b6692e9bd82b2
7
- data.tar.gz: 4b0b4dae55ee2f40a9a9a79f336a3f8cec5711058d55bec443b0be45e3afecde0ac45cb4053800e95e5820918a67d6a35bcf8ac82d6ef436873de71b61ebe33b
6
+ metadata.gz: d74968aa0449b85a88e54e5bfc876cefe809937eb38a21c4e5b7038ce669de34aef258509f373bb1f4be2014d5e5fed1ffd71c464c89a42030b096340d58b6c3
7
+ data.tar.gz: 0ce78139b5124ee24939f52f2e58eea0ab963f642349e21827070d7b5d46d447b3f5dbb46fb5388eef05068e9df9e7cc011da7ef9259bf3913d1add235e942fc
@@ -7,22 +7,13 @@ module RFunk
7
7
  end
8
8
 
9
9
  def initialize(options = {})
10
- attributes = self.class.instance_variable_get(ATTRIBUTES_VARIABLE_NAME)
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
- raise TypeError, "Expected a type of '#{type}' for attribute '#{name}'" unless value.instance_of?(type)
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
- raise RFunk::NotFoundError, "Attribute with name '#{key}' does not exist. The only available attributes are '#{attributes}'" unless attributes.key?(key)
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
@@ -0,0 +1,4 @@
1
+ module RFunk
2
+ class AttributeType < Struct.new(:name, :type, :options)
3
+ end
4
+ end
data/lib/rfunk/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module RFunk
2
- VERSION = '0.1.0'
2
+ VERSION = '0.2.0'
3
3
  end
data/lib/rfunk.rb CHANGED
@@ -2,6 +2,7 @@ require 'singleton'
2
2
  require 'ice_nine'
3
3
  require 'ice_nine/core_ext/object'
4
4
 
5
+ require 'rfunk/attribute_type'
5
6
  require 'rfunk/attribute'
6
7
  require 'rfunk/option'
7
8
  require 'rfunk/none'
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.1.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