rfunk 0.3.0 → 0.4.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: 67d0f85734d6898bb2d564a59f4e3ec06e3e32b9
4
- data.tar.gz: 5ff36ad9b6ee2c6fae4e9fc8fa10f19eb5135679
3
+ metadata.gz: 3c7fa7d2d0ae8bb59de9d14f9393c7fc99c9225e
4
+ data.tar.gz: 617c897cd4843dd3ef0782b40f20c70edd4ecf7d
5
5
  SHA512:
6
- metadata.gz: 904998816f7d080cadd75e274e9a4f1321ee396d69b9c4d84e6f2ea96179018260f4a50b8fa2fb9c385b30655f80158edf1295e617a09e925380ec9f1440ff9b
7
- data.tar.gz: a7f3a9ba4847578253c7e53357d5f1be431f05ca2073019bf8fc8f3cc23271f7820d00c6e5044de1ed9ecbdcb8a93823687b9766854b9dd32aee63efd5c64c22
6
+ metadata.gz: 036a43078df1545a68c123d85b53b4800009c1a7f4242e2959aa288925fa74b5b5df213243b883557db40d64703c0c0e3ba24d18481aee59a0836233f838300a
7
+ data.tar.gz: c824e1f69c3dff059959e22f830aa886d06e822648afdcc8c659612b2cb9a066a97ecf0ca749a7a84867fda7982480252b06d153694746c2c929e66e5b4fdcc4
data/lib/rfunk.rb CHANGED
@@ -2,17 +2,21 @@ require 'singleton'
2
2
  require 'ice_nine'
3
3
  require 'ice_nine/core_ext/object'
4
4
 
5
+ require 'rfunk/attribute/immutable'
6
+ require 'rfunk/attribute/not_found_error'
7
+ require 'rfunk/attribute/immutable_error'
8
+ require 'rfunk/attribute/attribute_variable'
9
+ require 'rfunk/attribute/error_checking'
5
10
  require 'rfunk/attribute/attribute_type'
11
+ require 'rfunk/attribute/variable'
6
12
  require 'rfunk/attribute/attribute'
7
13
  require 'rfunk/maybe/option'
8
14
  require 'rfunk/maybe/none'
9
15
  require 'rfunk/maybe/some'
10
- require 'rfunk/attribute/not_found_error'
11
16
  require 'rfunk/lazy'
12
- require 'rfunk/either/value'
17
+ require 'rfunk/either/either'
13
18
  require 'rfunk/either/failure'
14
19
  require 'rfunk/either/success'
15
- require 'rfunk/either/either'
16
20
  require 'rfunk/tuple'
17
21
 
18
22
  include RFunk
@@ -1,6 +1,6 @@
1
1
  module RFunk
2
2
  module Attribute
3
- ATTRIBUTES_VARIABLE_NAME = '@attributes'
3
+ include RFunk::Variable
4
4
 
5
5
  def self.included(base)
6
6
  base.extend(ClassMethods)
@@ -13,31 +13,28 @@ module RFunk
13
13
 
14
14
  module ClassMethods
15
15
  def attribute(name, type, options = {})
16
- add_attribute(name, type, options)
16
+ AttributeVariable.new.add(instance: self,
17
+ name: name,
18
+ type: type,
19
+ options: options)
17
20
 
18
- self.send :define_method, name do |value = nil|
21
+ define_method(name) { |value = nil|
19
22
  if value
20
- raise_expected_type(name, value, type)
21
- create_immutable(variable_name(name), value)
23
+ ErrorChecking.new.raise_expected_type(name, value, type)
24
+ Immutable.new.create(instance: self,
25
+ variable_name: variable_name(name),
26
+ value: value)
22
27
  else
23
28
  Option(self.instance_variable_get(variable_name(name)))
24
29
  end
25
- end
26
- end
27
-
28
- private
29
-
30
- def add_attribute(name, type, options)
31
- attributes = self.instance_variable_get(ATTRIBUTES_VARIABLE_NAME) || {}
32
- attributes[name] = AttributeType.new(name, type, options)
33
- self.instance_variable_set(ATTRIBUTES_VARIABLE_NAME, attributes)
30
+ }
34
31
  end
35
32
  end
36
33
 
37
34
  private
38
35
 
