Class: Rbss::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/rbss/base.rb

Direct Known Subclasses

Custom, Parser

Instance Method Summary collapse

Constructor Details

#initialize(files) ⇒ Base

Returns a new instance of Base.



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/rbss/base.rb', line 3

def initialize(files)
  if files.is_a?(Array)
    @@files = files
  elsif files.is_a?(String)
    @@files = files.split(',')
  elsif files.is_a?(Symbol)
    @@files = [files.to_s]
  elsif files.is_a?(NilClass)
    @@files = nil
  else
    raise 'List of files must be either an Array of Strings or a String of files seperated by commas'
  end

  @@top_stack_level = caller.size - 1
  @@stack = [[@@top_stack_level, nil, Selector.new(nil,nil)]] # stack level, name, object
  @@fake_stack = nil
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/rbss/base.rb', line 21

def method_missing(method_name, *args, &block)
  if !@@fake_stack.nil?
    stack_level = @@fake_stack
    @@fake_stack = nil
  else
    stack_level = caller.size - (@offset || 0)
  end

  reverse_stack = @@stack.reverse
  parent = reverse_stack[0]
  i = 0
  until parent[0] < stack_level || parent[0] == @@top_stack_level
    i += 1
    parent = reverse_stack[i]
  end
  
  if block_given?
    selector = Selector.new(method_name.to_s, args)
    if stack_level != @@top_stack_level
      selector.identify_parent(parent[1])
    end
    @@stack = @@stack << [stack_level, selector.name, selector]
    yield
  else
    property = Property.new(method_name.to_s, args.first)
    parent[2].add_to_properties(property)
  end
end