rfunk 0.6.1 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e8bec17463922dd4c70a584ccafd24973bb29de2
4
- data.tar.gz: 161a3098109d8701ec9c728ea4978614874aa7d0
3
+ metadata.gz: 063f99ddc73ca7ec1f670674f3be7e90bdc077d5
4
+ data.tar.gz: 7a2960caa00efc87d550c78754581f5f96f16909
5
5
  SHA512:
6
- metadata.gz: a22230f56cbc42906a1a2496e7fc104f0d582de7131381f6a871a59044d975b4b325603fa7963a7ad57c249dcff25bf9c9a91ce5bf4261f1cb54f814a32fc541
7
- data.tar.gz: 40ba8fa669edce325f246b1d0681a8d9709c7542be93510bb5b5e71b65950e52a1219c3a0eadf1575406ba6d7b2f2d80fd34606ecc095a9a9f30f0eee76a7877
6
+ metadata.gz: ab8c129b60df6f8650f475afa5b0707cd8ffd8df4d39c67c389efb8c362696eb1df183a6861e3a81a25c0f9aeb567f60732af3dca160d4fc0dba42a6ad3c326e
7
+ data.tar.gz: c9c7a897333250ea44cf49c6b4968db76141f89943a1b3efd2461d9fb4bc99495efc8d0004a0c546dd277dde92b718acd726a7692c32237bbf26850ae67ebcf2
@@ -0,0 +1,4 @@
1
+ module RFunk
2
+ class AssertionError < StandardError
3
+ end
4
+ end
@@ -1,7 +1,7 @@
1
1
  module RFunk
2
2
  module Attribute
3
3
  def self.included(base)
4
- base.extend(ClassMethods)
4
+ base.extend(RFunk::AttributeDefinition)
5
5
  base.extend(RFunk::AttributeFunction)
6
6
  end
7
7
 
@@ -10,26 +10,6 @@ module RFunk
10
10
  with_attributes(options)
11
11
  end
12
12
 
13
- module ClassMethods
14
- def attribute(name, type, options = {})
15
- AttributeVariable.new.add(instance: self,
16
- name: name,
17
- type: type,
18
- options: options)
19
-
20
- define_method(name) { |value = nil|
21
- if value
22
- ErrorChecking.new.raise_expected_type(name, value, type)
23
- Immutable.new.create(instance: self,
24
- variable_name: variable_name(name),
25
- value: value)
26
- else
27
- Option(self.instance_variable_get(variable_name(name)))
28
- end
29
- }
30
- end
31
- end
32
-
33
13
  private
34
14
 
35
15
  def attributes
@@ -0,0 +1,21 @@
1
+ module RFunk
2
+ module AttributeDefinition
3
+ def attribute(name, type, options = {})
4
+ AttributeVariable.new.add(instance: self,
5
+ name: name,
6
+ type: type,
7
+ options: options)
8
+
9
+ define_method(name) { |value = nil|
10
+ if value
11
+ ErrorChecking.new.raise_expected_type(name, value, type)
12
+ Immutable.new.create(instance: self,
13
+ variable_name: variable_name(name),
14
+ value: value)
15
+ else
16
+ Option(self.instance_variable_get(variable_name(name)))
17
+ end
18
+ }
19
+ end
20
+ end
21
+ end
@@ -2,7 +2,7 @@ module RFunk
2
2
  module AttributeFunction
3
3
  def fun(method_name, &block)
4
4
  lambda = lambda { |*args|
5
- instance_exec(*args.unshift(Function.new), &block)
5
+ Function.new(self, &block).execute(*args)
6
6
  }
7
7
 
8
8
  define_method method_name, &lambda
@@ -1,5 +1,9 @@
1
1
  module RFunk
2
2
  class ErrorChecking
3
+ def raise_condition_error(type, value)
4
+ raise type, 'The condition was not met!' unless value
5
+ end
6
+
3
7
  def raise_expected_type(name, value, type)
4
8
  case value
5
9
  when Some
@@ -1,6 +1,10 @@
1
1
  module RFunk
2
2
  class Function
3
- def initialize
3
+ attr_reader :this
4
+
5
+ def initialize(this, &block)
6
+ @this = this
7
+ @block = block
4
8
  @variables = {}
5
9
  end
6
10
 
@@ -9,7 +13,7 @@ module RFunk
9
13
  if variables.empty?