39
36
  def attributes
40
- self.class.instance_variable_get(ATTRIBUTES_VARIABLE_NAME)
37
+ AttributeVariable.new.attributes(self.class)
41
38
  end
42
39
 
43
40
  def with_defaults
@@ -49,44 +46,18 @@ module RFunk
49
46
 
50
47
  def with_attributes(options)
51
48
  options.each { |key, value|
52
- raise_not_found(key, attributes)
49
+ ErrorChecking.new.raise_not_found(key, attributes)
53
50
  set_variable(attributes[key], key, value)
54
51
  }
55
52
  end
56
53
 
57
54
  def set_variable(attribute, key, value)
58
- raise_expected_type(key, value, attribute.type)
55
+ ErrorChecking.new.raise_expected_type(key, value, attribute.type)
59
56
  self.instance_variable_set(variable_name(key), value)
60
57
  end
61
58
 
62
59
  def variable_name(name)
63
60
  "@#{name}"
64
61
  end
65
-
66
- def create_immutable(variable_name, value)
67
- self.class.new.tap { |object|
68
- self.instance_variables.select { |v| v != variable_name }.each { |v|
69
- previous_value = self.instance_variable_get(v)
70
- object.instance_variable_set(v, previous_value)
71
- }
72
-
73
- object.instance_variable_set(variable_name, value)
74
- object.deep_freeze
75
- }
76
- end
77
-
78
- def raise_expected_type(name, value, type)
79
- unless value.instance_of?(type)
80
- message = "Expected a type of '#{type}' for attribute '#{name}'"
81
- raise TypeError, message
82
- end
83
- end
84
-
85
- def raise_not_found(key, attributes)
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
90
- end
91
62
  end
92
63
  end
@@ -0,0 +1,24 @@
1
+ module RFunk
2
+ class AttributeVariable
3
+ ATTRIBUTES_VARIABLE_NAME = '@attributes'
4
+
5
+ def add(options)
6
+ instance = options.fetch(:instance)
7
+ name = options.fetch(:name)
8
+ attributes = attributes(instance)
9
+
10
+ attributes[name] = AttributeType.new(name, options.fetch(:type), options.fetch(:options))
11
+ attributes(instance, attributes)
12
+ end
13
+
14
+ def attributes(*args)
15
+ instance = args[0]
16
+
17
+ if args.length == 1
18
+ instance.instance_variable_get(ATTRIBUTES_VARIABLE_NAME) || {}
19
+ else
20
+ instance.instance_variable_set(ATTRIBUTES_VARIABLE_NAME, args[1])
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,23 @@
1
+ module RFunk
2
+ class ErrorChecking
3
+ def raise_expected_type(name, value, type)
4
+ unless value.is_a?(type)
5
+ message = "Expected a type of '#{type}' for attribute '#{name}'"
6
+ raise TypeError, message
7
+ end
8
+ end
9
+
10
+ def raise_not_found(key, attributes)
11
+ unless attributes.key?(key)
12
+ message = "Attribute with name '#{key}' does not exist. The only available attributes are '#{attributes.keys}'"
13
+ raise RFunk::NotFoundError, message
14
+ end
15
+ end
16
+
17
+ def raise_immutable(options, variable)
18
+ keys = options.keys.select { |k| variable.has_key?(k) }
19
+ message = "Could not set variables '#{keys}', because variables are immutable."
20
+ raise ImmutableError, message if keys.any?
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,19 @@
1
+ module RFunk
2
+ class Immutable
3
+ def create(options)
4
+ instance = options.fetch(:instance)
5
+ variable_name = options.fetch(:variable_name)
6
+ value = options.fetch(:value)
7
+
8
+ instance.class.new.tap { |object|
9
+ instance.instance_variables.select { |v| v != variable_name }.each { |v|
10
+ previous_value = instance.instance_variable_get(v)
11
+ object.instance_variable_set(v, previous_value)
12
+ }
13
+
14
+ object.instance_variable_set(variable_name, value)
15
+ object.deep_freeze
16
+ }
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,4 @@
1
+ module RFunk
2
+ class ImmutableError < StandardError
3
+ end
4
+ end
@@ -0,0 +1,21 @@
1
+ module RFunk
2
+ module Variable
3
+ def var(options)
4
+ name = "#{self.class.to_s.downcase}_#{caller_locations(1, 1)[0].label}"
5
+ name = variable_name("#{name}_#{Digest::MD5.hexdigest(name)}")
6
+
7
+ if options.is_a?(Hash)
8
+ variable = self.instance_variable_get(name)
9
+
10
+ if variable
11
+ ErrorChecking.new.raise_immutable(options, variable)
12
+ self.instance_variable_set(name, variable.merge(options))
13
+ else
14
+ self.instance_variable_set(name, options)
15
+ end
16
+ else
17
+ Some(Some(self.instance_variable_get(name))[options])
18
+ end
19
+ end
20
+ end
21
+ end
@@ -1,4 +1,27 @@
1
1
  module RFunk
