to_ascii 0.0.1

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 3f6cda9dc0082ac6275a254fa8bd4ab2ce7b3927
4
+ data.tar.gz: 5bdd348613fddbd9d3e7a69ec9c24a2df0d1e134
5
+ SHA512:
6
+ metadata.gz: ff6e9e037d396ac2100fec23c2d5272bf64375a90a9adc5c723e10e32e4e8ace8ba2ab820698d8c8460d49768bd2a9e6ad26410b9cf28c7fbe30d3ef700f0930
7
+ data.tar.gz: 2ba269b77b83ca606bf73b6cb2190a4c955cfaf46bd0478f05afdff21ea55e4c95cf6f10cea417cb64ed2b85e4207883975e3950351600c1f2eb2d65b64637f9
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in to_ascii.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Jason Wall
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,29 @@
1
+ # ToAscii
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'to_ascii'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install to_ascii
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it ( http://github.com/<my-github-username>/to_ascii/fork )
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,9 @@
1
+ require "to_ascii/version"
2
+ require 'active_support'
3
+ require 'active_support/core_ext'
4
+
5
+ module ToAscii
6
+ require 'to_ascii/class_extensions'
7
+ require 'to_ascii/visitor'
8
+ require 'to_ascii/adapters'
9
+ end
@@ -0,0 +1 @@
1
+ module ToAscii::Adapters; end
@@ -0,0 +1,12 @@
1
+ require 'to_ascii'
2
+
3
+ module ToAscii::Adapters::ActiveRecordExtensions
4
+ def to_a_for_ascii
5
+ where(nil).to_a
6
+ end
7
+ end
8
+
9
+ ActiveSupport.on_load(:active_record) do
10
+ ActiveRecord::Base.send(:extend, ToAscii::ClassExtensions)
11
+ ActiveRecord::Base.send(:extend, ToAscii::Adapters::ActiveRecordExtensions)
12
+ end
@@ -0,0 +1,3 @@
1
+ %w(active_record array enumerable).each do |f|
2
+ require "to_ascii/adapters/#{f}"
3
+ end
@@ -0,0 +1,3 @@
1
+ require 'to_ascii'
2
+
3
+ Array.send(:include, ToAscii::ClassExtensions)
@@ -0,0 +1,3 @@
1
+ require 'to_ascii'
2
+
3
+ Enumerable.send(:include, ToAscii::ClassExtensions)
@@ -0,0 +1,27 @@
1
+ module ToAscii
2
+ module ClassExtensions
3
+ def to_ascii(visitor = nil, io = STDOUT, &block)
4
+ begin
5
+ visitor ||= Visitor.for_class(self) unless block_given?
6
+ rescue NameError => e
7
+ warn "Missing default visitor for #{self} and no overrides provided"
8
+ raise e
9
+ end
10
+ visitor ||= Visitor
11
+ visitor = visitor.new
12
+ visitor.instance_eval(&block) if block_given?
13
+
14
+ a = to_a_for_ascii # important to load before so activerecord logs don't get mixed into the table
15
+ visitor.headers(io)
16
+ a.each do |row|
17
+ io.puts visitor.visit(row)
18
+ end
19
+ io.puts visitor.cell_border
20
+ io
21
+ end
22
+
23
+ def to_a_for_ascii
24
+ to_a
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,3 @@
1
+ module ToAscii
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,41 @@
1
+ module ToAscii
2
+ class Visitor
3
+ class << self
4
+ def for_class(clazz)
5
+ "#{clazz.name}ToAscii".constantize
6
+ end
7
+
8
+ def column(name, width)
9
+ columns << [name, width]
10
+ end
11
+
12
+ def columns
13
+ @columns ||= []
14
+ end
15
+ end
16
+
17
+ attr_reader :columns
18
+
19
+ def initialize
20
+ @columns = self.class.columns.dup
21
+ end
22
+
23
+ def column(name, width)
24
+ columns << [name, width]
25
+ end
26
+
27
+ def cell_border
28
+ @cell_border ||= "|#{columns.map { |c| '-' * c[1] }.join('|')}|"
29
+ end
30
+
31
+ def headers(io)
32
+ io.puts cell_border
33
+ io.puts "|#{columns.map { |c| c[0].to_s.center(c[1]) }.join('|')}|"
34
+ io.puts cell_border
35
+ end
36
+
37
+ def visit(o)
38
+ "|#{columns.map { |c| o.send(c[0]).to_s.center(c[1]) }.join('|')}|"
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,2 @@
1
+ require 'to_ascii'
2
+ require 'stringio'
@@ -0,0 +1,22 @@
1
+ require 'spec_helper'
2
+ require 'to_ascii/adapters/array'
3
+
4
+ describe Array do
5
+ Person = Struct.new(:first_name, :last_name)
6
+
7
+ let(:people) do
8
+ [Person.new('Isaac', 'Newton'), Person.new('Albert', 'Einstein')]
9
+ end
10
+ let(:io) { StringIO.new }
11
+
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
17
+
18
+ %w(Isaac Newton Albert Einstein first_name last_name).each do |word|
19
+ expect(io.string).to match(/#{word}/)
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,77 @@
1
+ require 'spec_helper'
2
+
3
+ describe ToAscii::ClassExtensions do
4
+ let(:io) { StringIO.new }
5
+
6
+ class TestClass
7
+ extend ToAscii::ClassExtensions
8
+
9
+ def self.to_a
10
+ []
11
+ end
12
+ end
13
+
14
+ class ClassWithNoDefaultVisitor
15
+ extend ToAscii::ClassExtensions
16
+
17
+ class << self
18
+ attr_reader :warned
19
+ def warn(message)
20
+ @warned = true
21
+ end
22
+
23
+ def to_a
24
+ []
25
+ end
26
+ end
27
+ end
28
+
29
+ class TestClassToAscii < ToAscii::Visitor
30
+ column :to_ascii_visitor, 24
31
+ end
32
+
33
+ class CustomVisitor < ToAscii::Visitor
34
+ column :name, 32
35
+ end
36
+
37
+ describe '#to_ascii' do
38
+ it 'should work with nil visitor param' do
39
+ TestClass.to_ascii(nil, io)
40
+ expect(io.string).to match(/to_ascii_visitor/)
41
+ end
42
+
43
+ it 'should accept a block' do
44
+ ClassWithNoDefaultVisitor.to_ascii(nil, io) do
45
+ column :id, 10
46
+ end
47
+ expect(io.string).to match(/id/)
48
+ expect(io.string).to_not match(/to_ascii_visitor/)
49
+ end
50
+
51
+ it 'should accept a class' do
52
+ ClassWithNoDefaultVisitor.to_ascii(CustomVisitor, io)
53
+ expect(io.string).to match(/name/)
54
+ end
55
+
56
+ it 'should accept a combo' do
57
+ ClassWithNoDefaultVisitor.to_ascii(CustomVisitor, io) do
58
+ column :id, 10
59
+ end
60
+ expect(io.string).to match(/id/)
61
+ expect(io.string).to match(/name/)
62
+ end
63
+
64
+ it 'can take the default visitor' do
65
+ TestClass.to_ascii(TestClassToAscii, io) do
66
+ column :id, 10
67
+ end
68
+ expect(io.string).to match(/id/)
69
+ expect(io.string).to match(/to_ascii_visitor/)
70
+ end
71
+
72
+ it 'should raise an error when no default and nothing provided' do
73
+ expect { ClassWithNoDefaultVisitor.to_ascii }.to raise_error(NameError)
74
+ expect(ClassWithNoDefaultVisitor.warned).to be
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'to_ascii/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "to_ascii"
8
+ spec.version = ToAscii::VERSION
9
+ spec.authors = ["Jason Wall"]
10
+ spec.email = ["javajo@gmail.com"]
11
+ spec.summary = %q{#to_ascii a collection}
12
+ spec.description = %q{Adds the #to_ascii method to a number of collection type classes to print out a nicely formatted
13
+ ASCII table of the attributes of each element.}
14
+ spec.homepage = ""
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0")
18
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
19
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_dependency "activesupport"
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.5"
25
+ spec.add_development_dependency "rake"
26
+ spec.add_development_dependency "rspec", "~> 3.1.0"
27
+ end
metadata ADDED
@@ -0,0 +1,124 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: to_ascii
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Jason Wall
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-11-25 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '1.5'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '1.5'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 3.1.0
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: 3.1.0
69
+ description: |-
70
+ Adds the #to_ascii method to a number of collection type classes to print out a nicely formatted
71
+ ASCII table of the attributes of each element.
72
+ email:
73
+ - javajo@gmail.com
74
+ executables: []
75
+ extensions: []
76
+ extra_rdoc_files: []
77
+ files:
78
+ - .gitignore
79
+ - .rspec
80
+ - Gemfile
81
+ - LICENSE.txt
82
+ - README.md
83
+ - Rakefile
84
+ - lib/to_ascii.rb
85
+ - lib/to_ascii/adapters.rb
86
+ - lib/to_ascii/adapters/active_record.rb
87
+ - lib/to_ascii/adapters/all.rb
88
+ - lib/to_ascii/adapters/array.rb
89
+ - lib/to_ascii/adapters/enumerable.rb
90
+ - lib/to_ascii/class_extensions.rb
91
+ - lib/to_ascii/version.rb
92
+ - lib/to_ascii/visitor.rb
93
+ - spec/spec_helper.rb
94
+ - spec/to_ascii/adapters/array_spec.rb
95
+ - spec/to_ascii/class_extensions_spec.rb
96
+ - to_ascii.gemspec
97
+ homepage: ''
98
+ licenses:
99
+ - MIT
100
+ metadata: {}
101
+ post_install_message:
102
+ rdoc_options: []
103
+ require_paths:
104
+ - lib
105
+ required_ruby_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ required_rubygems_version: !ruby/object:Gem::Requirement
111
+ requirements:
112
+ - - '>='
113
+ - !ruby/object:Gem::Version
114
+ version: '0'
115
+ requirements: []
116
+ rubyforge_project:
117
+ rubygems_version: 2.0.14
118
+ signing_key:
119
+ specification_version: 4
120
+ summary: '#to_ascii a collection'
121
+ test_files:
122
+ - spec/spec_helper.rb
123
+ - spec/to_ascii/adapters/array_spec.rb
124
+ - spec/to_ascii/class_extensions_spec.rb