Class: ESC_POS::Specifications::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/esc-pos/specifications/base.rb

Direct Known Subclasses

Epson, Epson::TM_U220

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBase

Returns a new instance of Base.

Raises:

  • (NoMethodError)


15
16
17
# 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

Class Method Details

.set(name, value) ⇒ Object



11
12
13
# File 'lib/esc-pos/specifications/base.rb', line 11

def self.set(name, value)
  specifications[name] = value
end

.specificationsObject



7
8
9
# File 'lib/esc-pos/specifications/base.rb', line 7

def self.specifications
  @@specifications ||= {}
end

Instance Method Details

#cut_paper(partial_cut = false) ⇒ Object



85
86
87
88
89
90
91
# 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) ⇒ Object



39
40
41
42
43
# 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) ⇒ Object



93
94
95
# File 'lib/esc-pos/specifications/base.rb', line 93

def get_value(key)
  self.class.specifications[key]
end

#go_to_cutObject



77
78
79
# 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) ⇒ Object



81
82
83
# 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) ⇒ Object



97
98
99
100
101
102
103
# 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 = {}) ⇒ Object



45
46
47
48
49
50
51
# 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_printerObject



35
36
37
# 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) ⇒ Object



57
58
59
# 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) ⇒ Object



69
70
71
# 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) ⇒ Object



61
62
63
# 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) ⇒ Object



53
54
55
# 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) ⇒ Object



65
66
67
# 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 = '-') ⇒ Object



73
74
75
# 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 = {}) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# 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