class Nova::Constructor

Creates a star from a {Nova.create} call.

Public Class Methods

new(options, &block) click to toggle source

Initialize the constructor.

@param options [Hash] the definition of the star. It should

be a single key-value pair, with the first key being the
type of star, and the first value being the name of the
star.

@yield for the construction of the new Star. @example

Constructor.new(some_star: :another_star) do
  on :some_event do; end
end.create # => Nova::Star/SomeStar.another_star
# File lib/nova/constructor.rb, line 17
def initialize(options, &block)
  @options = options
  @block = block
end

Public Instance Methods

data() click to toggle source

Returns information about the star, like the type, the required platforms, and what it’s named.

@return [Hash<Symbol, Object>]

# File lib/nova/constructor.rb, line 45
def data
  @_data ||= {
    :as   => @options.values.first,
    :type => @options.keys.first
  }
end
modify_or_create() click to toggle source

Modifies an already existing star if it exists, or creates it if it doesn’t.

@raise [NoStarError] when the star type couldn’t be found. @return [Class] a subclass of the star type.

# File lib/nova/constructor.rb, line 27
def modify_or_create
  star_type = Star.types[data[:type]]

  raise NoStarError,
    "Could not find star type #{data[:type]}." unless star_type

  if Star.stars[data[:type]][data[:as]]
    handle_existing
  else
    handle_new star_type
  end
end

Private Instance Methods

handle_existing() click to toggle source

Handles an existing star. Executes the block in the instance of the star, adds the definition’s required_platforms to the stars, and then returns the star.

@return [Class]

# File lib/nova/constructor.rb, line 59
def handle_existing
  star = Star.stars[data[:type]][data[:as]]

  star.class_exec &@block

  star
end
handle_new(star_type) click to toggle source

Handles defining a new star. Creates a class as a subclass of the star, sets its name and type, and executes the block in the instance of the star. Adds the required_platform to the star, sets the star to {Star.stars}, and returns the new star.

@param star_type [Class] the type of star it is. @return [Class]

# File lib/nova/constructor.rb, line 74
def handle_new(star_type)
  new_star = Class.new(star_type)
  new_star.as   = data[:as]
  new_star.type = data[:type]
  new_star.class_exec &@block

  Star.stars[data[:type]][data[:as]] = new_star
end