class Apricot::Identifier

Attributes

name[R]
unqualified_name[R]

Public Class Methods

intern(name) click to toggle source
# File lib/apricot/identifier.rb, line 7
def self.intern(name)
  name = name.to_sym
  @table[name] ||= new(name)
end
new(name) click to toggle source
# 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

Public Instance Methods

const_names() click to toggle source
# File lib/apricot/identifier.rb, line 63
def const_names
  raise "#{@name} is not a constant" unless constant?
  @const_names
end
constant?() click to toggle source
# File lib/apricot/identifier.rb, line 39
def constant?
  @constant
end
fn?() click to toggle source

Does the identifier reference a fn on a namespace?

# File lib/apricot/identifier.rb, line 44
def fn?
  qualifier.is_a?(Namespace) && qualifier.fns.include?(@unqualified_name)
end
hash() click to toggle source
# File lib/apricot/identifier.rb, line 78
def hash
  @name.hash
end
inspect() click to toggle source
# 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() click to toggle source

Get the metadata of the object this identifier references, or nil.

# File lib/apricot/identifier.rb, line 54
def meta
  qualifier.is_a?(Namespace) && qualifier.vars[@unqualified_name] &&
    qualifier.vars[@unqualified_name].apricot_meta
end
method?() click to toggle source

Does the identifier reference a method on a module?

# File lib/apricot/identifier.rb, line 49
def method?
  !qualifier.is_a?(Namespace) && qualifier.respond_to?(@unqualified_name)
end
qualified?() click to toggle source
# File lib/apricot/identifier.rb, line 35
def qualified?
  @qualified
end
qualifier() click to toggle source
# File lib/apricot/identifier.rb, line 59
def qualifier
  @qualifier ||= Apricot.current_namespace
end
to_s() click to toggle source
# File lib/apricot/identifier.rb, line 98
def to_s
  @name.to_s
end
to_sym() click to toggle source
# File lib/apricot/identifier.rb, line 102
def to_sym
  @name
end

Private Instance Methods

initialize_copy(other) click to toggle source

Copying Identifiers is not allowed.

# File lib/apricot/identifier.rb, line 69
def initialize_copy(other)
  raise TypeError, "copy of #{self.class} is not allowed"
end