10
14
  self.variables = options
11
15
  else
12
- ErrorChecking.new.raise_immutable(options, variables)
16
+ error_checking.raise_immutable(options, variables)
13
17
  self.variables = variables.merge(options)
14
18
  end
15
19
  else
@@ -17,8 +21,59 @@ module RFunk
17
21
  end
18
22
  end
19
23
 
24
+ def assert(&_)
25
+ if yield
26
+ true
27
+ else
28
+ raise AssertionError
29
+ end
30
+ end
31
+
32
+ def pre(&block)
33
+ @pre_block = block
34
+ end
35
+
36
+ def post(&block)
37
+ @post_block = block
38
+ end
39
+
40
+ def body(&block)
41
+ @body_block = block
42
+ end
43
+
44
+ def execute(*args)
45
+ result = instance_exec(*args, &block)
46
+
47
+ if body_block
48
+ if pre_block
49
+ instance_eval(&pre_block).tap { |r|
50
+ error_checking.raise_condition_error(PreConditionError, r)
51
+ }
52
+ end
53
+
54
+ Option(instance_eval(&body_block)).tap {
55
+ if post_block
56
+ instance_eval(&post_block).tap { |r|
57
+ error_checking.raise_condition_error(PostConditionError, r)
58
+ }
59
+ end
60
+ }
61
+ else
62
+ Option(result)
63
+ end
64
+ end
65
+
66
+ def method_missing(method, *arguments, &block)
67
+ this.send(method, *arguments, &block)
68
+ end
69
+
20
70
  private
21
71
 
72
+ attr_reader :block, :pre_block, :post_block, :body_block
22
73
  attr_accessor :variables
74
+
75
+ def error_checking
76
+ @error_checking ||= ErrorChecking.new
77
+ end
23
78
  end
24
79
  end
@@ -0,0 +1,4 @@
1
+ module RFunk
2
+ class PostConditionError < StandardError
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module RFunk
2
+ class PreConditionError < StandardError
3
+ end
4
+ end
data/lib/rfunk/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module RFunk
2
- VERSION = '0.6.1'
2
+ VERSION = '0.7.0'
3
3
  end
data/lib/rfunk.rb CHANGED
@@ -5,11 +5,15 @@ require 'ice_nine/core_ext/object'
5
5
 
6
6
  require 'rfunk/attribute/immutable'
7
7
  require 'rfunk/attribute/not_found_error'
8
+ require 'rfunk/attribute/pre_condition_error'
9
+ require 'rfunk/attribute/post_condition_error'
10
+ require 'rfunk/attribute/assertion_error'
8
11
  require 'rfunk/attribute/immutable_error'
9
12
  require 'rfunk/attribute/attribute_variable'
10
13
  require 'rfunk/attribute/error_checking'
11
14
  require 'rfunk/attribute/attribute_type'
12
15
  require 'rfunk/attribute/function'
16
+ require 'rfunk/attribute/attribute_definition'
13
17
  require 'rfunk/attribute/attribute_function'
14
18
  require 'rfunk/attribute/attribute'
15
19
  require 'rfunk/maybe/option'
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.6.1
4
+ version: 0.7.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-20 00:00:00.000000000 Z
11
+ date: 2014-03-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ice_nine
@@ -74,7 +74,9 @@ extensions: []
74
74
  extra_rdoc_files: []
75
75
  files:
76
76
  - lib/rfunk.rb
77
+ - lib/rfunk/attribute/assertion_error.rb
77
78
  - lib/rfunk/attribute/attribute.rb
79
+ - lib/rfunk/attribute/attribute_definition.rb
78
80
  - lib/rfunk/attribute/attribute_function.rb
79
81
  - lib/rfunk/attribute/attribute_type.rb
80
82
  - lib/rfunk/attribute/attribute_variable.rb
@@ -83,6 +85,8 @@ files:
83
85
  - lib/rfunk/attribute/immutable.rb
84
86
  - lib/rfunk/attribute/immutable_error.rb
85
87
  - lib/rfunk/attribute/not_found_error.rb
88
+ - lib/rfunk/attribute/post_condition_error.rb
89
+ - lib/rfunk/attribute/pre_condition_error.rb
86
90
  - lib/rfunk/either/either.rb
87
91
  - lib/rfunk/either/failure.rb
88
92
  - lib/rfunk/either/success.rb