class ESC_POS::Specifications::Base

Public Class Methods

new() click to toggle source
# File lib/esc-pos/specifications/base.rb, line 15
def initialize
  raise NoMethodError, 'This is just a base class, use it to create custom specifications.'
end
set(name, value) click to toggle source
# File lib/esc-pos/specifications/base.rb, line 11
def self.set(name, value)
  specifications[name] = value
end
specifications() click to toggle source
# File lib/esc-pos/specifications/base.rb, line 7
def self.specifications
  @@specifications ||= {}
end

Public Instance Methods

cut_paper(partial_cut = false) click to toggle source
# File lib/esc-pos/specifications/base.rb, line 85
def cut_paper(partial_cut = false)
  if partial_cut
    "#{get_value(:esc_code)}m"
  else
    "#{get_value(:esc_code)}i"
  end
end
feed_lines(lines) click to toggle source
# File lib/esc-pos/specifications/base.rb, line 39
def feed_lines(lines)
  return '' if lines == 1

  "#{get_value(:esc_code)}\x64" << (lines - 1).chr
end
get_value(key) click to toggle source
# File lib/esc-pos/specifications/base.rb, line 93
def get_value(key)
  self.class.specifications[key]
end
go_to_cut() click to toggle source
# File lib/esc-pos/specifications/base.rb, line 77
def go_to_cut
  feed_lines(get_value(:lines_to_cut_line))
end
go_to_cut_and_cut(spaces_after = 0) click to toggle source
# File lib/esc-pos/specifications/base.rb, line 81
def go_to_cut_and_cut(spaces_after = 0)
  "#{get_value(:gs_code)}V#{65.chr}#{spaces_after}\r"
end
re_encode_text(txt) click to toggle source
# File lib/esc-pos/specifications/base.rb, line 97
def re_encode_text(txt)
  return txt unless get_value(:special_encoding)

  txt.encode(get_value(:special_encoding))
rescue Encoding::UndefinedConversionError
  txt.force_encoding(get_value(:special_encoding))
end
render(options = {}) click to toggle source
# File lib/esc-pos/specifications/base.rb, line 45
def render(options = {})
  template_filename = options.fetch(:template, self.class.to_s.underscore)

  template = File.read(File.join(Settings.templates_path, "#{template_filename}.esc_pos.erb"))
  erb = ERB.new(template, 0, '%<>-')
  erb.result(binding)
end
reset_printer() click to toggle source
# File lib/esc-pos/specifications/base.rb, line 35
def reset_printer
  "#{get_value(:esc_code)}#{get_value(:reset_printer_code)}"
end
set_alignment(alignment) click to toggle source
# File lib/esc-pos/specifications/base.rb, line 57
def set_alignment(alignment)
  "#{get_value(:esc_code)}#{get_value(:alignment_selector_code)}#{get_value(alignment)}"
end
set_character_code_table(character_code) click to toggle source
# File lib/esc-pos/specifications/base.rb, line 69
def set_character_code_table(character_code)
  "#{get_value(:esc_code)}#{get_value(:character_table_selector_code)}#{get_value(character_code).chr}"
end
set_color(color) click to toggle source
# File lib/esc-pos/specifications/base.rb, line 61
def set_color(color)
  "#{get_value(:esc_code)}#{get_value(:color_selector_code)}#{get_value(color)}"
end
set_font(font) click to toggle source
# File lib/esc-pos/specifications/base.rb, line 53
def set_font(font)
  "#{get_value(:esc_code)}#{get_value(:font_selector_code)}#{get_value(font)}"
end
set_international_character_set(character_set) click to toggle source
# File lib/esc-pos/specifications/base.rb, line 65
def set_international_character_set(character_set)
  "#{get_value(:esc_code)}#{get_value(:international_character_selector_code)}#{get_value(character_set).chr}"
end
split_line(char = '-') click to toggle source
# File lib/esc-pos/specifications/base.rb, line 73
def split_line(char = '-')
  text(char * get_value(:width), :font => :font_b)
end
text(txt, options = {}) click to toggle source
# File lib/esc-pos/specifications/base.rb, line 19
def text(txt, options = {})
  font = options.fetch(:font, :font_b)
  color = options.fetch(:color, :color_black)

  formatted_text = ''
  formatted_text << set_font(font)
  formatted_text << set_alignment(options[:align_type]) if options[:align_type]
  formatted_text << set_color(color)

  if txt
    formatted_text << re_encode_text(txt)
  end

  formatted_text
end