Class: StatementExecutor

Inherits:
Object
  • Object
show all
Defined in:
lib/rubyslim/statement_executor.rb

Instance Method Summary collapse

Constructor Details

#initializeStatementExecutor

Returns a new instance of StatementExecutor.



7
8
9
10
11
12
# File 'lib/rubyslim/statement_executor.rb', line 7

def initialize
  @instances = {}
  @modules = []
  @symbols = {}
  @libraries = [SlimHelperLibrary.new(self)]
end

Instance Method Details

#acquire_symbol(symbol_text) ⇒ Object



150
151
152
153
154
# File 'lib/rubyslim/statement_executor.rb', line 150

def acquire_symbol(symbol_text)
  symbol = get_symbol(symbol_text[1..-1])
  symbol = symbol_text if symbol.nil?
  symbol
end

#add_module(module_name) ⇒ Object



138
139
140
# File 'lib/rubyslim/statement_executor.rb', line 138

def add_module(module_name)
  @modules << module_name.gsub(/\./, '::')
end

#call(instance_name, method_name, *args) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/rubyslim/statement_executor.rb', line 118

def call(instance_name, method_name, *args)
  begin
    instance = self.instance(instance_name)
    if method = method_to_call(instance, method_name)
      send_message_to_instance(instance, method, args)
    elsif instance.respond_to?(:sut) && method = method_to_call(instance.sut, method_name)
      send_message_to_instance(instance.sut, method, args)
    else
      @libraries.reverse_each do |library|
        method = method_to_call(library, method_name)
        return send_message_to_instance(library, method, args) if method
      end
      raise SlimError.new("message:<<NO_INSTANCE #{instance_name}>>") if instance.nil?
      raise SlimError.new("message:<<NO_METHOD_IN_CLASS #{method_name}[#{args.length}] #{instance.class.name}.>>")
    end
  rescue SlimError => e
    Statement::EXCEPTION_TAG + e.to_s
  end
end

#construct(class_name, constructor_arguments) ⇒ Object



62
63
64
65
66
67
68
69
# File 'lib/rubyslim/statement_executor.rb', line 62

def construct(class_name, constructor_arguments)
  class_object = get_class(class_name)
  begin
    class_object.new(*replace_tables_with_hashes(constructor_arguments))
  rescue ArgumentError => e
    raise SlimError.new("message:<<COULD_NOT_INVOKE_CONSTRUCTOR #{class_name}[#{constructor_arguments.length}]>>")
  end
end

#construct_instance(class_name, constructor_arguments) ⇒ Object



39
40
41
42
# File 'lib/rubyslim/statement_executor.rb', line 39

def construct_instance(class_name, constructor_arguments)
  require_class(class_name);
  construct(class_name, constructor_arguments);
end

#create(instance_name, class_name, constructor_arguments) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/rubyslim/statement_executor.rb', line 19

def create(instance_name, class_name, constructor_arguments)
  begin
    instance = replace_symbol(class_name)
    if instance.is_a?(String)
      instance = construct_instance(instance, replace_symbols(constructor_arguments))
      if library?(instance_name)
        @libraries << instance
      end
    end
    set_instance(instance_name, instance)
    "OK"
  rescue SlimError => e
    Statement::EXCEPTION_TAG + e.to_s
  end
end

#get_class(class_name) ⇒ Object

Raises:



87
88
89
90
91
92
93
94
95
# File 'lib/rubyslim/statement_executor.rb', line 87

def get_class(class_name)
  with_each_fully_qualified_class_name(class_name) do |fully_qualified_name|
    begin
      return eval(fully_qualified_name)
    rescue NameError
    end
  end
  raise SlimError.new("message:<<COULD_NOT_INVOKE_CONSTRUCTOR #{class_name} in any module #{@modules.inspect}>>")
end

#get_symbol(name) ⇒ Object



146
147
148
# File 'lib/rubyslim/statement_executor.rb', line 146

def get_symbol(name)
  @symbols[name]
end

#instance(instance_name) ⇒ Object



97
98
99
# File 'lib/rubyslim/statement_executor.rb', line 97

def instance(instance_name)
  @instances[instance_name]
end

