brocade 1.2.3 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,23 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
22
+ Gemfile.lock
23
+
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source 'https://rubygems.org'
2
+ gemspec
data/Rakefile CHANGED
@@ -1,12 +1,9 @@
1
- require 'bundler'
2
- Bundler::GemHelper.install_tasks
1
+ require 'bundler/gem_tasks'
3
2
 
4
3
  require 'rake/testtask'
5
4
 
6
5
  desc 'Test the brocade plugin.'
7
6
  Rake::TestTask.new(:test) do |t|
8
- t.libs << 'lib'
9
- t.libs << 'test'
10
7
  t.pattern = 'test/**/*_test.rb'
11
8
  t.verbose = true
12
9
  end
@@ -1,22 +1,27 @@
1
- $LOAD_PATH.unshift 'lib'
1
+ # encoding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift lib unless $LOAD_PATH.include? lib
2
4
  require 'brocade/version'
3
5
 
4
6
  Gem::Specification.new do |s|
5
- s.name = 'brocade'
6
- s.version = Brocade::VERSION
7
- s.summary = 'Generates barcodes for Rails ActiveRecord models.'
8
- s.description = s.summary
9
- s.homepage = 'http://github.com/airblade/brocade'
10
- s.authors = ['Andy Stewart']
11
- s.email = 'boss@airbladesoftware.com'
12
- s.files = %w[ LICENSE README.md Rakefile brocade.gemspec ]
13
- s.files += %w[ init.rb install.rb ]
14
- s.files += Dir.glob("lib/**/*")
15
- s.test_files = Dir.glob("test/**/*")
16
- s.require_path = 'lib'
7
+ s.name = 'brocade'
8
+ s.version = Brocade::VERSION
9
+ s.summary = 'Generates barcodes for Rails ActiveRecord models.'
10
+ s.description = s.summary
11
+ s.homepage = 'https://github.com/airblade/brocade'
12
+ s.authors = ['Andy Stewart']
13
+ s.email = 'boss@airbladesoftware.com'
14
+
15
+ s.files = `git ls-files`.split($/)
16
+ s.executables = s.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ s.test_files = s.files.grep(%r{^(test|spec|features)/})
18
+ s.require_paths = ['lib']
17
19
 
18
20
  s.add_dependency 'barby', '~> 0.5'
19
21
  s.add_dependency 'chunky_png', '~> 1.2'
20
- s.add_development_dependency 'rake', '0.8.7'
22
+ s.add_dependency 'active_support', '~> 3'
23
+ s.add_dependency 'i18n'
24
+ s.add_development_dependency 'rake', '~> 10'
25
+ s.add_development_dependency 'minitest', '~> 5'
21
26
  end
22
27
 
@@ -1,3 +1,4 @@
1
+ require 'active_support/core_ext'
1
2
  require 'barby'
2
3
  require 'barby/barcode/code_128'
3
4
  require 'barby/outputter/png_outputter'
@@ -36,21 +37,35 @@ module Brocade
36
37
  :code
37
38
  end
38
39
 
39
- def symbology
40
- :code128
40
+ # Returns a Code128 barcode instance.
41
+ #
42
+ # opts:
43
+ # :subset - specify the Code128 subset to use ('A', 'B', or 'C').
44
+ def barcode(opts = {})
45
+ data = format_for_subset_c_if_applicable send(barcodable)
46
+ if (subset = opts[:subset])
47
+ case subset
48
+ when 'A'; Barby::Code128A.new data
49
+ when 'B'; Barby::Code128B.new data
50
+ when 'C'; Barby::Code128C.new data
51
+ end
52
+ else
53
+ most_efficient_barcode_for data
54
+ end
41
55
  end
42
56
 
57
+ # Writes a barcode PNG image.
58
+ #
59
+ # opts:
60
+ # :subset - specify the Code128 subset to use ('A', 'B', or 'C').
61
+ # remaining options passed through to PNGOutputter.
43
62
  def create_barcode(opts = {})
44
- barcode = Barby::Code128B.new send(barcodable)
45
63
  path = barcode_path
46
64
  FileUtils.mkdir_p File.dirname(path)
47
65
  File.open(path, 'wb') do |f|
48
- # Barby's PNG Outputter defaults to a margin of 10px and height of 100px.
49
- # NOTE: setting the width makes no difference.
50
- # http://github.com/toretore/barby/blob/master/lib/barby/outputter/png_outputter.rb
51
- f.write barcode.to_png(self.class.options.merge(opts))
66
+ f.write barcode(opts).to_png(self.class.options.merge(opts))
52
67
  end
53
- FileUtils.chmod(0666&~File.umask, path)
68
+ FileUtils.chmod(0666 &~ File.umask, path)
54
69
  end
55
70
 
56
71
  def update_barcode(opts = {})
@@ -85,6 +100,8 @@ module Brocade
85
100
  "#{::Rails.root}/public/system/barcodes/#{klass}/#{partitioned_id}/#{symbology}.png"
