to_ascii 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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 91a86e677bdf4a1f775a8ef679d6b0e57fc54c2f
4
- data.tar.gz: 2ec7fa498cd26c88412803f5169d402ca2a9eb59
3
+ metadata.gz: 674e4fe70567329846f9710c7d4046adde150bc9
4
+ data.tar.gz: 088abc95e28fd61311426ab187800ed57c94cc22
5
5
  SHA512:
6
- metadata.gz: 54c53215b6ea7d57bdecc7c9809be9e37117534af7f772faf834648a156d2678dcd6a98a0e58e375058d0692a8c62e13b27987dd3c2aa28df2a6c776be398106
7
- data.tar.gz: b2a4a08fafd0d4763233c469b63f60369f1fc73cb2ad90969ae6e1b6742d379d70ea58c442edacc4f4d37eab90772ccd61f6d91b558d76788c28af9b3fae83d3
6
+ metadata.gz: ee0ce50bad8b1b32e8c7a49012c2294f972d98fe151e5255a5fca73a105b264c51a18ca1b7edb518884c36151a9f4672b471abfd95f2cc1f4f94d297b5bc08ab
7
+ data.tar.gz: 05feacb545021678d4e4933d54423ab33801f084ba3b9a9dd8b45b90d6d513fb020a8f07718694d6986cd1e1970543b5a39ee1e064bfb6cf78504a3936aa6be2
@@ -1,3 +1,3 @@
1
1
  module ToAscii
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -1,27 +1,60 @@
1
1
  module ToAscii
2
+ module ColumnDefiner
3
+ def column(name, width)
4
+ columns << create_column(name, width)
5
+ end
6
+
7
+ def id(width = 6)
8
+ create_column :id, width
9
+ end
10
+
11
+ def respond_to_missing?(method, include_private = false) # ruby 1.9+ only, but 1.8 won't care because it just looks like a method #honeybadger
12
+ true
13
+ end
14
+
15
+ def respond_to?(method, include_private = false)
16
+ true
17
+ end # for ruby 1.8 completeness
18
+
19
+ def method_missing(method, *args, &block)
20
+ raise ArgumentError, "wrong number of arguments (#{args.length} for 0..1)" if args.length > 1
21
+ width = args.length == 1 ? args[0] : method.to_s.length + 2
22
+ create_column method, width
23
+ end
24
+
25
+ private
26
+
27
+ def create_column(name, width)
28
+ [name, width]
29
+ end
30
+ end
31
+
2
32
  class Visitor
3
33
  class << self
34
+ include ColumnDefiner
35
+
4
36
  def for_class(clazz)
5
37
  "#{clazz.name}ToAscii".constantize
6
38
  end
7
39
 
8
- def column(name, width)
9
- columns << [name, width]
10
- end
11
-
12
- def columns
40
+ def columns(*args)
13
41
  @columns ||= []
42
+ return @columns if args.length == 0
43
+
44
+ @columns += args
14
45
  end
15
46
  end
16
47
 
17
- attr_reader :columns
48
+ include ColumnDefiner
18
49
 
19
50
  def initialize
20
51
  @columns = self.class.columns.dup
21
52
  end
22
53
 
23
- def column(name, width)
24
- columns << [name, width]
54
+ def columns(*args)
55
+ return @columns if args.length == 0
56
+
57
+ @columns += args
25
58
  end
26
59
 
27
60
  def cell_border
@@ -10,10 +10,7 @@ describe Array do
10
10
  let(:io) { StringIO.new }
11
11
 
12
12
  it 'should add to_ascii to an array' do
13
- people.to_ascii(nil, io) do
14
- column :first_name, 16
15
- column :last_name, 16
16
- end
13
+ people.to_ascii(nil, io) { columns first_name(16), last_name(16) }
17
14
 
18
15
  %w(Isaac Newton Albert Einstein first_name last_name).each do |word|
19
16
  expect(io.string).to match(/#{word}/)
@@ -42,7 +42,7 @@ describe ToAscii::ClassExtensions do
42
42
 
43
43
  it 'should accept a block' do
44
44
  ClassWithNoDefaultVisitor.to_ascii(nil, io) do
45
- column :id, 10
45
+ columns id(10)
46
46
  end
47
47
  expect(io.string).to match(/id/)
48
48
  expect(io.string).to_not match(/to_ascii_visitor/)
@@ -0,0 +1,69 @@
1
+ require 'spec_helper'
2
+
3
+ describe ToAscii::Visitor do
4
+ let(:visitor) { Class.new(ToAscii::Visitor) }
5
+
6
+ def column_name_of col
7
+ col[0]
8
+ end
9
+
10
+ def column_width_of col
11
+ col[1]
12
+ end
13
+
14
+ shared_examples_for 'a column definer' do
15
+ describe :id do
16
+ it 'should, by default, add a column :id with width of 6' do
17
+ result = subject.id
18
+ expect(column_name_of result).to eq :id
19
+ expect(column_width_of result).to eq 6
20
+ end
21
+
22
+ it 'should accept a width param though' do
23
+ result = subject.id(18)
24
+ expect(column_width_of result).to eq 18
25
+ end
26
+ end
27
+
28
+ describe :method_missing do
29
+ it 'should generate any column def' do
30
+ result = subject.magic_potion
31
+ expect(column_name_of result).to eq :magic_potion
32
+ expect(column_width_of result).to eq 14
33
+ end
34
+
35
+ it 'should accept a width argument' do
36
+ result = subject.magic_potion(300)
37
+ expect(column_width_of result).to eq(300)
38
+ end
39
+
40
+ it 'should raise arity errors if has too many args' do
41
+ # this might save some people some headaches
42
+ expect { subject.magic(30, :id) }.to raise_error(ArgumentError, 'wrong number of arguments (2 for 0..1)')
43
+ end
44
+ end
45
+
46
+ describe :columns do
47
+ it 'should just return the collection if no params' do
48
+ expect(subject.columns.length).to eq 0
49
+ subject.column :name, 60
50
+ expect(subject.columns.length).to eq 1
51
+ end
52
+
53
+ it 'should add columns if provided by params' do
54
+ subject.columns [:id, 16]
55
+ expect(subject.columns.length).to eq 1
56
+ end
57
+ end
58
+ end
59
+
60
+ subject { visitor }
61
+
62
+ it_should_behave_like 'a column definer'
63
+
64
+ context :instance do
65
+ subject { visitor.new }
66
+
67
+ it_should_behave_like 'a column definer'
68
+ end
69
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: to_ascii
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jason Wall
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-11-25 00:00:00.000000000 Z
11
+ date: 2014-12-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -93,6 +93,7 @@ files:
93
93
  - spec/spec_helper.rb
94
94
  - spec/to_ascii/adapters/array_spec.rb
95
95
  - spec/to_ascii/class_extensions_spec.rb
96
+ - spec/to_ascii/visitor_spec.rb
96
97
  - to_ascii.gemspec
97
98
  homepage: http://github.com/thejayvm/to_ascii
98
99
  licenses:
@@ -122,3 +123,4 @@ test_files:
122
123
  - spec/spec_helper.rb
123
124
  - spec/to_ascii/adapters/array_spec.rb
124
125
  - spec/to_ascii/class_extensions_spec.rb
126
+ - spec/to_ascii/visitor_spec.rb