#library?(instance_name) ⇒ Boolean

Returns:

  • (Boolean)


14
15
16
17
# File 'lib/rubyslim/statement_executor.rb', line 14

def library?(instance_name)
  library_prefix = "library"
  instance_name[0, library_prefix.length] == library_prefix
end

#make_path_to_class(class_name) ⇒ Object



45
46
47
48
49
# File 'lib/rubyslim/statement_executor.rb', line 45

def make_path_to_class(class_name)
  module_names = split_class_name(class_name)
  files = module_names.collect { |module_name| to_file_name(module_name) }
  File.join(files)
end

#method_to_call(instance, method_name) ⇒ Object



111
112
113
114
115
116
# File 'lib/rubyslim/statement_executor.rb', line 111

def method_to_call(instance, method_name)
  return nil unless instance
  return method_name.to_sym if instance.respond_to?(method_name)
  return "#{$1}=".to_sym if method_name =~ /set_(\w+)/ && instance.respond_to?("#{$1}=")
  return nil
end

#replace_symbol(item) ⇒ Object



156
157
158
159
160
161
162
163
# File 'lib/rubyslim/statement_executor.rb', line 156

def replace_symbol(item)
  match = item.match(/\A\$\w*\z/)
  return acquire_symbol(match[0]) if match

  item.gsub(/\$\w*/) do |match|
    acquire_symbol(match)
  end
end

#replace_symbols(list) ⇒ Object



165
166
167
168
169
170
171
172
173
# File 'lib/rubyslim/statement_executor.rb', line 165

def replace_symbols(list)
  list.map do |item|
    if item.kind_of?(Array)
      replace_symbols(item)
    else
      replace_symbol(item)
    end
  end
end

#replace_tables_with_hashes(constructor_arguments) ⇒ Object



55
56
57
58
59
60
# File 'lib/rubyslim/statement_executor.rb', line 55

def replace_tables_with_hashes(constructor_arguments)
  args = constructor_arguments.map do |arg|
    TableToHashConverter.convert arg
  end
  return args
end

#require_class(class_name) ⇒ Object

Raises:



76
77
78
79
80
81
82
83
84
85
# File 'lib/rubyslim/statement_executor.rb', line 76

def require_class(class_name)
  with_each_fully_qualified_class_name(class_name) do |fully_qualified_name|
    begin
      require make_path_to_class(fully_qualified_name)
      return
    rescue LoadError
    end
  end
  raise SlimError.new("message:<<COULD_NOT_INVOKE_CONSTRUCTOR #{class_name} failed to find in #{@modules.map { |mod| make_path_to_class(mod) }.inspect}>>")
end

#send_message_to_instance(instance, method, args) ⇒ Object



106
107
108
109
# File 'lib/rubyslim/statement_executor.rb', line 106

def send_message_to_instance(instance, method, args)
  symbols = replace_symbols(args)
  instance.send(method, *replace_tables_with_hashes(symbols))
end

#set_instance(instance_name, instance) ⇒ Object



35
36
37
# File 'lib/rubyslim/statement_executor.rb', line 35

def set_instance(instance_name, instance)
  @instances[instance_name] = instance
end

#set_symbol(name, value) ⇒ Object



142
143
144
# File 'lib/rubyslim/statement_executor.rb', line 142

def set_symbol(name, value)
  @symbols[name] = value
end

#split_class_name(class_name) ⇒ Object



51
52
53
# File 'lib/rubyslim/statement_executor.rb', line 51

def split_class_name(class_name)
  class_name.split(/\:\:/)
end

#to_file_name(module_name) ⇒ Object



101
102
103
104
# File 'lib/rubyslim/statement_executor.rb', line 101

def to_file_name(module_name)
  value = module_name[0..0].downcase + module_name[1..-1]
  value.gsub(/[A-Z]/) { |cap| "_#{cap.downcase}" }
end

#with_each_fully_qualified_class_name(class_name, &block) ⇒ Object



72
73
74
# File 'lib/rubyslim/statement_executor.rb', line 72

def with_each_fully_qualified_class_name(class_name, &block)
  (@modules.map { |module_name| module_name + "::" + class_name } << class_name).reverse.each &block
end