rescpos 0.0.2 → 0.0.3

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.
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- rescpos (0.0.2)
4
+ rescpos (0.0.3)
5
5
 
6
6
  GEM
7
7
  remote: http://rubygems.org/
@@ -0,0 +1,9 @@
1
+ <%= text(key_value("结帐单号", @bill[:id])) %>
2
+ <%= text(key_value("打印时间", Time.now.strftime("%Y-%m-%d %H:%M").to_s)) %>
3
+ <%= single_splitline %>
4
+ <%= table(@bill_items){|t| t.config([9]); t.td([:name, :quantity])} %>
5
+ <%= double_splitline %>
6
+ <%= text(key_value("客户签名", underline(27))) %>
7
+
8
+
9
+
data/examples/print.rb CHANGED
@@ -4,13 +4,17 @@ $:.unshift(File.join(File.dirname(__FILE__)))
4
4
 
5
5
  require 'rescpos'
6
6
  require 'reports/dish_item_report'
7
+ require 'reports/bill_list_report'
7
8
 
8
9
  Rescpos.configure do |config|
9
10
  config.template_path = File.expand_path(File.join(File.dirname(__FILE__), 'escposes'))
10
11
  end
11
12
 
12
13
  report = DishItemReport.new
14
+ report1 = BillListReport.new
13
15
 
14
16
  printer = Rescpos::Printer.open("192.168.1.3", 9100)
15
17
  printer.print_report(report, :encoding => 'GBK')
18
+ # print table
19
+ printer.print_report(report1, :encoding => 'GBK')
16
20
  printer.close
@@ -0,0 +1,32 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.unshift(File.join(File.dirname(__FILE__), "../../lib"))
3
+
4
+ require 'rescpos'
5
+
6
+ class BillListReport < Rescpos::Report
7
+ attr_reader :bill, :bill_items
8
+
9
+ def initialize
10
+ @bill_items = [{
11
+ :name => '广靓油条',
12
+ :quantity => 2,
13
+ :dish_category => '面组'
14
+ }, {
15
+ :name => '靓油条',
16
+ :quantity => 3,
17
+ :dish_category => '面组'
18
+ }, {
19
+ :name => '广式靓油条',
20
+ :quantity => 1,
21
+ :dish_category => '面组'
22
+ }]
23
+
24
+ @bill = {
25
+ :id => "20110819000002",
26
+ :waiter => '000',
27
+ :created_at => '2011-08-18 13:11:03',
28
+ :table => '01B[大堂]',
29
+ :num_of_people => 2,
30
+ }
31
+ end
32
+ end
@@ -15,10 +15,8 @@ module Rescpos
15
15
  @socket.close
16
16
  end
17
17
 
18
- def print(content, opts={})
19
- if opts[:encoding]
20
- content = Iconv.iconv("#{opts[:encoding]}//IGNORE","UTF-8//IGNORE", content)[0]
21
- end
18
+ def print(content, opts={:encoding => 'GBK'})
19
+ content = Iconv.iconv("#{opts[:encoding]}//IGNORE","UTF-8//IGNORE", content)[0]
22
20
  @socket.send(content, Socket::MSG_OOB)
23
21
  cut
24
22
  end
@@ -2,6 +2,9 @@ module Rescpos
2
2
  module ReportUtil
3
3
  FONT_NORMAL = "\x00"
4
4
  FONT_BIG = "\x11"
5
+ ALIGN_C = "\x01"
6
+ ALIGN_L = "\x00"
7
+ ALIGN_R = "\x02"
5
8
 
6
9
  def single_splitline
7
10
  text("-" * 42, :font_size => FONT_NORMAL)
@@ -25,6 +28,7 @@ module Rescpos
25
28
  formatted_text = ''
26
29
  formatted_text << fontsize(font_size)
27
30
  formatted_text << grayscale(options[:gray]) if options[:gray]
31
+ formatted_text << align(options[:align_type]) if options[:align_type]
28
32
  formatted_text << txt if txt
29
33
  end
30
34
 
@@ -40,22 +44,31 @@ module Rescpos
40
44
  "#{label}: #{value}"
41
45
  end