86
101
  end
87
102
 
103
+ private
104
+
88
105
  def klass
89
106
  self.class.to_s.underscore.pluralize
90
107
  end
@@ -95,6 +112,27 @@ module Brocade
95
112
  # 1,000,000 records is enough for now.
96
113
  ("%06d" % id).scan(/\d{3}/).join('/')
97
114
  end
115
+
116
+ def most_efficient_barcode_for(data)
117
+ Barby::Code128C.new data
118
+ rescue ArgumentError
119
+ begin
120
+ Barby::Code128B.new data
121
+ rescue ArgumentError
122
+ Barby::Code128A.new data
123
+ end
124
+ end
125
+
126
+ def symbology
127
+ :code128
128
+ end
129
+
130
+ def format_for_subset_c_if_applicable(data)
131
+ stringified_data = "#{data}"
132
+ return data unless stringified_data =~ /^\d+$/
133
+ return data if stringified_data.length.even?
134
+ "0#{stringified_data}"
135
+ end
98
136
  end
99
137
 
100
138
  end
@@ -1,3 +1,3 @@
1
1
  module Brocade
2
- VERSION = '1.2.3'
2
+ VERSION = '1.3.0'
3
3
  end
@@ -0,0 +1,49 @@
1
+ require_relative 'helper'
2
+ require 'brocade'
3
+
4
+ class Foo
5
+ include Brocade
6
+ attr_accessor :code
7
+
8
+ def self.after_create(callback); end
9
+ def self.before_update(callback); end
10
+ def self.after_destroy(callback); end
11
+ end
12
+
13
+ class BrocadeTest < TestCase
14
+
15
+ test 'sanity' do
16
+ assert_respond_to Foo, :has_barcode
17
+ end
18
+
19
+ test 'explicit code 128 subset' do
20
+ Foo.send :has_barcode
21
+ foo = Foo.new
22
+ foo.code = 42
23
+ assert_kind_of Barby::Code128A, foo.barcode(subset: 'A')
24
+ assert_kind_of Barby::Code128B, foo.barcode(subset: 'B')
25
+ assert_kind_of Barby::Code128C, foo.barcode(subset: 'C')
26
+ end
27
+
28
+ test 'implicit code 128 subset' do
29
+ Foo.send :has_barcode
30
+ foo = Foo.new
31
+
32
+ foo.code = 42
33
+ assert_kind_of Barby::Code128C, foo.barcode
34
+
35
+ foo.code = '42aA'
36
+ assert_kind_of Barby::Code128B, foo.barcode
37
+
38
+ foo.code = "42A\t"
39
+ assert_kind_of Barby::Code128A, foo.barcode
40
+ end
41
+
42
+ test 'code c handles odd number of digits' do
43
+ Foo.send :has_barcode
44
+ foo = Foo.new
45
+ foo.code = 4
46
+ assert_kind_of Barby::Code128C, foo.barcode(subset: 'C')
47
+ end
48
+
49
+ end
@@ -1,10 +1,8 @@
1
- require 'rubygems'
2
- require 'test/unit'
3
- require 'shoulda'
1
+ gem 'minitest'
2
+ require 'minitest/autorun'
4
3
 
5
- $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
- $LOAD_PATH.unshift(File.dirname(__FILE__))
7
- require 'brocade'
8
-
9
- class Test::Unit::TestCase
4
+ class TestCase < Minitest::Test
5
+ def self.test(name, &block)
6
+ define_method :"test_#{name.inspect}", &block
7
+ end
10
8
  end
metadata CHANGED
@@ -1,123 +1,159 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: brocade
3
- version: !ruby/object:Gem::Version
4
- hash: 25
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.3.0
5
5
  prerelease:
6
- segments:
7
- - 1
8
- - 2
9
- - 3
10
- version: 1.2.3
11
6
  platform: ruby
12
- authors:
7
+ authors:
13
8
  - Andy Stewart
14
9
  autorequire:
15
10
  bindir: bin
16
11
  cert_chain: []
17
-
18
- date: 2011-10-20 00:00:00 +02:00
19
- default_executable:
20
- dependencies:
21
- - !ruby/object:Gem::Dependency
12
+ date: 2013-11-26 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
22
15
  name: barby
23
- prerelease: false
24
- version_requirements: &id001 !ruby/object:Gem::Requirement
16
+ requirement: !ruby/object:Gem::Requirement
25
17
  none: false
26
- requirements:
18
+ requirements:
27
19
  - - ~>
28
- - !ruby/object:Gem::Version
29
- hash: 1
30
- segments:
31
- - 0
32
- - 5
33
- version: "0.5"
20
+ - !ruby/object:Gem::Version
21
+ version: '0.5'
34
22
  type: :runtime
