summaryrefslogtreecommitdiff
path: root/lib/xsay.rb
blob: 44a74738c20131878993b8b80d8c5aa96e8d79fa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
require "thor"
require "colorize"
require "xsay/render"
require "xsay/version"

module Xsay
  class CLI < Thor
    ANIMALS=Dir[File.expand_path("xsay/templates/*.template", File.dirname(__FILE__))]
    class_option :colour, default: :default, required: false
    class_option :distance, default: 0, required: false, type: :numeric
    class_option :speed, default: 1, required: false, type: :numeric

    ANIMALS.each do |filename|
      animal = File.basename(filename).split(".")[0]

      desc "#{animal} <message>", "xsay #{animal} hello"
      define_method animal do |*args|
        renderer.render(args, IO.read(filename))
      end
    end

    desc "all <message>", "xsay all hello"
    def all(*args)
      animals = public_methods - Thor.new.methods - [:random, :all]
      animals.each { |x| public_send(x, *args) }
      nil
    end

    desc "random <message>", "xsay random hello"
    def random(*args)
      random_colour = (String.colors + [:rainbow]).sample
      renderer(random_colour).render(args, IO.read(ANIMALS.shuffle.sample))
    end

    private

    def renderer(colour = options[:colour].to_sym)
      Render.new(
        colour: colour,
        distance: options[:distance],
        speed: options[:speed]
      )
    end
  end
end