Class: Apricot::Identifier
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#unqualified_name ⇒ Object
readonly
Returns the value of attribute unqualified_name.
Class Method Summary collapse
Instance Method Summary collapse
- #const_names ⇒ Object
- #constant? ⇒ Boolean
-
#fn? ⇒ Boolean
Does the identifier reference a fn on a namespace?.
- #hash ⇒ Object
-
#initialize(name) ⇒ Identifier
constructor
A new instance of Identifier.
- #inspect ⇒ Object
-
#meta ⇒ Object
Get the metadata of the object this identifier references, or nil.
-
#method? ⇒ Boolean
Does the identifier reference a method on a module?.
- #qualified? ⇒ Boolean
- #qualifier ⇒ Object
- #to_s ⇒ Object
- #to_sym ⇒ Object
Constructor Details
#initialize(name) ⇒ Identifier
Returns a new instance of Identifier.
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/apricot/identifier.rb', line 14 def initialize(name) @name = name if @name =~ /\A(?:[A-Z]\w*::)*[A-Z]\w*\z/ @constant = true @const_names = @name.to_s.split('::').map(&:to_sym) elsif @name =~ /\A(.+?)\/(.+)\z/ @qualified = true qualifier_id = Identifier.intern($1) raise 'Qualifier in qualified identifier must be a constant' unless qualifier_id.constant? @qualifier = qualifier_id.const_names.reduce(Object) do |mod, name| mod.const_get(name) end @unqualified_name = $2.to_sym else @unqualified_name = name end end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
3 4 5 |
# File 'lib/apricot/identifier.rb', line 3 def name @name end |
#unqualified_name ⇒ Object (readonly)
Returns the value of attribute unqualified_name.
3 4 5 |
# File 'lib/apricot/identifier.rb', line 3 def unqualified_name @unqualified_name end |
Class Method Details
.intern(name) ⇒ Object
7 8 9 10 |
# File 'lib/apricot/identifier.rb', line 7 def self.intern(name) name = name.to_sym @table[name] ||= new(name) end |
Instance Method Details
#const_names ⇒ Object
63 64 65 66 |
# File 'lib/apricot/identifier.rb', line 63 def const_names raise "#{@name} is not a constant" unless constant? @const_names end |
#constant? ⇒ Boolean
39 40 41 |
# File 'lib/apricot/identifier.rb', line 39 def constant? @constant end |
#fn? ⇒ Boolean
Does the identifier reference a fn on a namespace?
44 45 46 |
# File 'lib/apricot/identifier.rb', line 44 def fn? qualifier.is_a?(Namespace) && qualifier.fns.include?(@unqualified_name) end |
#hash ⇒ Object
78 79 80 |
# File 'lib/apricot/identifier.rb', line 78 def hash @name.hash end |
#inspect ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/apricot/identifier.rb', line 82 def inspect case @name when :true, :false, :nil, /\A(?:\+|-)?\d/ # Use arbitrary identifier syntax for identifiers that would otherwise # be parsed as keywords or numbers str = @name.to_s.gsub(/(\\.)|\|/) { $1 || '\|' } "#|#{str}|" when /\A#{Reader::IDENTIFIER}+\z/ @name.to_s else str = @name.to_s.inspect[1..-2] str.gsub!(/(\\.)|\|/) { $1 || '\|' } "#|#{str}|" end end |
#meta ⇒ Object
Get the metadata of the object this identifier references, or nil.
54 55 56 57 |
# File 'lib/apricot/identifier.rb', line 54 def qualifier.is_a?(Namespace) && qualifier.vars[@unqualified_name] && qualifier.vars[@unqualified_name]. end |
#method? ⇒ Boolean
Does the identifier reference a method on a module?
49 50 51 |
# File 'lib/apricot/identifier.rb', line 49 def method? !qualifier.is_a?(Namespace) && qualifier.respond_to?(@unqualified_name) end |
#qualified? ⇒ Boolean
35 36 37 |
# File 'lib/apricot/identifier.rb', line 35 def qualified? @qualified end |
#qualifier ⇒ Object
59 60 61 |
# File 'lib/apricot/identifier.rb', line 59 def qualifier @qualifier ||= Apricot.current_namespace end |
#to_s ⇒ Object
98 99 100 |
# File 'lib/apricot/identifier.rb', line 98 def to_s @name.to_s end |
#to_sym ⇒ Object
102 103 104 |
# File 'lib/apricot/identifier.rb', line 102 def to_sym @name end |