ryanwood-slither 0.99.2 → 0.99.3

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -24,7 +24,7 @@ PROJ.name = 'slither'
24
24
  PROJ.authors = 'Ryan Wood'
25
25
  PROJ.email = 'ryan.wood@gmail.com'
26
26
  PROJ.url = 'http://github.com/ryanwood/slither'
27
- PROJ.version = '0.99.2'
27
+ PROJ.version = '0.99.3'
28
28
  PROJ.exclude = %w(\.git .gitignore ^tasks \.eprj ^pkg)
29
29
  PROJ.readme_file = 'README.rdoc'
30
30
 
@@ -1,6 +1,8 @@
1
1
  require 'date'
2
2
 
3
3
  class Slither
4
+ class ParserError < RuntimeError; end
5
+
4
6
  class Column
5
7
  attr_reader :name, :length, :alignment, :type, :padding, :precision, :options
6
8
 
@@ -12,6 +14,7 @@ class Slither
12
14
  @alignment = options[:align] || :right
13
15
  @type = options[:type] || :string
14
16
  @padding = options[:padding] || :space
17
+ @truncate = options[:truncate] || false
15
18
  # Only used with floats, this determines the decimal places
16
19
  @precision = options[:precision]
17
20
  end
@@ -34,6 +37,8 @@ class Slither
34
37
  end
35
38
  else value.strip
36
39
  end
40
+ rescue
41
+ raise ParserError, "The value '#{value}' could not be converted to type #{@type}: #{$!}"
37
42
  end
38
43
 
39
44
  def format(value)
@@ -90,11 +95,7 @@ class Slither
90
95
  else
91
96
  value.to_s
92
97
  end
93
- raise(
94
- Slither::FormattedStringExceedsLengthError,
95
- "The formatted value '#{result}' in column '#{@name}' exceeds the allowed length of #{@length} chararacters."
96
- ) if result.length > @length
97
- result
98
+ validate_size result
98
99
  end
99
100
 
100
101
  def assert_valid_options(options)
@@ -104,6 +105,20 @@ class Slither
104
105
  unless options[:padding].nil? || [:space, :zero].include?(options[:padding])
105
106
  raise ArgumentError, "Option :padding only accepts :space (default) or :zero"
106
107
  end
107
- end
108
+ end
109
+
110
+ def validate_size(result)
111
+ # Handle when length is out of range
112
+ if result.length > @length
113
+ if @truncate
114
+ start = @alignment == :left ? 0 : -@length
115
+ result = result[start, @length]
116
+ else
117
+ raise Slither::FormattedStringExceedsLengthError,
118
+ "The formatted value '#{result}' in column '#{@name}' exceeds the allowed length of #{@length} chararacters."
119
+ end
120
+ end
121
+ result
122
+ end
108
123
  end
109
124
  end
Binary file
@@ -153,12 +153,24 @@ describe Slither::Column do
153
153
  @column.format('Bill').should == ' Bill'
154
154
  end
155
155
 
156
- it "should raise an error if the value is longer than the length" do
157
- @value = "XX" * @length
158
- lambda { @column.format(@value) }.should raise_error(
159
- Slither::FormattedStringExceedsLengthError,
160
- "The formatted value '#{@value}' in column '#{@name}' exceeds the allowed length of #{@length} chararacters."
161
- )
156
+ describe "whose size is too long" do
157
+ it "should raise an error if truncate is false" do
158
+ @value = "XX" * @length
159
+ lambda { @column.format(@value) }.should raise_error(
160
+ Slither::FormattedStringExceedsLengthError,
161
+ "The formatted value '#{@value}' in column '#{@name}' exceeds the allowed length of #{@length} chararacters."
162
+ )
163
+ end
164
+
165
+ it "should truncate from the left if truncate is true and aligned left" do
166
+ @column = Slither::Column.new(@name, @length, :truncate => true, :align => :left)
167
+ @column.format("This is too long").should == "This "
168
+ end
169
+
170
+ it "should truncate from the right if truncate is true and aligned right" do
171
+ @column = Slither::Column.new(@name, @length, :truncate => true, :align => :right)
172
+ @column.format("This is too long").should == " long"
173
+ end
162
174
  end
163
175
 
164
176
  it "should support the integer type" do
@@ -208,4 +220,5 @@ describe Slither::Column do
208
220
  @column.format(dt).should == '08222009'
209
221
  end
210
222
  end
223
+
211
224
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ryanwood-slither
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.99.2
4
+ version: 0.99.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryan Wood
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-04-28 00:00:00 -07:00
12
+ date: 2009-06-22 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -20,7 +20,7 @@ dependencies:
20
20
  requirements:
21
21
  - - ">="
22
22
  - !ruby/object:Gem::Version
23
- version: 2.5.0
23
+ version: 2.5.1
24
24
  version:
25
25
  description: A simple, clean DSL for describing, writing, and parsing fixed-width text files.
26
26
  email: ryan.wood@gmail.com