Class: Ryan::Const
- Inherits:
-
Object
- Object
- Ryan::Const
- 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
-
#sexp ⇒ Object
readonly
Returns the value of attribute sexp.
Instance Method Summary collapse
- #class? ⇒ Boolean
- #func_by_name(name_as_symbol) ⇒ Object
- #funcs ⇒ Object
- #initialization_args ⇒ Object
-
#initialize(sexp) ⇒ Const
constructor
A new instance of Const.
- #module? ⇒ Boolean
- #name(sexp = @sexp, list = []) ⇒ Object
- #type(sexp = @sexp, val = nil) ⇒ Object
Constructor Details
#initialize(sexp) ⇒ Const
Returns a new instance of Const.
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
#sexp ⇒ Object (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
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 |
#funcs ⇒ Object
45 46 47 |
# File 'lib/ryan/const.rb', line 45 def funcs @funcs ||= load_funcs.flatten end |
#initialization_args ⇒ Object
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
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 |