42
46
 
43
- def align(format)
44
- if format == 'C'
45
- return "\x1b\x61\x01"
46
- elsif format == 'L'
47
- return "\x1b\x61\x00"
48
- elsif format == 'R'
49
- return "\x1b\x61\x02"
50
- end
47
+ def align(type)
48
+ "\x1b\x61" << type.to_s
51
49
  end
52
50
 
53
- def table(positions)
51
+ def table(data)
52
+ table = Rescpos::Table.new(data)
53
+ yield table
54
54
  command = "\x1b\x44"
55
- for position in positions
56
- command << ascii(position)
55
+ table.positions.each do |position|
56
+ command << position.chr
57
57
  end
58
58
  command << "\x00"
59
+ table.data.each do |item|
60
+ table.keys.each do |key|
61
+ begin
62
+ if item[key]
63
+ command << "#{item[key]}"+"\x09"
64
+ end
65
+ rescue
66
+ command << "#{item.send(key)}"+"\x09"
67
+ end
68
+ end
69
+ command << "\n"
70
+ end
71
+ command
59
72
  end
60
73
 
61
74
  def horizontal_tab
@@ -0,0 +1,16 @@
1
+ module Rescpos
2
+ class Table
3
+ attr_reader :data, :positions, :keys
4
+ def initialize(data)
5
+ @data = data
6
+ end
7
+
8
+ def config(positions)
9
+ @positions = positions
10
+ end
11
+
12
+ def td(keys)
13
+ @keys = keys
14
+ end
15
+ end
16
+ end
@@ -1,3 +1,3 @@
1
1
  module Rescpos
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
data/lib/rescpos.rb CHANGED
@@ -5,6 +5,7 @@ require "core_ext/string"
5
5
 
6
6
  require "rescpos/version"
7
7
  require "rescpos/printer"
8
+ require "rescpos/table"
8
9
  require "rescpos/report_util"
9
10
  require "iconv"
10
11
  require "rescpos/configuration"
@@ -5,6 +5,10 @@ class ReportUtilTest
5
5
  include Rescpos::ReportUtil
6
6
  end
7
7
 
8
+ class BillItemTest
9
+ attr_accessor :name, :quantity
10
+ end
11
+
8
12
  describe ReportUtilTest do
9
13
  before :each do
10
14
  @report_util = ReportUtilTest.new
@@ -39,7 +43,7 @@ describe ReportUtilTest do
39
43
  end
40
44
 
41
45
  it "should return a formmat text" do
42
- @report_util.text('abc', {:font_size => ReportUtilTest::FONT_BIG, :gray => 4}).should == "\x1d\x21\x11\x1b\x6d34abc"
46
+ @report_util.text('abc', {:font_size => ReportUtilTest::FONT_BIG, :gray => 4, :align_type => ReportUtilTest::ALIGN_C}).should == "\x1d\x21\x11\x1b\x6d34\x1b\x61\x01abc"
43
47
  end
44
48
 
45
49
  it "should return a formatted key and value pair" do
@@ -47,8 +51,31 @@ describe ReportUtilTest do
47
51
  end
48
52
 
49
53
  it "should return a align" do
50
- @report_util.align('C').should == "\x1b\x61\x01"
51
- @report_util.align('L').should == "\x1b\x61\x00"
52
- @report_util.align('R').should == "\x1b\x61\x02"
54
+ @report_util.align(ReportUtilTest::ALIGN_C).should == "\x1b\x61\x01"
55
+ @report_util.align(ReportUtilTest::ALIGN_L).should == "\x1b\x61\x00"
56
+ @report_util.align(ReportUtilTest::ALIGN_R).should == "\x1b\x61\x02"
57
+ end
58
+
59
+ it "give a hash should return a table" do
60
+ bill_item = {
61
+ :name => 'a',
62
+ :quantity => 2,
63
+ }
64
+ table = @report_util.table([bill_item]) do |t|
65
+ t.config([9])
66
+ t.td([:name, :quantity])
67
+ end
68
+ table.should == "\x1b\x44#{9.chr}\x00a\x092\x09\n"
69
+ end
70
+
71
+ it "give a object should return a table" do
72
+ bill_item = BillItemTest.new
73
+ bill_item.name = 'a'
74
+ bill_item.quantity = 2
75
+ table = @report_util.table([bill_item]) do |t|
76
+ t.config([9])
77
+ t.td([:name, :quantity])
78
+ end
79
+ table.should == "\x1b\x44#{9.chr}\x00a\x092\x09\n"
53
80
  end
