Class: Apricot::Identifier

Inherits:
Object show all
Defined in:
lib/apricot/identifier.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/apricot/identifier.rb', line 3

def name
  @name
end

#unqualified_nameObject (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_namesObject



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

Returns:

  • (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?

Returns:

  • (Boolean)


44
45
46
# File 'lib/apricot/identifier.rb', line 44

def fn?
  qualifier.is_a?(Namespace) && qualifier.fns.include?(@unqualified_name)
end

#hashObject



78
79
80
# File 'lib/apricot/identifier.rb', line 78

def hash
  @name.hash
end

#inspectObject



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

#metaObject

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



54
55
56
57
# 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?Boolean

Does the identifier reference a method on a module?

Returns:

  • (Boolean)


49
50
51
# File 'lib/apricot/identifier.rb', line 49

def method?
  !qualifier.is_a?(Namespace) && qualifier.respond_to?(@unqualified_name)
end

#qualified?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/apricot/identifier.rb', line 35

def qualified?
  @qualified
end

#qualifierObject



59
60
61
# File 'lib/apricot/identifier.rb', line 59

def qualifier
  @qualifier ||= Apricot.current_namespace
end

#to_sObject



98
99
100
# File 'lib/apricot/identifier.rb', line 98

def to_s
  @name.to_s
end

#to_symObject



102
103
104
# File 'lib/apricot/identifier.rb', line 102

def to_sym
  @name
end