rfunk 0.9.0 → 0.10.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: 6579f662e130f4cdabcc91e46617cedb9a497ec6
4
- data.tar.gz: 21584e48ad127ceb7f776f36da1a2d9b88cd3176
3
+ metadata.gz: 62eadf99b770f0a7b858ec63d74bbf43a2e0fcd9
4
+ data.tar.gz: 43c0275f0e81ab6802c9f7acf4b37d6c05254800
5
5
  SHA512:
6
- metadata.gz: 253bb8ffe3a96eedad4d3006323e0d8a6d2d57a5c07510e60a751f9d4d79c853ebb9229fb5652580cdc95037e08f57539c8614c5d1958fd1434ca68019e7f937
7
- data.tar.gz: 433b260802c1c656791119b5812e91d191b7d34578abc4728de7b6a8dff3628cf543f274e5fb3d9e4c10e5fa162f050d89a418679be99c5f56af1c241e06391b
6
+ metadata.gz: 7c8b2e6b5c23fee2d9359aa0a60e38ed798cab3992eb5708a569d90b8fc788c1a689ec490c5228bf835367b139ce60d67589d0927b1a03ae91d96b6a19f550f9
7
+ data.tar.gz: b7b1866df97a5e3ba8b241dc1822460516a507ae007f139e8d31a759f93a1f91eeacb35fb443432a189037f183bef6d664042e7fe4d66ff839d2e9d76954c296
@@ -31,7 +31,7 @@ module RFunk
31
31
  end
32
32
 
33
33
  def set_variable(attribute, key, value)
34
- ErrorChecking.new.raise_expected_type(key, value, attribute.type)
34
+ ErrorChecking.new.raise_expected_attribute_type(key, value, attribute.type)
35
35
  self.instance_variable_set(variable_name(key), value)
36
36
  end
37
37
 
@@ -8,7 +8,7 @@ module RFunk
8
8
 
9
9
  define_method(name) { |value = nil|
10
10
  if value
11
- ErrorChecking.new.raise_expected_type(name, value, type)
11
+ ErrorChecking.new.raise_expected_attribute_type(name, value, type)
12
12
  Immutable.new.create(instance: self,
13
13
  variable_name: variable_name(name),
14
14
  value: value)
@@ -1,23 +1,21 @@
1
1
  module RFunk
2
2
  module AttributeFunction
3
3
  def fun(definition, &block)
4
- method_definition = method_definition(definition)
4
+ function_definition = function_definition(definition)
5
+ lambda = -> (*args) { Function.new(self, function_definition, &block).execute(*args) }
5
6
 
6
- lambda = lambda { |*args|
7
- Function.new(self, method_definition, &block).execute(*args)
8
- }
9
-
10
- define_method method_definition.value(0).value, &lambda
7
+ define_method function_definition.value(0).value, &lambda
11
8
  end
12
9
 
13
10
  [:fn, :func, :defn].each { |m| alias_method m, :fun }
14
11
 
15
12
  private
16
13
 
17
- def method_definition(definition)
14
+ def function_definition(definition)
18
15
  case definition
19
16
  when Hash
20
- Tuple(definition.keys.first, definition.values.first)
17
+ return_type = TypeAnnotation.new(definition.values.first.to_s)
18
+ Tuple(definition.keys.first, return_type)
21
19
  else
22
20
  Tuple(definition, None())
23
21
  end
@@ -8,10 +8,14 @@ module RFunk
8
8
  raise_return_type_with_message name, value, type, 'return'
9
9
  end
10
10
 
11
- def raise_expected_type(name, value, type)
11
+ def raise_expected_attribute_type(name, value, type)
12
12
  raise_return_type_with_message name, value, type, 'attribute'
13
13
  end
14
14
 
15
+ def raise_expected_parameter_type(name, value, type)
16
+ raise_return_type_with_message name, value, type, 'parameter'
17
+ end
18
+
15
19
  def raise_not_found(key, attributes)
16
20
  unless attributes.key?(key)
17
21
  message = "Attribute with name '#{key}' does not exist. The only available attributes are '#{attributes.keys}'"
@@ -2,9 +2,9 @@ module RFunk
2
2
  class Function
3
3
  attr_reader :this
4
4
 
5
- def initialize(this, method_definition, &block)
5
+ def initialize(this, function_definition, &block)
6
6
  @this = this
7
- @method_definition = method_definition
7
+ @function_definition = function_definition
8
8
  @block = block
9
9
  @variables = {}
10
10
  end
@@ -43,7 +43,8 @@ module RFunk
43
43
  end
44
44
 
45
45
  def execute(*args)
46
- result = instance_exec(*args, &block)
46
+ validate_parameter_types *args
47
+ return_value = instance_exec(*args, &block)
47
48
 
48
49
  if body_block
49
50
  if pre_block
@@ -62,8 +63,8 @@ module RFunk
62
63
  validate_return_type(body)
63
64
  }
