Class: Jsus::Tag

Inherits:
Object
  • Object
show all
Defined in:
lib/jsus/tag.rb

Overview

Tag is basically just a string that contains a package name and a name for class (or not necessarily a class) which the given SourceFile provides/requires/extends/replaces.

"Core/Class" is a tag

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, options = {}) ⇒ Tag

Creates a tag from given name/options.

The way it works may seem a bit tricky but actually it parses name/options combinations in different ways and may be best described by examples:

Between all those, tags b,c,d and e are equal, meaning they all use the same spot in Hash or wherever else.

Examples:


a = Tag.new("Class")      # :package_name => "",     :name => "Class", :external => false
b = Tag.new("Core/Class") # :package_name => "Core", :name => "Class", :external => true
core = Package.new(...) # let's consider its name is 'Core'
c = Tag.new("Class", :package => core) # :package_name => "Core", :name => "Class", :external => false
d = Tag.new("Core/Class", :package => core) # :package_name => "Core", :name => "Class", :external => false
mash = Package.new(...) # let's consider its name is 'Mash'
e = Tag.new("Core/Class", :package => mash) # :package_name => "Core", :name => "Class", :external => true

Parameters:

  • name (String)

    tag name

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :package_name (String)

    owner package name

  • :package (Jsus::Package)

    :owner package

  • :external (Boolean)

    whether tag is considered external



41
42
43
44
45
46
# File 'lib/jsus/tag.rb', line 41

def initialize(name, options = {})
  normalized_options = Tag.normalize_name_and_options(name, options)
  [:name, :package, :package_name, :external].each do |field|
    self.send("#{field}=", normalized_options[field])
  end
end

Instance Attribute Details

#externalObject

Whether tag is external



12
13
14
# File 'lib/jsus/tag.rb', line 12

def external
  @external
end

#packageObject

Owner package



10
11
12
# File 'lib/jsus/tag.rb', line 10

def package
  @package
end

Class Method Details

.[](*args) ⇒ Object

Alias for Tag.new



60
61
62
# File 'lib/jsus/tag.rb', line 60

def self.[](*args)
  new(*args)
end

.name_and_options_to_full_name(name, options = {}) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



154
155
156
# File 'lib/jsus/tag.rb', line 154

def self.name_and_options_to_full_name(name, options = {})
  normalized_options_to_full_name(normalize_name_and_options(name, options))
end

.new(tag_or_name, *args, &block) ⇒ Object

When given a tag instead of tag name, just returns it.



50
51
52
53
54
55
56
# File 'lib/jsus/tag.rb', line 50

def self.new(tag_or_name, *args, &block)
  if tag_or_name.kind_of?(Tag)
    tag_or_name
  else
    super
  end
end

.normalize_name_and_options(name, options = {}) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/jsus/tag.rb', line 130

def self.normalize_name_and_options(name, options = {})
  result = {}
  name.gsub!(%r(^(\.)?/), "")
  if name.index("/")
    parsed_name = name.split("/")
    result[:package_name], result[:name] = parsed_name[0..-2].join("/"), parsed_name[-1]
    result[:external] = options[:package] ? (result[:package_name] != options[:package].name) : true
  else
    if options[:package]
      result[:package] = options[:package]
      result[:package_name] = options[:package].name
    end
    result[:name] = name
  end
  result[:package_name] = normalize_package_name(result[:package_name]) if result[:package_name]
  result
end

.normalize_package_name(name) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



159
160
161
162
163
164
# File 'lib/jsus/tag.rb', line 159

def self.normalize_package_name(name)
  package_chunks = name.split("/")
  package_chunks.map do |pc|
    Jsus::Util::Inflection.random_case_to_mixed_case(pc)
  end.join("/")
end

.normalized_options_to_full_name(options) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



149
150
151
# File 'lib/jsus/tag.rb', line 149

def self.normalized_options_to_full_name(options)
  [options[:package_name], options[:name]].compact.join("/")
end

Instance Method Details

#==(other) ⇒ Object



103
104
105
106
107
108
109
# File 'lib/jsus/tag.rb', line 103

def ==(other)
  if other.kind_of?(Tag)
    self.name == other.name
  else
    super
  end
end

#empty?Boolean

Returns whether name is empty.

Returns:

  • (Boolean)

    whether name is empty



98
99
100
# File 'lib/jsus/tag.rb', line 98

def empty?
  @name.empty?
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


112
113
114
# File 'lib/jsus/tag.rb', line 112

def eql?(other)
  self.==(other)
end

#external?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/jsus/tag.rb', line 68

def external?
  !!external
end

#hashObject



117
118
119
# File 'lib/jsus/tag.rb', line 117

def hash
  self.name.hash
end

#inspectString

Returns human-readable representation.

Returns:

  • (String)

    human-readable representation



123
124
125
# File 'lib/jsus/tag.rb', line 123

def inspect
  "<Jsus::Tag: #{name}>"
end

#name(options = {}) ⇒ String Also known as: to_s

Note:

only non-external tags support short forms.

Returns a well-formed name for the tag.

Examples:

Tag.new('Core/Class').name(:short => true) # => 'Core/Class'
core = Package.new(...) # let's consider its name is 'Core'
Tag.new('Core/Class', :package => core).name(:short => true) # => 'Class'

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :short (Boolean)

    whether the tag should try using short form

Returns:

  • (String)

    a well-formed name for the tag.



81
82
83
84
85
86
87
# File 'lib/jsus/tag.rb', line 81

def name(options = {})
  if !package_name || package_name.empty? || (options[:short] && !external?)
    @name
  else
    "#{package_name}/#{@name}"
  end
end

#name=(new_value) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



172
173
174
# File 'lib/jsus/tag.rb', line 172

def name=(new_value)
  @name = new_value
end

#package_nameObject



92
93
94
# File 'lib/jsus/tag.rb', line 92

def package_name
  @package_name ||= (@package ? @package.name : "")
end

#package_name=(new_value) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



167
168
169
# File 'lib/jsus/tag.rb', line 167

def package_name=(new_value)
  @package_name = new_value
end