printr 0.2.1 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. data/lib/printr.rb +55 -24
  2. data/printr.gemspec +1 -1
  3. metadata +4 -12
data/lib/printr.rb CHANGED
@@ -4,22 +4,25 @@ require 'erb'
4
4
  require 'serialport'
5
5
  module Printr
6
6
  require 'printr/engine' if defined?(Rails)
7
+ mattr_accessor :encoding
8
+ @@encoding = 'ISO-8859-15'
7
9
  mattr_accessor :debug
8
10
  @@debug = false
9
11
  mattr_accessor :serial_baud_rate
10
12
  @@serial_baud_rate = 9600
11
13
  mattr_accessor :scope
12
- @@scope = 'printr'
13
- mattr_accessor :printr_source #:yaml or :active_record
14
- @@printr_source = :yaml
15
- mattr_accessor :logger
14
+ @@scope = 'printr' # essential what views directory to look in for the
15
+ # templates
16
+ mattr_accessor :printr_source #:yaml or :active_record
17
+ @@printr_source = :yaml # :active_record => {:class_name => ClassName, :name => :model_field_name, :path => :model_path_name
18
+ # E.G. printr_model = {:class_name => Printer, :name => :short_name, :path => :location }
19
+ # to create the list of printers it will call:
20
+ # Printer.all.each { |p| @printrs[p.send(Printr.printr_model[:name].snake_case.to_sym)] = p.send(Printr.printr_model[:path]) }
21
+ # so if you have a printer named bar_printer, then you can print to it with @printr.bar_printer 'textl'
22
+ mattr_accessor :logger # Expects STDOUT[Default], A logger, or a File Descriptor via File.open
16
23
  @@logger = STDOUT
17
- # :active_record => {:class_name => ClassName, :name => :model_field_name, :path => :model_path_name
18
- # E.G. printr_model = {:class_name => Printer, :name => :short_name, :path => :location }
19
- # to create the list of printers it will call:
20
- # Printer.all.each { |p| @printrs[p.send(Printr.printr_model[:name].snake_case.to_sym)] = p.send(Printr.printr_model[:path]) }
21
- # so if you have a printer named bar_printer, then you can print to it with @printr.bar_printer 'textl'
22
- mattr_accessor :sanitize_tokens #pair list of needle regex, replace must be by 2s, i.e. [/[abc]/,"x",/[123]/,'0']
24
+
25
+ mattr_accessor :sanitize_tokens #pair list of needle regex, replace must be by 2s, i.e. [/[abc]/,"x",/[123]/,'0']
23
26
  @@sanitize_tokens = []
24
27
  mattr_accessor :codes