54
81
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rescpos
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-08-28 00:00:00.000000000Z
12
+ date: 2011-08-31 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rb-inotify
16
- requirement: &80126060 !ruby/object:Gem::Requirement
16
+ requirement: &82882830 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *80126060
24
+ version_requirements: *82882830
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: libnotify
27
- requirement: &80125540 !ruby/object:Gem::Requirement
27
+ requirement: &82882360 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *80125540
35
+ version_requirements: *82882360
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: rspec-instafail
38
- requirement: &80124890 !ruby/object:Gem::Requirement
38
+ requirement: &82881740 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '0'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *80124890
46
+ version_requirements: *82881740
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: cucumber
49
- requirement: &80124110 !ruby/object:Gem::Requirement
49
+ requirement: &82880970 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: '0'
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *80124110
57
+ version_requirements: *82880970
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: rspec
60
- requirement: &80123580 !ruby/object:Gem::Requirement
60
+ requirement: &82880300 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ! '>='
@@ -65,10 +65,10 @@ dependencies:
65
65
  version: '0'
66
66
  type: :development
67
67
  prerelease: false
68
- version_requirements: *80123580
68
+ version_requirements: *82880300
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: guard-bundler
71
- requirement: &80122640 !ruby/object:Gem::Requirement
71
+ requirement: &82879370 !ruby/object:Gem::Requirement
72
72
  none: false
73
73
  requirements:
74
74
  - - ! '>='
@@ -76,10 +76,10 @@ dependencies:
76
76
  version: '0'
77
77
  type: :development
78
78
  prerelease: false
79
- version_requirements: *80122640
79
+ version_requirements: *82879370
80
80
  - !ruby/object:Gem::Dependency
81
81
  name: guard-cucumber
82
- requirement: &80121900 !ruby/object:Gem::Requirement
82
+ requirement: &82878680 !ruby/object:Gem::Requirement
83
83
  none: false
84
84
  requirements:
85
85
  - - ! '>='
@@ -87,10 +87,10 @@ dependencies:
87
87
  version: '0'
88
88
  type: :development
89
89
  prerelease: false
90
- version_requirements: *80121900
90
+ version_requirements: *82878680
91
91
  - !ruby/object:Gem::Dependency
92
92
  name: guard-rspec
93
- requirement: &80121130 !ruby/object:Gem::Requirement
93
+ requirement: &82877790 !ruby/object:Gem::Requirement
94
94
  none: false
95
95
  requirements:
96
96
  - - ! '>='
@@ -98,7 +98,7 @@ dependencies:
98
98
  version: '0'
99
99
  type: :development
100
100
  prerelease: false
101
- version_requirements: *80121130
101
+ version_requirements: *82877790
102
102
  description: Print formatted docs with Winpos WP-800
103
103
  email:
104
104
  - towerhe@gmail.com
@@ -114,8 +114,10 @@ files:
114
114
  - Guardfile
115
115
  - README.textile
116
116
  - Rakefile
117
+ - examples/escposes/bill_list.escpos.erb
117
118
  - examples/escposes/dish_item.escpos.erb
118
119
  - examples/print.rb
120
+ - examples/reports/bill_list_report.rb
119
121
  - examples/reports/dish_item_report.rb
120
122
  - features/render_report.feature
121
123
  - features/step_definitions/.gitkeep
@@ -128,6 +130,7 @@ files:
128
130
  - lib/rescpos/printer.rb
129
131
  - lib/rescpos/report.rb
130
132
  - lib/rescpos/report_util.rb
133
+ - lib/rescpos/table.rb
131
134
  - lib/rescpos/version.rb
132
135
  - rescpos.gemspec
133
136
  - spec/lib/core_ext/string_spec.rb