Class: Passcard::Outputter

Inherits:
Object
  • Object
show all
Defined in:
lib/passcard/outputter.rb

Direct Known Subclasses

AsciiOutputter, HtmlOutputter

Constant Summary collapse

HEADERS =

50

'ABCDEFGHIJKLMNOPQRSTUVWXYZ 0123456789 αβγδθλμπϕ $£¥€¢' + # 50
'! # % & * < = > ? @ ✓ ∞ ♬ ♡ ♢ ♤ ♧ ☯ ☾ ✈ ☎ ☀ ☁ ☂ ☃ ★ ⌘ ♞  ✂ ✎ '

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(reader) ⇒ Outputter

Returns a new instance of Outputter.



8
9
10
11
# File 'lib/passcard/outputter.rb', line 8

def initialize(reader)
  self.reader = reader
  self.palette = Passcard::Palette.new
end

Instance Attribute Details

#gridObject

30



6
7
8
# File 'lib/passcard/outputter.rb', line 6

def grid
  @grid
end

#paletteObject

30



6
7
8
# File 'lib/passcard/outputter.rb', line 6

def palette
  @palette
end

#readerObject

30



6
7
8
# File 'lib/passcard/outputter.rb', line 6

def reader
  @reader
end

Class Method Details

.register(*args) ⇒ Object

Register one or more handler methods with this outputter. Passcard will then be able to use these methods to get the output from the outputter. For example, if you have an HtmlOutputter, you could do:

register :to_html, :to_xml

You could then do a Passcard.to_png and get the result of that method. The class which registers the method will receive the generator instance as the only argument, and the default implementation of initialize puts that into the passcard accessor.

You can also have different method names in the outputter by providing a hash:

register to_html: :create_html, to_xml: :create_xml

Raises:



30
31
32
33
34
35
36
37
# File 'lib/passcard/outputter.rb', line 30

def self.register(*args)
  hash = args.last.is_a?(Hash) ? args.pop : {}
  raise Passcard::Error, "You must register a method name!" if args.empty? && hash.empty?
  args.each{|name| hash[name] = name}
  hash.each do |name, method_name|
    ::Passcard.register_outputter(name, self, method_name)
  end
end

Instance Method Details

#col_headersObject



51
52
53
54
55
# File 'lib/passcard/outputter.rb', line 51

def col_headers
  headers   = self.class.const_get("COLUMN_HEADERS") rescue nil
  headers ||= self.class.const_get("HEADERS")
  headers.gsub(/ /, '').chars.take(grid.col_size)
end

#data_in(file) ⇒ Object



66
67
68
69
70
71
# File 'lib/passcard/outputter.rb', line 66

def data_in(file)
  content = File.read(file)
  regex   = /^\s*__END__\s*$/
  return "" if content.match(regex).nil?
  content.split(regex).last.strip
end

#get_grid(options = {}) ⇒ Object



57
58
59
60
61
62
63
64
# File 'lib/passcard/outputter.rb', line 57

def get_grid(options = {})
  random = options[:type].to_s.to_sym == :random
  coordinates = get_grid_coordinates(options)
  @grid = reader.random_grid(20, 30) if  random
  @grid = reader.slice(*coordinates) if !random
  use_palette :passcard
  @grid
end

#row_headersObject



45
46
47
48
49
# File 'lib/passcard/outputter.rb', line 45

def row_headers
  headers   = self.class.const_get("ROW_HEADERS") rescue nil
  headers ||= self.class.const_get("HEADERS")
  headers.gsub(/ /, '').chars.take(grid.row_size)
end

#use_palette(type, options = {}) ⇒ Object



39
40
41
42
43
# File 'lib/passcard/outputter.rb', line 39

def use_palette(type, options = {})
  options.merge!("n" => @grid.row_size) if @grid
  self.palette.type = type
  self.palette.options = options
end