Class: Ryan::Const

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

Constant Summary collapse

CLASS_MOD_DEFS =
{ class: :class, module: :module }
CONSTANT_DEFS =
{ cdecl: :class }.merge CLASS_MOD_DEFS
PRIVATE_DEFS =
[:private, :protected]

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/ryan/const.rb', line 9

def initialize(sexp)
  sexp = sexp[2] if sexp[0] == :block
  @sexp = sexp
end

Instance Attribute Details

#sexpObject (readonly)

Returns the value of attribute sexp.



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

def sexp
  @sexp
end

Instance Method Details

#class?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/ryan/const.rb', line 49

def class?
  type == :class
end

#func_by_name(name_as_symbol) ⇒ Object



41
42
43
# File 'lib/ryan/const.rb', line 41

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

#funcsObject



45
46
47
# File 'lib/ryan/const.rb', line 45

def funcs
  @funcs ||= load_funcs.flatten
end

#initialization_argsObject



37
38
39
# File 'lib/ryan/const.rb', line 37

def initialization_args
  funcs.find(-> { OpenStruct.new }) { |func| func.name == :initialize }.args.to_a.reject { |a| a.to_s.sub(/^\*/, '').empty? }
end

#module?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/ryan/const.rb', line 53

def module?
  type == :module
end

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



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

def name(sexp = @sexp, list = [])
  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])
    list << sexp[1].to_s
    # skip when the second element is nil; it's just there sometimes and usually indicates the end of the definition
    if !sexp[2].nil? || (sexp[3].is_a?(Sexp) and CLASS_MOD_DEFS.key?(sexp[3].first))
      compact_sexp = sexp.compact[2]
      if CLASS_MOD_DEFS.key?(compact_sexp.first)
        name(compact_sexp, list)
      end
    end
  end
  list.join('::')
end

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



57
58
59
60
61
62
63
64
65
# File 'lib/ryan/const.rb', line 57

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