64
65
  else
65
- validate_return_type(result)
66
- Option(result)
66
+ validate_return_type(return_value)
67
+ Option(return_value)
67
68
  end
68
69
  end
69
70
 
@@ -73,17 +74,33 @@ module RFunk
73
74
 
74
75
  private
75
76
 
76
- attr_reader :block, :pre_block, :post_block, :body_block, :method_definition
77
+ attr_reader :block, :pre_block, :post_block, :body_block, :function_definition
77
78
  attr_accessor :variables
78
79
 
79
80
  def error_checking
80
- @error_checking ||= ErrorChecking.new
81
+ Lazy(-> { ErrorChecking.new }).value
81
82
  end
82
83
 
83
- def validate_return_type(result)
84
- error_checking.raise_expected_return_type method_definition.value(0).value,
85
- result,
86
- method_definition.value(1)
84
+ def validate_return_type(return_value)
85
+ error_checking.raise_expected_return_type(function_name.value,
86
+ return_value,
87
+ type_annotation.return)
88
+ end
89
+
90
+ def validate_parameter_types(*args)
91
+ values = args.zip(type_annotation.parameters)
92
+ values.each_with_index { |v, i|
93
+ tuple = Tuple(*v)
94
+ error_checking.raise_expected_parameter_type(i + 1, tuple.value(0), tuple.value(1))
95
+ }
96
+ end
97
+
98
+ def function_name
99
+ function_definition.value(0)
100
+ end
101
+
102
+ def type_annotation
103
+ function_definition.value(1)
87
104
  end
88
105
  end
89
106
  end
@@ -0,0 +1,25 @@
1
+ module RFunk
2
+ class TypeAnnotation
3
+ attr_reader :parameters, :return
4
+
5
+ def initialize(annotation)
6
+ case Option(annotation)
7
+ when Some
8
+ @annotation = annotation.split('->')
9
+ @parameters = if @annotation.count == 2
10
+ @annotation[0].split(',').map { |p| Object.const_get(p.strip) }
11
+ else
12
+ []
13
+ end
14
+ @return = if @annotation.count == 2
15
+ Object.const_get(@annotation[1].strip)
16
+ else
17
+ Object.const_get(@annotation[0].strip)
18
+ end
19
+ else
20
+ @parameters = []
21
+ @return = None()
22
+ end
23
+ end
24
+ end
25
+ end
data/lib/rfunk/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module RFunk
2
- VERSION = '0.9.0'
2
+ VERSION = '0.10.0'
3
3
  end
data/lib/rfunk.rb CHANGED
@@ -17,6 +17,7 @@ require 'rfunk/attribute/function'
17
17
  require 'rfunk/attribute/attribute_definition'
18
18
  require 'rfunk/attribute/attribute_function'
19
19
  require 'rfunk/attribute/attribute'
20
+ require 'rfunk/attribute/type_annotation'
20
21
  require 'rfunk/maybe/option'
21
22
  require 'rfunk/maybe/none'
22
23
  require 'rfunk/maybe/some'
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.9.0
4
+ version: 0.10.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alex Falkowski
@@ -115,6 +115,7 @@ files:
115
115
  - lib/rfunk/attribute/not_found_error.rb
116
116
  - lib/rfunk/attribute/post_condition_error.rb
117
117
  - lib/rfunk/attribute/pre_condition_error.rb
118
+ - lib/rfunk/attribute/type_annotation.rb
118
119
  - lib/rfunk/either/either.rb
119
120
  - lib/rfunk/either/failure.rb
120
121
  - lib/rfunk/either/success.rb