Class: Features2Cards::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/features2cards/cli.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args, out_stream = STDOUT, error_stream = STDERR) ⇒ CLI

Returns a new instance of CLI.



15
16
17
18
19
20
21
# File 'lib/features2cards/cli.rb', line 15

def initialize(args, out_stream = STDOUT, error_stream = STDERR)
  @args    = args
  @out_stream   = out_stream
  @error_stream = error_stream
  @paths   = []
  @options = default_options
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



9
10
11
# File 'lib/features2cards/cli.rb', line 9

def options
  @options
end

#pathsObject (readonly)

Returns the value of attribute paths.



8
9
10
# File 'lib/features2cards/cli.rb', line 8

def paths
  @paths
end

Class Method Details

.execute(args) ⇒ Object



11
12
13
# File 'lib/features2cards/cli.rb', line 11

def self.execute(args)
  new(args).execute!
end

Instance Method Details

#cardsObject



67
68
69
# File 'lib/features2cards/cli.rb', line 67

def cards
  features_to_cards(features)
end

#default_optionsObject



106
107
108
109
110
# File 'lib/features2cards/cli.rb', line 106

def default_options
  {
    :pdf_file => "cards.pdf"
  }
end

#execute!Object



23
24
25
26
27
28
29
30
31
32
# File 'lib/features2cards/cli.rb', line 23

def execute!
  parse!(@args)
  load_cucumber
  if feature_files.empty?
    usage
    exit
  end

  generate_pdf(cards)
end

#feature_filesObject



77
78
79
80
81
82
83
84
# File 'lib/features2cards/cli.rb', line 77

def feature_files
  potential_feature_files = @paths.map do |path|
    path = path.gsub(/\\/, '/') # In case we're on windows. Globs don't work with backslashes.
    path = path.chomp('/')
    File.directory?(path) ? Dir["#{path}/**/*.feature"] : path
  end.flatten.uniq
  potential_feature_files
end

#featuresObject



71
72
73
74
75
# File 'lib/features2cards/cli.rb', line 71

def features
  feature_files.map do |file|
    Cucumber::FeatureFile.new(file).parse(Cucumber::StepMother.new, {})
  end
end

#features_to_cards(features) ⇒ Object



91
92
93
94
95
# File 'lib/features2cards/cli.rb', line 91

def features_to_cards(features)
  features.map do |feature|
    [Card.for_feature(feature)]
  end.flatten
end

#generate_pdf(cards) ⇒ Object



97
98
99
# File 'lib/features2cards/cli.rb', line 97

def generate_pdf(cards)
  Prawn::Document.generate_cards(@options[:pdf_file], cards)
end

#load_cucumberObject



61
62
63
64
65
# File 'lib/features2cards/cli.rb', line 61

def load_cucumber
  Cucumber::Ast::Feature.class_eval do
    attr_reader :scenarios
  end
end

#parse!(args) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/features2cards/cli.rb', line 34

def parse!(args)
  @args = args
  @args.extend(::OptionParser::Arguable)
  @args.options  do |opts|
    opts.banner = ["Usage: features2cards [options] [ [FILE|DIR] ]+", "",
      "Examples:",
      "features2cards features2cards.feature",
      "features2cards examples/i18n/it",
    ].join("\n")
    opts.on("-o", "--out [FILE]",
      "Specify pdf output file (Default: #{@options[:pdf_file]}).") do |v|
      @options[:pdf_file] = v
    end
    opts.on_tail("--version", "Show version.") do
      @out_stream.puts VERSION::STRING
      Kernel.exit
    end
    opts.on_tail("-h", "--help", "You're looking at it.") do
      @out_stream.puts opts.help
      Kernel.exit
    end
end.parse!

  # Whatever is left after option parsing is the FILE arguments
  @paths += args
end

#parserObject



87
88
89
# File 'lib/features2cards/cli.rb', line 87

def parser
  @parser ||= Cucumber::Parser::FeatureParser.new
end

#usageObject



101
102
103
104
# File 'lib/features2cards/cli.rb', line 101

def usage
  @error_stream.puts "ERROR: No feature files given"
  @error_stream.puts "Type 'features2cards --help' for usage."
end