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
13
14
15
16
17
# File 'lib/ryan/const.rb', line 9

def initialize(sexp)
  if sexp[0] == :block
    # This is common when the file ha require or other code at the top before the class def,
    # in which case, grab the last element of the block and assume that's the class def.
    @sexp = sexp[sexp.size - 1]
  else
    @sexp = sexp
  end
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)


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

def class?
  type == :class
end

#func_by_name(name_as_symbol) ⇒ Object



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

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

#funcsObject



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

def funcs
  @funcs ||= load_funcs.flatten
end

#initialization_argsObject



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

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)


58
59
60
# File 'lib/ryan/const.rb', line 58

def module?
  type == :module
end

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



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/ryan/const.rb', line 25

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



62
63
64
65
66
67
68
69
70
# File 'lib/ryan/const.rb', line 62

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