Class: Rubyzen::Declarations::ConstantDeclaration
- Inherits:
-
Object
- Object
- Rubyzen::Declarations::ConstantDeclaration
- Includes:
- Providers::ClassNameProvider, Providers::FilePathProvider, Providers::LineNumberProvider, Providers::SourceCodeProvider
- Defined in:
- lib/rubyzen/declarations/constant_declaration.rb
Overview
Represents a constant assignment (+MAX = 100+) or reference (MAX).
Instance Attribute Summary collapse
- #node ⇒ RuboCop::AST::Node readonly
- #parent ⇒ FileDeclaration, ... readonly
Instance Method Summary collapse
-
#assignment? ⇒ Boolean
Returns whether this is a constant assignment (
:casgn). -
#enclosing_class ⇒ ClassDeclaration?
Returns the enclosing ClassDeclaration, if any.
-
#enclosing_module ⇒ ModuleDeclaration?
Returns the enclosing ModuleDeclaration, if any.
-
#in_class? ⇒ Boolean
Returns whether this constant is defined inside a class.
-
#in_module? ⇒ Boolean
Returns whether this constant is defined inside a module.
-
#initialize(node, parent) ⇒ ConstantDeclaration
constructor
A new instance of ConstantDeclaration.
-
#name ⇒ String
Returns the constant name.
-
#reference? ⇒ Boolean
Returns whether this is a constant reference (
:const). -
#scoped? ⇒ Boolean
Returns whether this constant is defined inside a class or module.
-
#top_level? ⇒ Boolean
Returns whether this constant is defined at file scope (not inside a class or module).
-
#value ⇒ String, ...
Returns the assigned value for constant assignments.
Methods included from Providers::SourceCodeProvider
Methods included from Providers::ClassNameProvider
Methods included from Providers::LineNumberProvider
Methods included from Providers::FilePathProvider
Constructor Details
#initialize(node, parent) ⇒ ConstantDeclaration
Returns a new instance of ConstantDeclaration.
25 26 27 28 |
# File 'lib/rubyzen/declarations/constant_declaration.rb', line 25 def initialize(node, parent) @node = node @parent = parent end |
Instance Attribute Details
#node ⇒ RuboCop::AST::Node (readonly)
18 19 20 |
# File 'lib/rubyzen/declarations/constant_declaration.rb', line 18 def node @node end |
#parent ⇒ FileDeclaration, ... (readonly)
21 22 23 |
# File 'lib/rubyzen/declarations/constant_declaration.rb', line 21 def parent @parent end |
Instance Method Details
#assignment? ⇒ Boolean
Returns whether this is a constant assignment (:casgn).
68 69 70 |
# File 'lib/rubyzen/declarations/constant_declaration.rb', line 68 def assignment? node.type == :casgn end |
#enclosing_class ⇒ ClassDeclaration?
Returns the enclosing Rubyzen::Declarations::ClassDeclaration, if any.
97 98 99 100 101 |
# File 'lib/rubyzen/declarations/constant_declaration.rb', line 97 def enclosing_class find_enclosing_ast_node(:class) do |n| Rubyzen::Declarations::ClassDeclaration.new(n, file_declaration) end end |
#enclosing_module ⇒ ModuleDeclaration?
Returns the enclosing ModuleDeclaration, if any.
113 114 115 116 117 |
# File 'lib/rubyzen/declarations/constant_declaration.rb', line 113 def enclosing_module find_enclosing_ast_node(:module) do |n| Rubyzen::Declarations::ModuleDeclaration.new(n, file_declaration) end end |
#in_class? ⇒ Boolean
Returns whether this constant is defined inside a class.
106 107 108 |
# File 'lib/rubyzen/declarations/constant_declaration.rb', line 106 def in_class? !enclosing_class.nil? end |
#in_module? ⇒ Boolean
Returns whether this constant is defined inside a module.
122 123 124 |
# File 'lib/rubyzen/declarations/constant_declaration.rb', line 122 def in_module? !enclosing_module.nil? end |
#name ⇒ String
Returns the constant name.
33 34 35 36 37 38 39 40 |
# File 'lib/rubyzen/declarations/constant_declaration.rb', line 33 def name case node.type when :casgn node.children[1].to_s when :const node.const_name end end |
#reference? ⇒ Boolean
Returns whether this is a constant reference (:const).
75 76 77 |
# File 'lib/rubyzen/declarations/constant_declaration.rb', line 75 def reference? node.type == :const end |
#scoped? ⇒ Boolean
Returns whether this constant is defined inside a class or module.
129 130 131 |
# File 'lib/rubyzen/declarations/constant_declaration.rb', line 129 def scoped? !top_level? end |
#top_level? ⇒ Boolean
Returns whether this constant is defined at file scope (not inside a class or module).
82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/rubyzen/declarations/constant_declaration.rb', line 82 def top_level? return false unless parent.is_a?(Rubyzen::Declarations::FileDeclaration) current_node = node while current_node current_node = current_node.parent return false if current_node && (current_node.type == :class || current_node.type == :module) end true end |
#value ⇒ String, ...
Returns the assigned value for constant assignments.
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/rubyzen/declarations/constant_declaration.rb', line 45 def value return nil unless assignment? value_node = node.children[2] return nil unless value_node case value_node.type when :str value_node.str_content when :int value_node.children[0] when :float value_node.children[0] when :true, :false value_node.type == :true else value_node.source end end |