35
- requirement: *id001
36
- - !ruby/object:Gem::Dependency
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '0.5'
30
+ - !ruby/object:Gem::Dependency
37
31
  name: chunky_png
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: '1.2'
38
+ type: :runtime
38
39
  prerelease: false
39
- version_requirements: &id002 !ruby/object:Gem::Requirement
40
+ version_requirements: !ruby/object:Gem::Requirement
40
41
  none: false
41
- requirements:
42
+ requirements:
42
43
  - - ~>
43
- - !ruby/object:Gem::Version
44
- hash: 11
45
- segments:
46
- - 1
47
- - 2
48
- version: "1.2"
44
+ - !ruby/object:Gem::Version
45
+ version: '1.2'
46
+ - !ruby/object:Gem::Dependency
47
+ name: active_support
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: '3'
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '3'
62
+ - !ruby/object:Gem::Dependency
63
+ name: i18n
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
49
70
  type: :runtime
50
- requirement: *id002
51
- - !ruby/object:Gem::Dependency
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
52
79
  name: rake
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ~>
84
+ - !ruby/object:Gem::Version
85
+ version: '10'
86
+ type: :development
53
87
  prerelease: false
54
- version_requirements: &id003 !ruby/object:Gem::Requirement
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ~>
92
+ - !ruby/object:Gem::Version
93
+ version: '10'
94
+ - !ruby/object:Gem::Dependency
95
+ name: minitest
96
+ requirement: !ruby/object:Gem::Requirement
55
97
  none: false
56
- requirements:
57
- - - "="
58
- - !ruby/object:Gem::Version
59
- hash: 49
60
- segments:
61
- - 0
62
- - 8
63
- - 7
64
- version: 0.8.7
98
+ requirements:
99
+ - - ~>
100
+ - !ruby/object:Gem::Version
101
+ version: '5'
65
102
  type: :development
66
- requirement: *id003
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ~>
108
+ - !ruby/object:Gem::Version
109
+ version: '5'
67
110
  description: Generates barcodes for Rails ActiveRecord models.
68
111
  email: boss@airbladesoftware.com
69
112
  executables: []
70
-
71
113
  extensions: []
72
-
73
114
  extra_rdoc_files: []
74
-
75
- files:
115
+ files:
116
+ - .gitignore
117
+ - Gemfile
76
118
  - LICENSE
77
119
  - README.md
78
120
  - Rakefile
79
121
  - brocade.gemspec
80
- - init.rb
81
- - install.rb
122
+ - lib/brocade.rb
82
123
  - lib/brocade/has_barcode.rb
83
124
  - lib/brocade/version.rb
84
- - lib/brocade.rb
125
+ - test/brocade_test.rb
85
126
  - test/helper.rb
86
- - test/test_brocade.rb
87
- has_rdoc: true
88
- homepage: http://github.com/airblade/brocade
127
+ homepage: https://github.com/airblade/brocade
89
128
  licenses: []
90
-
91
129
  post_install_message:
92
130
  rdoc_options: []
93
-
94
- require_paths:
131
+ require_paths:
95
132
  - lib
96
- required_ruby_version: !ruby/object:Gem::Requirement
133
+ required_ruby_version: !ruby/object:Gem::Requirement
97
134
  none: false
98
- requirements:
99
- - - ">="
100
- - !ruby/object:Gem::Version
101
- hash: 3
102
- segments:
135
+ requirements:
136
+ - - ! '>='
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ segments:
103
140
  - 0
104
- version: "0"
105
- required_rubygems_version: !ruby/object:Gem::Requirement
141
+ hash: -3770506909574615478
142
+ required_rubygems_version: !ruby/object:Gem::Requirement
106
143
  none: false
107
- requirements:
108
- - - ">="
109
- - !ruby/object:Gem::Version
110
- hash: 3
111
- segments:
144
+ requirements:
145
+ - - ! '>='
146
+ - !ruby/object:Gem::Version
147
+ version: '0'
148
+ segments:
112
149
  - 0
113
- version: "0"
150
+ hash: -3770506909574615478
114
151
  requirements: []
115
-
116
152
  rubyforge_project:
117
- rubygems_version: 1.6.2
153
+ rubygems_version: 1.8.23
118
154
  signing_key:
119
155
  specification_version: 3
120
156
  summary: Generates barcodes for Rails ActiveRecord models.
121
- test_files:
157
+ test_files:
158
+ - test/brocade_test.rb
122
159
  - test/helper.rb
123
- - test/test_brocade.rb
data/init.rb DELETED
@@ -1 +0,0 @@
1
- require 'brocade'
data/install.rb DELETED
@@ -1 +0,0 @@
1
- puts 'You have chosen wisely.'
@@ -1,7 +0,0 @@
1
- require 'helper'
2
-
3
- class TestBrocade < Test::Unit::TestCase
4
- should "probably rename this file and start testing for real" do
5
- flunk "hey buddy, you should probably rename this file and start testing for real"
6
- end
7
- end