2
+ class Either
3
+ def initialize(value)
4
+ @value = value
5
+ end
6
+
7
+ def result
8
+ case value
9
+ when Some, None
10
+ value
11
+ else
12
+ Option(value)
13
+ end
14
+ end
15
+
16
+ def ==(other)
17
+ other.result == result
18
+ end
19
+
20
+ private
21
+
22
+ attr_reader :value
23
+ end
24
+
2
25
  def Either(value)
3
26
  if lambda?(value)
4
27
  either_with_lambda(value)
@@ -1,5 +1,5 @@
1
1
  module RFunk
2
- class Failure < Value
2
+ class Failure < Either
3
3
  def success?
4
4
  false
5
5
  end
@@ -1,5 +1,5 @@
1
1
  module RFunk
2
- class Success < Value
2
+ class Success < Either
3
3
  def success?
4
4
  true
5
5
  end
@@ -2,7 +2,7 @@ module RFunk
2
2
  class None < Option
3
3
  include Singleton
4
4
 
5
- def value(default = nil)
5
+ def value(default = None())
6
6
  Option(default)
7
7
  end
8
8
 
@@ -22,6 +22,6 @@ module RFunk
22
22
  private
23
23
 
24
24
  def nothing?(value)
25
- value.nil? || (value.respond_to?(:empty?) && value.empty?) || value.is_a?(None)
25
+ value.nil? || (value.respond_to?(:empty?) && value.empty?) || value == None.instance
26
26
  end
27
27
  end
@@ -1,11 +1,13 @@
1
1
  module RFunk
2
2
  class Some < Option
3
- attr_reader :value
4
-
5
3
  def initialize(value)
6
4
  @value = value
7
5
  end
8
6
 
7
+ def value(_ = None())
8
+ @value
9
+ end
10
+
9
11
  def or(_)
10
12
  self
11
13
  end
data/lib/rfunk/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module RFunk
2
- VERSION = '0.3.0'
2
+ VERSION = '0.4.0'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rfunk
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alex Falkowski
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-03-14 00:00:00.000000000 Z
11
+ date: 2014-03-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ice_nine
@@ -62,11 +62,15 @@ files:
62
62
  - lib/rfunk.rb
63
63
  - lib/rfunk/attribute/attribute.rb
64
64
  - lib/rfunk/attribute/attribute_type.rb
65
+ - lib/rfunk/attribute/attribute_variable.rb
66
+ - lib/rfunk/attribute/error_checking.rb
67
+ - lib/rfunk/attribute/immutable.rb
68
+ - lib/rfunk/attribute/immutable_error.rb
65
69
  - lib/rfunk/attribute/not_found_error.rb
70
+ - lib/rfunk/attribute/variable.rb
66
71
  - lib/rfunk/either/either.rb
67
72
  - lib/rfunk/either/failure.rb
68
73
  - lib/rfunk/either/success.rb
69
- - lib/rfunk/either/value.rb
70
74
  - lib/rfunk/lazy.rb
71
75
  - lib/rfunk/maybe/none.rb
72
76
  - lib/rfunk/maybe/option.rb
@@ -1,24 +0,0 @@
1
- module RFunk
2
- class Value
3
- def initialize(value)
4
- @value = value
5
- end
6
-
7
- def result
8
- case value
9
- when Some, None
10
- value
11
- else
12
- Option(value)
13
- end
14
- end
15
-
16
- def ==(other)
17
- other.result == result
18
- end
19
-
20
- private
21
-
22
- attr_reader :value
23
- end
24
- end