Class: DRG::Ruby::Const

Inherits:
Object
  • Object
show all
Defined in:
lib/drg/ruby/const.rb

Direct Known Subclasses

Spec

Constant Summary collapse

CONSTANT_DEFS =
{ :class => :class, :module => :module, :cdecl => :class }

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sexp) ⇒ Const

Returns a new instance of Const.

Parameters:

  • sexp (Sexp)


9
10
11
12
# File 'lib/drg/ruby/const.rb', line 9

def initialize(sexp)
  sexp = sexp.select { |x| x.is_a?(Array) }.last if sexp[0] == :block
  @sexp = sexp
end

Instance Attribute Details

#sexpObject (readonly)

Returns the value of attribute sexp.



6
7
8
# File 'lib/drg/ruby/const.rb', line 6

def sexp
  @sexp
end

Instance Method Details

#class?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/drg/ruby/const.rb', line 47

def class?
  type == :class
end

#class_varsObject



60
61
# File 'lib/drg/ruby/const.rb', line 60

def class_vars
end

#func_by_name(name_as_symbol) ⇒ Object



39
40
41
# File 'lib/drg/ruby/const.rb', line 39

def func_by_name(name_as_symbol)
  funcs.find { |x| x.name == name_as_symbol }
end

#funcsObject



43
44
45
# File 'lib/drg/ruby/const.rb', line 43

def funcs
  @funcs ||= load_funkyness
end

#initialization_argsObject



35
36
37
# File 'lib/drg/ruby/const.rb', line 35

def initialization_args
  funcs.find(-> { OpenStruct.new }) { |func| func.name == :initialize }.args.to_a
end

#instance_varsObject



56
57
# File 'lib/drg/ruby/const.rb', line 56

def instance_vars
end

#module?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/drg/ruby/const.rb', line 51

def module?
  type == :module
end

#name(sexp = @sexp, list = []) ⇒ Object



14
15
16
17
18
19
20
21
22
23
# File 'lib/drg/ruby/const.rb', line 14

def name(sexp = @sexp, list = [])
  sexp = Array(sexp)
  if sexp[1].is_a?(Sexp) && sexp[1][0] == :colon2
    parts = sexp[1].to_a.flatten
    list.concat parts.drop(parts.size / 2)
  elsif CONSTANT_DEFS.key?(sexp[0])
    name(sexp.compact[2], list << sexp[1].to_s)
  end
  list.join('::')
end

#type(sexp = @sexp, val = nil) ⇒ Object



25
26
27
28
29
30
31
32
33
# File 'lib/drg/ruby/const.rb', line 25

def type(sexp = @sexp, val = nil)
  sexp = Array(sexp)
  if sexp[1].is_a?(Sexp) && sexp[1][0] == :colon2
    val = sexp[0]
  elsif CONSTANT_DEFS.key?(sexp[0])
    val = type(sexp.compact[2], sexp[0])
  end
  CONSTANT_DEFS[val]
end