class Take::Project::Target

Attributes

project[R]

Public Class Methods

new(hash, project, &block) click to toggle source

Initializes the target.

@param [Project] project the project that this target is

a part of.

@param [Hash] hash a hash of the options for this target. If

the any key is not one of the below, it is assumed to be the
target key-pair; there may only be one target key-pair (any
more will cause an ArgumentError).  If the key of the key
pair is a symbol, it is considered an "invalid" target,
meaning it _must not_ be a default target.

@option hash [Symbol] :type The type of target it is. This is

normally assumed from the extension.

@option hash [Array<String>] :depends, :depends_on Files that

the target depends on.  This is normally set by the value
of the target key pair.

@option hash [Symbol, String] :name The name of the target.

This is normally set by the key of the target key pair.
# File lib/take/project/target.rb, line 23
def initialize(hash, project, &block)
  raise ArgumentError, "Targets require blocks" \
    unless block_given?
  @block = block
  @project = project
  @dependencies = []
  handle_arguments(hash)
  guess_type unless @type
end

Public Instance Methods

invalid?() click to toggle source
# File lib/take/project/target.rb, line 33
def invalid?
  @target.is_a?(Symbol)
end
name() click to toggle source
# File lib/take/project/target.rb, line 37
def name
  if invalid?
    @target
  else
    @target.to_path
  end
end
sub(what, to) click to toggle source
# File lib/take/project/target.rb, line 45
def sub(what, to)
  @dependencies.map do |dep|
    if dep.extname == what
      dep.sub_ext(to)
    else
      dep
    end
  end
end
to_a() click to toggle source
# File lib/take/project/target.rb, line 55
def to_a
  @dependencies.map(&:to_path)
end

Private Instance Methods

guess_type() click to toggle source
# File lib/take/project/target.rb, line 63
def guess_type
  if invalid?
    @type = :invalid
  else
    @type = @target.type
  end
end
handle_arguments(hash) click to toggle source
# File lib/take/project/target.rb, line 71
def handle_arguments(hash)
  [:type, :depends, :depends_on, :name]
  defined_target = false
  raise ArgumentError, "Expected Hash, got #{hash.class}" \
    unless hash.is_a?(Hash)

  hash.each do |key, value|
    case key
    when :type
      @type = value
    when :depends, :depends_on
      @dependencies.concat(Array(value))
    when :name
      @target = value
    when String, Symbol
      raise ArgumentError, "Target already defined!  Maybe you" \
        " misspelled.  Unknown option #{key.inspect}." \
        if defined_target
      @target = key
      defined_target = true
      @dependencies.concat(Array(value))
    else
      raise ArgumentError, "Unknown option #{key.inspect}."
    end
  end

  @target = project.file(@target) unless invalid?
  @dependencies.map! { |dep| project.file(dep) }
end