Class: RFunk::Function

Inherits:
Object
  • Object
show all
Defined in:
lib/rfunk/attribute/function.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(this, method_definition, &block) ⇒ Function

Returns a new instance of Function.



5
6
7
8
9
10
# File 'lib/rfunk/attribute/function.rb', line 5

def initialize(this, method_definition, &block)
  @this = this
  @method_definition = method_definition
  @block = block
  @variables = {}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *arguments, &block) ⇒ Object



70
71
72
# File 'lib/rfunk/attribute/function.rb', line 70

def method_missing(method, *arguments, &block)
  this.send(method, *arguments, &block)
end

Instance Attribute Details

#thisObject (readonly)

Returns the value of attribute this.



3
4
5
# File 'lib/rfunk/attribute/function.rb', line 3

def this
  @this
end

Instance Method Details

#assert(&_) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/rfunk/attribute/function.rb', line 25

def assert(&_)
  if yield
    true
  else
    raise AssertionError
  end
end

#body(&block) ⇒ Object



41
42
43
# File 'lib/rfunk/attribute/function.rb', line 41

def body(&block)
  @body_block = block
end

#execute(*args) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/rfunk/attribute/function.rb', line 45

def execute(*args)
  result = instance_exec(*args, &block)

  if body_block
    if pre_block
      instance_eval(&pre_block).tap { |r|
        error_checking.raise_condition_error(PreConditionError, r)
      }
    end

    Option(instance_eval(&body_block)).tap { |body|
      if post_block
        instance_eval(&post_block).tap { |post|
          error_checking.raise_condition_error(PostConditionError, post)
        }
      end

      validate_return_type(body)
    }
  else
    validate_return_type(result)
    Option(result)
  end
end

#post(&block) ⇒ Object



37
38
39
# File 'lib/rfunk/attribute/function.rb', line 37

def post(&block)
  @post_block = block
end

#pre(&block) ⇒ Object



33
34
35
# File 'lib/rfunk/attribute/function.rb', line 33

def pre(&block)
  @pre_block = block
end

#var(options) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/rfunk/attribute/function.rb', line 12

def var(options)
  if options.is_a?(Hash)
    if variables.empty?
      self.variables = options
    else
      error_checking.raise_immutable(options, variables)
      self.variables = variables.merge(options)
    end
  else
    Some(Some(variables)[options])
  end
end