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

Constant Summary collapse

SEPARATOR =

Default separator of full name parts

"/".freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(full_name) ⇒ Tag

Creates a tag from given string.

Examples:


a = Tag.new("Class")         # :namespace => nil,             :name => "Class"
b = Tag.new("Core/Class")    # :namespace => "Core",          :name => "Class"
c = Tag.new("Mootools/Core") # :namespace => "Mootools/Core", :name => "Class"

Parameters:

  • name (String)

    full tag name



25
26
27
# File 'lib/jsus/tag.rb', line 25

def initialize(full_name)
  self.full_name = full_name
end

Instance Attribute Details

#namespaceString

Name without namespace

Returns:

  • (String)


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

def namespace
  @namespace
end

Class Method Details

.[](*args) ⇒ Object

Alias for Tag.new



65
66
67
# File 'lib/jsus/tag.rb', line 65

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

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

Note:

When given a tag instead of tag name, returns the input

Instantiates a tag.



55
56
57
58
59
60
61
# File 'lib/jsus/tag.rb', line 55

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

Instance Method Details

#==(other) ⇒ Object



118
119
120
121
122
123
124
# File 'lib/jsus/tag.rb', line 118

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

#empty?Boolean

Returns whether name is empty.

Returns:

  • (Boolean)

    whether name is empty



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

def empty?
  !@name || @name.empty?
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


127
128
129
# File 'lib/jsus/tag.rb', line 127

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

#full_nameString Also known as: to_s

Full name, including namespace

Returns:

  • (String)


32
33
34
35
36
37
38
# File 'lib/jsus/tag.rb', line 32

def full_name
  if namespace
    "#{namespace}#{SEPARATOR}#{name}"
  else
    "#{name}"
  end
end

#full_name=(full_name) ⇒ Object

Assigns full name

Parameters:

  • full_name (String)

    full tag name, including namespace



45
46
47
48
49
50
# File 'lib/jsus/tag.rb', line 45

def full_name=(full_name)
  @full_name     = full_name
  name_parts     = @full_name.split(SEPARATOR)
  self.namespace = name_parts[0..-2].join(SEPARATOR) if name_parts.size > 1
  self.name      = name_parts[-1]
end

#hashObject



132
133
134
# File 'lib/jsus/tag.rb', line 132

def hash
  [self.name, self.namespace].hash
end

#inspectString

Returns human-readable representation.

Returns:

  • (String)

    human-readable representation



138
139
140
# File 'lib/jsus/tag.rb', line 138

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

#nameString

Returns the last part of the tag.

Examples:

Tag.new('Core/Class').name # => 'Class'

Returns:

  • (String)

    last part of the tag



76
77
78
# File 'lib/jsus/tag.rb', line 76

def name
  @name
end

#name=(name) ⇒ Object

Set name.

Parameters:

  • name (String)


83
84
85
# File 'lib/jsus/tag.rb', line 83

def name=(name)
  @name = normalize(name)
end

#normalize(string) ⇒ String

Normalizes name or namespace (converts snake_case to MixedCase)

Parameters:

  • string (String)

Returns:

  • (String)


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

def normalize(string)
  return unless string
  return string if string.include?("*")
  parts = string.split(SEPARATOR)
  parts.map {|part| Util::Inflection.random_case_to_mixed_case_preserve_dots(part) }.join(SEPARATOR)
end