Class: MethodParameter

Inherits:
BaseVariable show all
Defined in:
lib/core/variable/MethodParameter.rb

Instance Attribute Summary collapse

Attributes inherited from BaseVariable

#instance_variable, #uniq_id_history, #value, #variable_id

Attributes included from Variable

#scope_id, #uniq_id

Instance Method Summary collapse

Methods inherited from BaseVariable

#destructive_instance_calls, #find_actual_variable, #instance_calls, #realised?, reset_global_id, #returning_instance_calls, #write, #write_with_uniq_id

Methods included from Variable

#increament_uniq_id!, reset_global_id, #to_var, variable_id

Methods included from VariableIncluded

#find_actual_variable, #variable

Methods inherited from Array

#cauldron_method_calls, #contains?, #select_all, #to_intrinsic, #to_literal, #to_var, #write

Constructor Details

#initialize(id = nil) ⇒ MethodParameter

Returns a new instance of MethodParameter.



5
6
7
# File 'lib/core/variable/MethodParameter.rb', line 5

def initialize(id=nil)
  super(id)
end

Instance Attribute Details

#usage_variableObject

Returns a reference to the usage variable that is represented by this variable. Remember this MethodParameter represents a particular rule so its id doesn’t reflect all cases.



85
86
87
# File 'lib/core/variable/MethodParameter.rb', line 85

def usage_variable
  @usage_variable
end

#variable_id=(value) ⇒ Object (writeonly)

Sets the attribute variable_id

Parameters:

  • value

    the value to set the attribute variable_id to.



3
4
5
# File 'lib/core/variable/MethodParameter.rb', line 3

def variable_id=(value)
  @variable_id = value
end

Instance Method Details

#copyObject



77
78
79
# File 'lib/core/variable/MethodParameter.rb', line 77

def copy
  return Marshal.load(Marshal.dump(self))
end

#declaration_statementObject

Returns a statement showing the variable being declaraced

MethodParameter.new(Requirement.new())



115
116
117
118
# File 'lib/core/variable/MethodParameter.rb', line 115

def declaration_statement
  # TODO  Check that .copy method call works across all the uses of "declaration_statement"
  return Statement.new(ClassMethodCallContainer.new(MethodParameterClass.new,New.new,*self.collect {|x| x.copy.declaration_statement}))    
end

#describe(tab = 0, context = self) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/core/variable/MethodParameter.rb', line 47

def describe(tab=0,context=self)
  
  # Create a temporay copy of the variable
  copied_var = self.copy

  unless context == self
    
    # Return a duplicate variable with the requirements in the context indicated
    copied_var = context.contextual_variable(copied_var.variable_id)
    
  end
  
  # Print the name of the variable
  line = ''    
  line += write
  
  # Print out each of the requirements
  copied_var.each do |x|  
    desc = x.describe(context)
    desc.each_line do |l|
      line += "\t"+l
    end      
    line += "\n" unless x == copied_var.last      
  end
  line += "\n"
  
  return line
  
end

#literaliseObject

Attempts to return a literal that meets all the requirements.

Raises:

  • (StandardError)


99
100
101
102
103
104
105
106
107
108
109
# File 'lib/core/variable/MethodParameter.rb', line 99

def literalise

  # Does the variable have any "equal to literal" requirements
  self.each do |req|
    if req.structure_match?([This,Equal,Literal])
      return req[2].copy
    end
  end
  
  raise StandardError.new('The literalise call is very basic right now and needs improved')
end

#meets_requirements?(of) ⇒ Boolean

Returns true if this variables meets the requirements of the supplied variable. If the this meets the requirements of the supplied variable then this variable could replace the supplied variable in a statement.

TODO Write tests for this

TODO Have an act_as call that doesn’t return true/false but raises errors so I can identify

why something can't be something else.

Parameters:

  • of

Returns:



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/core/variable/MethodParameter.rb', line 21

def meets_requirements? of
  
  # Handle the possible datatypes of "of"
  begin 
    if of.kind_of?(Variable)
      return meets_requirements_of_variable(of)
    elsif of.kind_of?(InstanceCallContainer)
      return meets_requirements_of_instance_call(of)
    else
      raise StandardError.new('Not expecting class of type '+of.class.to_s)
    end
  rescue FailedVariableMatch
    return false
  end
end

#to_declarationObject



120
121
122
# File 'lib/core/variable/MethodParameter.rb', line 120

def to_declaration
  return VariableDeclaration.new('MethodParameter',*self.collect {|x| x.to_declaration})
end

#to_literal_stringObject

Raises an error to avoid me accidently trying to give this class the method.



40
41
42
43
44
# File 'lib/core/variable/MethodParameter.rb', line 40

def to_literal_string
  # TODO  I should turn this on further down the line - it realtes to tracking
  #raise StandardError.new('Method Variables can\'t be converted to a literal string because they don\'t have a value')
  return self.write
end