Module: Nova::Common::StarManagement::ClassMethods

Included in:
Star
Defined in:
lib/nova/common/star_management.rb

Overview

Class methods.

Instance Attribute Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Class

Retrieves the star with the given name.

Examples:

Nova::Star/Type.klass

Returns:

  • (Class)


104
105
106
107
108
109
110
# File 'lib/nova/common/star_management.rb', line 104

def method_missing(method, *args, &block)
  if (stars.key?(method) || stars[type].key?(method)) && args.length == 0
    stars[method] || stars[type][method]
  else
    super
  end
end

Instance Attribute Details

#asSymbol

The name of the star.

Returns:

  • (Symbol)


92
93
94
# File 'lib/nova/common/star_management.rb', line 92

def as
  @as
end

#remoteModule

The remote to use, by default, for stars.

Returns:

  • (Module)


83
84
85
# File 'lib/nova/common/star_management.rb', line 83

def remote
  @remote ||= Remote::Fake
end

#typeSymbol

The type of the star.

Returns:

  • (Symbol)


97
98
99
# File 'lib/nova/common/star_management.rb', line 97

def type
  @type
end

Instance Method Details

#/(other_class) ⇒ Class

Just a way to write it; syntaxic sugar. It returns what was passed.

Examples:

Nova::Star/Type.something

Parameters:

  • other_class (Class)

Returns:

  • (Class)

    other_class.



75
76
77
# File 'lib/nova/common/star_management.rb', line 75

def /(other_class)
  other_class
end

#[](star_name) ⇒ Hash, Class

An accessor for #stars.

Parameters:

  • star_name (Symbol)

Returns:

  • (Hash, Class)


56
57
58
# File 'lib/nova/common/star_management.rb', line 56

def [](star_name)
  stars[star_name]
end

#from_target(target) ⇒ nil, Class

Retrieves a star from a given target. The target should be in the format [<type>.]<star name>.

Parameters:

  • target (String)

    the target star.

Returns:

  • (nil, Class)

    nil if the star doesn’t exist, the star itself otherwise.



118
119
120
121
122
123
# File 'lib/nova/common/star_management.rb', line 118

def from_target(target)
  type, star_name, action = target.scan(%r{\A(?:([\w]+)\.)?([\w]+)(?:\.([\w]+?))?\z}).first

  type ||= :star
  stars[type.intern][star_name.intern]
end

#inherited(klass) ⇒ 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.

When the star is subclassed, add the subclass automatically to the star type list, unless it doesn’t have a proper name.



24
25
26
27
28
29
# File 'lib/nova/common/star_management.rb', line 24

def inherited(klass)
  return unless klass.name

  type = klass.name.gsub(/([A-Z])/) { |a| "_#{a.downcase}" }.gsub("::", "/")[1..-1].intern
  klass.star_type(type)
end

#inspectString

Cleans up the inspect a little bit.

Returns:

  • (String)


63
64
65
66
# File 'lib/nova/common/star_management.rb', line 63

def inspect
  @_inspect ||=
    ancestors.take_while { |x| x <= Star }.map(&:name).reverse.join("/").gsub(/\/\z/, "." + as.to_s)
end

#star_type(name) ⇒ self

Adds the Star to the type list.

Parameters:

  • name (Symbol)

    the name of the star.

Returns:

  • (self)


35
36
37
38
39
40
41
# File 'lib/nova/common/star_management.rb', line 35

def star_type(name)
  types.delete_if { |_, v| v == self }
  types[name] = self
  self.type   = name
  stars[name] = {}
  self
end

#starsHash{Symbol => Class}

All of the stars that have been defined. These are different from star types because they contain information such as events.

Returns:

  • (Hash{Symbol => Class})


48
49
50
# File 'lib/nova/common/star_management.rb', line 48

def stars
  @@stars ||= {}
end

#typesHash{Symbol => Class}

All of the types of stars. Should be a key-value pair, with the key being the name, and the value being the class.

Returns:

  • (Hash{Symbol => Class})


15
16
17
# File 'lib/nova/common/star_management.rb', line 15

def types
  @@types ||= {}
end