25
28
  @@codes = {
@@ -34,11 +37,15 @@ module Printr
34
37
  return Printr::Machine.new
35
38
  end
36
39
  def self.log(text)
40
+ return if not Printr.debug
41
+ text = "[Printr] #{text}" if not text.include?('[Printr]')
37
42
  if @@logger == STDOUT
38
- puts text
43
+ @@logger.puts
39
44
  else
40
45
  if @@logger.respond_to? :info
41
46
  @@logger.info text
47
+ elsif @@logger.respond_to? :puts
48
+ @@logger.puts text
42
49
  else
43
50
  puts text
44
51
  end
@@ -48,12 +55,16 @@ module Printr
48
55
  yield self
49
56
  end
50
57
  def self.get_printers
58
+ Printr.log "Getting Printers"
51
59
  if @@printr_source == :yaml then
60
+ Printr.log "printr_source == :yaml"
52
61
  @@conf = YAML::load(File.open("#{RAILS_ROOT}/config/printrs.yml"))
53
62
  elsif @@printr_source.class == Hash then
54
63
  if @@printr_source[:active_record] then
64
+ Printr.log "printr_source == :active_record"
55
65
  @@printr_source[:active_record][:class_name].all.each do |p|
56
66
  key = p.send(@@printr_source[:active_record][:name]).to_sym
67
+ Printr.log "conf[#{key}] = #{p.send(@@printr_source[:active_record][:path])}"
57
68
  @@conf[key] = p.send(@@printr_source[:active_record][:path])
58
69
  end
59
70
  end
@@ -64,6 +75,7 @@ module Printr
64
75
  @@conf.each do |key,value|
65
76
  @@printrs[key] = value
66
77
  end
78
+ Printr.log "open_printers returning: " + @@printrs.inspect
67
79
  @@printrs
68
80
  end
69
81
 
@@ -92,6 +104,7 @@ module Printr
92
104
  end
93
105
 
94
106
  def print_to(key,text)
107
+ Printr.log "[Printr] print_to(#{key},#{text[0..55]})"
95
108
  key = key.to_sym
96
109
  if text.nil? then
97
110
  Printr.log "[Printr] Umm...text is nil dudes..."
@@ -101,7 +114,7 @@ module Printr
101
114
  if text.nil? then
102
115
  Printr.log "[Printr] Sanitize nillified the text..."
103
116
  end
104
- Printr.log "[Printr] Going ahead with printing of: " + text.to_s[0..100]
117
+ Printr.log "[Printr] Going ahead with printing of: " + text.to_s[0..55]
105
118
 
106
119
  Printr.log "[Printr] Printing to device..." + Printr.conf[key]
107
120
  begin
@@ -115,6 +128,8 @@ module Printr
115
128
  end
116
129
  begin
117
130
  File.open(Printr.conf[key],'w:ISO8859-15') do |f|
131
+ Printr.log "[Printr] Writing text."
132
+ text.force_encoding 'ISO-8859-15'
118
133
  f.write text
119
134
  end
120
135
  rescue Exception => e
@@ -126,33 +141,49 @@ module Printr
126
141
  Printr.log "[Printr] Called with: #{sym}"
127
142
  if Printr.printrs[sym] then
128
143
  if args[1].class == Binding then
144
+ Printr.log "Binding was passed"
129
145
  print_to(sym,template(args[0],args[1])) #i.e. you call @printr.kitchen('item',binding)
130
146
  else
147
+ Printr.log "No Binding was passed"
131
148
  print_to(sym,args[0])
132
149
  end
133
150
  end
134
151
  end
152
+
135
153
  def sanitize(text)
154
+ # Printr.sanitize_tokens is a pair array, that is index 0 is the needle and 1 is the replace, 2 is the needle
155
+ # 3 the replacement etc. [needle,replace,needle,replace,needle,replace]
156
+ Printr.log "sanitize(#{text[0..55]})"
157
+ Printr.log "Forcing encoding to: " + Printr.encoding # Printr.encoding can be set in initializer with config.encoding = ISO-8859-15 etc
158
+ text.encode! Printr.encoding
159
+ char = ['ä', 'ü', 'ö', 'Ä', 'Ü', 'Ö', 'é', 'è', 'ú', 'ù', 'á', 'à', 'í', 'ì', 'ó', 'ò', 'â', 'ê', 'î', 'ô', 'û', 'ñ', 'ß']
160
+ replacement = ["\x84", "\x81", "\x94", "\x8E", "\x9A", "\x99", "\x82", "\x8A", "\xA3", "\x97", "\xA0", "\x85", "\xA1", "\x8D", "\xA2", "\x95", "\x83", "\x88", "\x8C", "\x93", "\x96", "\xA4", "\xE1"]
161
+ i = 0
162
+ Printr.log "Adding some tokens to the sanitize array"
136
163
  begin
137
- i = 0
138
- if Printr.sanitize_tokens.length > 1 then
139
- begin
140
- text.gsub!(Printr.sanitize_tokens[i],Printr.sanitize_tokens[i+1])
141
- i += 2
142
- end while i < Printr.sanitize_tokens.length
143
- end
144
- rescue Exception => e
145
- Printr.log "[Printr] Error in sanittize"
146
- end
164
+ rx = Regexp.new(char[i].encode(Printr.encoding))
165
+ rep = replacement[i].force_encoding(Printr.encoding)
166
+ Printr.sanitize_tokens << rx
167
+ Printr.sanitize_tokens << rep
168
+ i += 1
169
+ end while i < char.length
170
+ i = 0
171
+ begin
172
+ rx = Printr.sanitize_tokens[i]
173
+ rep = Printr.sanitize_tokens[i+1]
174
+ Printr.log "Replacing: " + rx.to_s + " with " + rep.to_s
175
+ text.gsub!(rx, rep)
176
+ i += 2
177
+ end while i < Printr.sanitize_tokens.length
147
178
  return text
148
179
  end
180
+
149
181
  def template(name,bndng)
150
182
  Printr.log "[Printr] attempting to print with template #{RAILS_ROOT}/app/views/#{Printr.scope}/#{name}.prnt.erb"
151
183
  begin
152
184
  erb = ERB.new(File.new("#{RAILS_ROOT}/app/views/#{Printr.scope}/#{name}.prnt.erb",'r').read)
153
185
  rescue Exception => e
154
- Printr.log "[Printr] Exception in view: " + e.inspect
155
-
186
+ Printr.log "[Printr] Exception in view: " + $!.inspect
156
187
  end
157
188
  Printr.log "[Printr] returning text"
158
189
  text = erb.result(bndng)
data/printr.gemspec CHANGED
@@ -4,7 +4,7 @@ require "printr/version"
4
4
 
5
5
  Gem::Specification.new do |s|
6
6
  s.name = "printr"
7
- s.version = '0.2.1'
7
+ s.version = '0.3.0'
8
8
  s.platform = Gem::Platform::RUBY
9
9
  s.authors = ["Michael Franzl","Jason Martin"]
10
10
  s.email = ["jason@jason-knight-martin.com"]
metadata CHANGED
@@ -1,12 +1,8 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: printr
3
3
  version: !ruby/object:Gem::Version
4
- prerelease: false
5
- segments:
6
- - 0
7
- - 2
8
- - 1
9
- version: 0.2.1
4
+ prerelease:
5
+ version: 0.3.0
10
6
  platform: ruby
11
7
  authors:
12
8
  - Michael Franzl
@@ -15,7 +11,7 @@ autorequire:
15
11
  bindir: bin
16
12
  cert_chain: []
17
13
 
18
- date: 2011-06-17 00:00:00 +02:00
14
+ date: 2011-06-28 00:00:00 +02:00
19
15
  default_executable:
20
16
  dependencies: []
21
17
 
@@ -64,21 +60,17 @@ required_ruby_version: !ruby/object:Gem::Requirement
64
60
  requirements:
65
61
  - - ">="
66
62
  - !ruby/object:Gem::Version
67
- segments:
68
- - 0
69
63
  version: "0"
70
64
  required_rubygems_version: !ruby/object:Gem::Requirement
71
65
  none: false
72
66
  requirements:
73
67
  - - ">="
74
68
  - !ruby/object:Gem::Version
75
- segments:
76
- - 0
77
69
  version: "0"
78
70
  requirements: []
79
71
 
80
72
  rubyforge_project: printr
81
- rubygems_version: 1.3.7
73
+ rubygems_version: 1.6.2
82
74
  signing_key:
83
75
  specification_version: 3
84
76
  summary: An engine for interfacing with printers.