monosheet 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1 @@
1
+ v0.9.0. Initial release
data/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2010 Jesse Trimble
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
@@ -0,0 +1,17 @@
1
+ CHANGELOG
2
+ LICENSE
3
+ Manifest
4
+ README.rdoc
5
+ Rakefile
6
+ features/monosheet.feature
7
+ features/support/person.rb
8
+ features/support/steps.rb
9
+ lib/monosheet.rb
10
+ lib/monosheet/api.rb
11
+ lib/monosheet/inheritable_attributes.rb
12
+ lib/monosheet/output_table.rb
13
+ lib/monosheet/output_table_header_row.rb
14
+ lib/monosheet/output_table_row_divider.rb
15
+ monosheet.gemspec
16
+ spec/header_row_spec.rb
17
+ spec/row_divider_spec.rb
File without changes
@@ -0,0 +1,15 @@
1
+ require "rubygems"
2
+ require "rake"
3
+ require "echoe"
4
+
5
+ Echoe.new('monosheet') do |p|
6
+ p.description = "Give monosheet an array of objects, it gives you back a human-readable table that outputs nicely in monospace font."
7
+ p.url = "http://github.com/jtrim/monosheet"
8
+ p.author = "Jesse Trimble"
9
+ p.email = "jesseltrimble@gmail.com"
10
+ p.ignore_pattern = ["tmp/*", "script/*"]
11
+ p.development_dependencies = ["active_support >=3.0.1"]
12
+ p.summary = "Easy, monospace output of tabular data."
13
+ end
14
+
15
+ Dir["#{File.dirname(__FILE__)}/tasks/*.rake"].sort.each { |ext| load ext }
@@ -0,0 +1,23 @@
1
+ Feature: Monosheet rendering
2
+ As a CLI user
3
+ To avoid ugliness
4
+ I test Monosheet to ensure things render properly
5
+
6
+ Scenario: I test to ensure the expected result is rendered
7
+ Given I initialize a collection with the following object properties:
8
+ | first_name | middle_name | last_name | birthday | favorite_color |
9
+ | James | Leon | Smith | June 1st, 1775 | green |
10
+ | Kayla | LeAnn | Smith | February 15th, 1776 | purple |
11
+ | Dexter | Luca | Johnson | January 30th, 1756 | blue |
12
+ Then my monosheet should look like:
13
+ """
14
+ +============+=============+===========+=====================+================+
15
+ : First Name : Middle Name : Last Name : Birthday : Favorite Color :
16
+ +============+=============+===========+=====================+================+
17
+ | James | Leon | Smith | June 1st, 1775 | green |
18
+ +------------+-------------+-----------+---------------------+----------------+
19
+ | Kayla | LeAnn | Smith | February 15th, 1776 | purple |
20
+ +------------+-------------+-----------+---------------------+----------------+
21
+ | Dexter | Luca | Johnson | January 30th, 1756 | blue |
22
+ +------------+-------------+-----------+---------------------+----------------+
23
+ """
@@ -0,0 +1,7 @@
1
+ class Person
2
+ attr_accessor :first_name
3
+ attr_accessor :middle_name
4
+ attr_accessor :last_name
5
+ attr_accessor :birthday
6
+ attr_accessor :favorite_color
7
+ end
@@ -0,0 +1,33 @@
1
+ require "rubygems"
2
+ require "ruby-debug"; Debugger.start
3
+ require File.expand_path(File.dirname(__FILE__) + "/person")
4
+ require File.expand_path(File.dirname(__FILE__) + "/../../lib/monosheet")
5
+
6
+ Given /^I initialize a collection with the following object properties:$/ do |object_property_hashes|
7
+ collection = []
8
+ object_property_hashes.hashes.each do |object_properties|
9
+ p = Person.new
10
+ p.first_name = object_properties[:first_name]
11
+ p.middle_name = object_properties[:middle_name]
12
+ p.last_name = object_properties[:last_name]
13
+ p.birthday = object_properties[:birthday]
14
+ p.favorite_color = object_properties[:favorite_color]
15
+
16
+ collection << p
17
+ end
18
+
19
+ $collection = collection.dup
20
+ end
21
+
22
+ Then /^my monosheet should look like:$/ do |expected_monosheet|
23
+ Monosheet::output_table($collection) do |t|
24
+ t.column :first_name
25
+ t.column :middle_name
26
+ t.column :last_name
27
+ t.column :birthday
28
+ t.column :favorite_color
29
+ end
30
+
31
+ Monosheet::output_table.render.should == expected_monosheet
32
+ end
33
+
@@ -0,0 +1,9 @@
1
+ require 'rubygems'
2
+ require 'active_support'
3
+
4
+ class String; include ActiveSupport::Inflector; end
5
+ String.extend(ActiveSupport::Inflector)
6
+
7
+ require "monosheet/inheritable_attributes"
8
+ require "monosheet/api"
9
+ require "monosheet/output_table"
@@ -0,0 +1,31 @@
1
+ require "active_support/core_ext/string/inflections"
2
+
3
+ module Monosheet
4
+
5
+ def Monosheet::output_table(data_source = nil)
6
+ return @output_table if data_source.nil?
7
+
8
+ @output_table = Monosheet::OutputTable.new(data_source)
9
+ yield(@output_table) if block_given?
10
+ end
11
+
12
+ end
13
+
14
+ module Monosheet::ObjectRenderer
15
+ def render(column_sizes)
16
+ rendered = "| "
17
+ @columns_to_render.each_with_index do |column, index|
18
+ if self.send(column.to_sym).is_a? Integer
19
+ rendered << self.send(column.to_sym).to_s.rjust(column_sizes[index])
20
+ else
21
+ rendered << self.send(column.to_sym).to_s.ljust(column_sizes[index])
22
+ end
23
+ rendered << " | "
24
+ end
25
+ rendered.strip
26
+ end
27
+
28
+ def set_columns_to_render(columns)
29
+ @columns_to_render = columns
30
+ end
31
+ end
@@ -0,0 +1,25 @@
1
+ module ClassLevelInheritableAttributes
2
+ def self.included(base)
3
+ base.extend(ClassMethods)
4
+ end
5
+
6
+ module ClassMethods
7
+ def inheritable_attributes(*args)
8
+ @inheritable_attributes = [:inheritable_attributes] unless @inheritable_attributes.is_a? Array
9
+ @inheritable_attributes += args
10
+ args.each do |arg|
11
+ class_eval %(
12
+ class << self; attr_accessor :#{arg} end
13
+ )
14
+ end
15
+ @inheritable_attributes
16
+ end
17
+
18
+ def inherited(subclass)
19
+ @inheritable_attributes.each do |inheritable_attribute|
20
+ instance_var = "@#{inheritable_attribute}"
21
+ subclass.instance_variable_set(instance_var, instance_variable_get(instance_var))
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,47 @@
1
+ require "monosheet/output_table_header_row"
2
+ require "monosheet/output_table_row_divider"
3
+
4
+ class Monosheet::OutputTable
5
+
6
+ def initialize(data_source)
7
+ @data_source = data_source
8
+ @columns = []
9
+ @row_divider_class = Class.new(Monosheet::OutputTableRowDivider)
10
+ end
11
+
12
+ def column(name)
13
+ return unless @data_source.respond_to?(:size) && @data_source.size > 0
14
+ @columns << name.to_sym
15
+ end
16
+
17
+ def render
18
+ output_items = [@row_divider_class.new("=")]
19
+ output_items << Monosheet::OutputTableHeaderRow.new(initialize_column_sizes_and_get_column_titles)
20
+ output_items << @row_divider_class.new("=")
21
+
22
+ @data_source.each do |data_item|
23
+ @columns.each_with_index do |column_key, index|
24
+ if data_item.send(column_key.to_sym).to_s.size > @row_divider_class.column_size[index]
25
+ @row_divider_class.column_size[index] = data_item.send(column_key.to_sym).to_s.size
26
+ end
27
+ end
28
+
29
+ data_item.extend Monosheet::ObjectRenderer
30
+ data_item.set_columns_to_render @columns
31
+
32
+ output_items << data_item
33
+ output_items << @row_divider_class.new
34
+ end
35
+
36
+ output_items.map { |item| item.render @row_divider_class.column_size }.join("\n")
37
+ end
38
+
39
+ private
40
+
41
+ def initialize_column_sizes_and_get_column_titles
42
+ column_titles = @columns.map { |c| c.to_s.titleize }
43
+ @row_divider_class.column_size = column_titles.map { |t| t.size }
44
+ column_titles
45
+ end
46
+
47
+ end
@@ -0,0 +1,14 @@
1
+ class Monosheet::OutputTableHeaderRow
2
+ attr_accessor :column_names
3
+ def initialize(column_names)
4
+ @column_names = column_names
5
+ end
6
+
7
+ def render(column_sizes)
8
+ rendered = ":"
9
+ @column_names.each_with_index do |name, index|
10
+ rendered << " #{ name.center column_sizes[index] } :"
11
+ end
12
+ rendered
13
+ end
14
+ end
@@ -0,0 +1,17 @@
1
+ class Monosheet::OutputTableRowDivider
2
+ include ClassLevelInheritableAttributes
3
+ inheritable_attributes :column_size
4
+ @column_size = []
5
+
6
+ def initialize(line_char="-")
7
+ @line_char = line_char
8
+ end
9
+
10
+ def render(*args) # ugly temporary hack because of the contract-by-design thing I've got going on with the "render" method
11
+ divider_output = "+"
12
+ self.class.column_size.each do |column_width_in_characters|
13
+ divider_output << "#{@line_char}#{@line_char * column_width_in_characters}#{@line_char}+"
14
+ end
15
+ divider_output
16
+ end
17
+ end
@@ -0,0 +1,27 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
5
+
6
+ s.name = %q{monosheet}
7
+ s.version = "0.9.0"
8
+ s.add_dependency 'activesupport', '~> 3.0.0'
9
+ s.add_development_dependency 'rspec', '~> 2.0.0'
10
+ s.add_development_dependency 'cucumber', '~> 0.9.0'
11
+
12
+ s.authors = ["Jesse Trimble"]
13
+ s.email = %q{jesseltrimble@gmail.com}
14
+ s.homepage = %q{http://github.com/jtrim/monosheet}
15
+
16
+ s.date = %q{2010-11-04}
17
+ s.description = %q{Give monosheet an array of objects, it gives you back a human-readable table that outputs nicely in monospace font.}
18
+ s.summary = %q{Easy, monospace output of tabular data.}
19
+
20
+ s.test_files = `git ls-files -- {spec,features}/*`.split("\n")
21
+ s.extra_rdoc_files = ["CHANGELOG", "LICENSE", "README.rdoc", "lib/monosheet.rb", "lib/monosheet/api.rb", "lib/monosheet/inheritable_attributes.rb", "lib/monosheet/output_table.rb", "lib/monosheet/output_table_header_row.rb", "lib/monosheet/output_table_row_divider.rb"]
22
+ s.files = ["CHANGELOG", "LICENSE", "Manifest", "README.rdoc", "Rakefile", "features/monosheet.feature", "features/support/person.rb", "features/support/steps.rb", "lib/monosheet.rb", "lib/monosheet/api.rb", "lib/monosheet/inheritable_attributes.rb", "lib/monosheet/output_table.rb", "lib/monosheet/output_table_header_row.rb", "lib/monosheet/output_table_row_divider.rb", "monosheet.gemspec", "spec/header_row_spec.rb", "spec/row_divider_spec.rb"]
23
+ s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Monosheet", "--main", "README.rdoc"]
24
+ s.require_path = "lib"
25
+ s.rubygems_version = %q{1.3.7}
26
+
27
+ end
@@ -0,0 +1,15 @@
1
+ require "lib/monosheet"
2
+
3
+ describe Monosheet::OutputTableHeaderRow, "initialize" do
4
+ it "should allow for the setting of column names in the constructor" do
5
+ header_row = Monosheet::OutputTableHeaderRow.new(["One", "Two", "Three"])
6
+ header_row.column_names.should == ["One", "Two", "Three"]
7
+ end
8
+ end
9
+
10
+ describe Monosheet::OutputTableHeaderRow, "render" do
11
+ it "should render a proper table header with centered column names, given column sizes" do
12
+ header_row = Monosheet::OutputTableHeaderRow.new(["One", "Two", "Three"])
13
+ header_row.render([10, 5, 8]).should == ": One : Two : Three :"
14
+ end
15
+ end
@@ -0,0 +1,35 @@
1
+ require "lib/monosheet"
2
+
3
+ describe Monosheet::OutputTableRowDivider, "column_size" do
4
+ it "should allow the setting of column sizes" do
5
+ Monosheet::OutputTableRowDivider.column_size = [5,5,5]
6
+ Monosheet::OutputTableRowDivider.column_size.should == [5,5,5]
7
+ end
8
+ end
9
+
10
+ describe Monosheet::OutputTableRowDivider, "render" do
11
+ it "should output the correctly sized table row divider" do
12
+ Monosheet::OutputTableRowDivider.new.render.should == "+-------+-------+-------+"
13
+ end
14
+ end
15
+
16
+ RowDividerSubclass = Class.new(Monosheet::OutputTableRowDivider)
17
+ describe RowDividerSubclass, "column_size" do
18
+ it "should allow the setting of column sizes that are different than the parent class" do
19
+ RowDividerSubclass.column_size = [5,4,3]
20
+ RowDividerSubclass.column_size.should == [5,4,3]
21
+
22
+ Monosheet::OutputTableRowDivider.column_size.should == [5,5,5]
23
+ end
24
+ end
25
+
26
+ describe RowDividerSubclass, "render" do
27
+ it "should output the correctly sized table row divider that's different than the super class" do
28
+ RowDividerSubclass.new.render.should == "+-------+------+-----+"
29
+ Monosheet::OutputTableRowDivider.new.render.should == "+-------+-------+-------+"
30
+ end
31
+
32
+ it "should support specifying a different divider character in the constructor" do
33
+ RowDividerSubclass.new("$").render.should == "+$$$$$$$+$$$$$$+$$$$$+"
34
+ end
35
+ end
metadata ADDED
@@ -0,0 +1,148 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: monosheet
3
+ version: !ruby/object:Gem::Version
4
+ hash: 59
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 9
9
+ - 0
10
+ version: 0.9.0
11
+ platform: ruby
12
+ authors:
13
+ - Jesse Trimble
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-11-04 00:00:00 -06:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: activesupport
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ hash: 7
30
+ segments:
31
+ - 3
32
+ - 0
33
+ - 0
34
+ version: 3.0.0
35
+ type: :runtime
36
+ version_requirements: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ name: rspec
39
+ prerelease: false
40
+ requirement: &id002 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ hash: 15
46
+ segments:
47
+ - 2
48
+ - 0
49
+ - 0
50
+ version: 2.0.0
51
+ type: :development
52
+ version_requirements: *id002
53
+ - !ruby/object:Gem::Dependency
54
+ name: cucumber
55
+ prerelease: false
56
+ requirement: &id003 !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ hash: 59
62
+ segments:
63
+ - 0
64
+ - 9
65
+ - 0
66
+ version: 0.9.0
67
+ type: :development
68
+ version_requirements: *id003
69
+ description: Give monosheet an array of objects, it gives you back a human-readable table that outputs nicely in monospace font.
70
+ email: jesseltrimble@gmail.com
71
+ executables: []
72
+
73
+ extensions: []
74
+
75
+ extra_rdoc_files:
76
+ - CHANGELOG
77
+ - LICENSE
78
+ - README.rdoc
79
+ - lib/monosheet.rb
80
+ - lib/monosheet/api.rb
81
+ - lib/monosheet/inheritable_attributes.rb
82
+ - lib/monosheet/output_table.rb
83
+ - lib/monosheet/output_table_header_row.rb
84
+ - lib/monosheet/output_table_row_divider.rb
85
+ files:
86
+ - CHANGELOG
87
+ - LICENSE
88
+ - Manifest
89
+ - README.rdoc
90
+ - Rakefile
91
+ - features/monosheet.feature
92
+ - features/support/person.rb
93
+ - features/support/steps.rb
94
+ - lib/monosheet.rb
95
+ - lib/monosheet/api.rb
96
+ - lib/monosheet/inheritable_attributes.rb
97
+ - lib/monosheet/output_table.rb
98
+ - lib/monosheet/output_table_header_row.rb
99
+ - lib/monosheet/output_table_row_divider.rb
100
+ - monosheet.gemspec
101
+ - spec/header_row_spec.rb
102
+ - spec/row_divider_spec.rb
103
+ has_rdoc: true
104
+ homepage: http://github.com/jtrim/monosheet
105
+ licenses: []
106
+
107
+ post_install_message:
108
+ rdoc_options:
109
+ - --line-numbers
110
+ - --inline-source
111
+ - --title
112
+ - Monosheet
113
+ - --main
114
+ - README.rdoc
115
+ require_paths:
116
+ - lib
117
+ required_ruby_version: !ruby/object:Gem::Requirement
118
+ none: false
119
+ requirements:
120
+ - - ">="
121
+ - !ruby/object:Gem::Version
122
+ hash: 3
123
+ segments:
124
+ - 0
125
+ version: "0"
126
+ required_rubygems_version: !ruby/object:Gem::Requirement
127
+ none: false
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ hash: 11
132
+ segments:
133
+ - 1
134
+ - 2
135
+ version: "1.2"
136
+ requirements: []
137
+
138
+ rubyforge_project:
139
+ rubygems_version: 1.3.7
140
+ signing_key:
141
+ specification_version: 3
142
+ summary: Easy, monospace output of tabular data.
143
+ test_files:
144
+ - features/monosheet.feature
145
+ - features/support/person.rb
146
+ - features/support/steps.rb
147
+ - spec/header_row_spec.rb
148
+ - spec/row_divider_spec.rb