Sha256: 544beb341e663e1638567b5adb8ee475870b85c1cb1446ea5af876f221442fca

Contents?: true

Size: 1.19 KB

Versions: 2

Compression:

Stored size: 1.19 KB

Contents

# -*- encoding: utf-8 -*-

module GitShizzle::Dsl
  class CommandCollection
    def initialize
      @commands = []
    end

    def add_command(command)
      raise DuplicateCommandDefinitionError.new(command) if @commands.any? { |c| c.identifier == command.identifier}

      @commands << command
    end

    def load(contents = nil)
      data = contents

      unless data
        filenames = ['commands', 'Commands', 'commands.rb', 'Commands.rb'].map do |f|
          "#{File.dirname(__FILE__)}/../../#{f}"
        end
        filename = filenames.find { |f| File.file?(f) }
        raise GitShizzle::Dsl::NoCommandFileFoundError.new if filename.nil?

        data = File.read(filename)
      end

      dsl.instance_eval(data, "./#{filename}")
    end

    def each(&block)
      @commands.each do |command|
        if block_given?
          block.call command
        else
          yield command
        end
      end
    end

    def find(identifier)
      @commands.find { |c| c.identifier == identifier } or raise GitShizzle::Dsl::CommandNotFoundError.new(identifier)
    end

    private
    def dsl
      GitShizzle::Dsl::Dsl.new(self)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
git_shizzle-0.2.8 lib/git_shizzle/dsl/command_collection.rb
git_shizzle-0.2.6 lib/git_shizzle/dsl/command_collection.rb