table_print 1.0.1 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,4 +1,4 @@
1
- Copyright (c) 2011 Chris Doyle
1
+ Copyright (c) 2012 Chris Doyle
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining
4
4
  a copy of this software and associated documentation files (the
@@ -0,0 +1,17 @@
1
+ Feature: Printing hash
2
+
3
+ Scenario: A simple array of hashes
4
+ Given a variable named data with
5
+ |title | author |
6
+ |First post! | Ryan |
7
+ |Second post! | John |
8
+ |Third post! | Peter |
9
+ And table_print data
10
+ Then the output should contain
11
+ """
12
+ TITLE | AUTHOR
13
+ ---------------------
14
+ First post! | Ryan
15
+ Second post! | John
16
+ Third post! | Peter
17
+ """
@@ -20,6 +20,11 @@ Given /^(.*) has a method named (\w*) with (.*)$/ do |klass, method_name, blk|
20
20
  Sandbox.add_method(klass, method_name, &eval(blk))
21
21
  end
22
22
 
23
+ Given /^a variable named (.*) with$/ do |variable, table|
24
+ @objs ||= OpenStruct.new
25
+ @objs.send("#{variable.downcase}=", table.hashes)
26
+ end
27
+
23
28
  When /^I instantiate a (.*) with (\{.*\})$/ do |klass, args|
24
29
  @objs ||= OpenStruct.new
25
30
  @objs.send("#{klass.downcase}=", Sandbox.const_get_from_string(klass).new(eval(args)))
@@ -30,7 +30,8 @@ module TablePrint
30
30
  group.set_column(c.name, c)
31
31
  end
32
32
 
33
- @data.each do |data|
33
+ group_data = (@data.first.is_a? Hash) ? [@data] : @data
34
+ group_data.each do |data|
34
35
  group.add_children(Fingerprinter.new.lift(columns, data))
35
36
  end
36
37
 
@@ -12,6 +12,8 @@ module TablePrint
12
12
  def hash_to_rows(prefix, hash, objects)
13
13
  rows = []
14
14
 
15
+
16
+
15
17
  # convert each object into its own row
16
18
  Array(objects).each do |target|
17
19
  row = populate_row(prefix, hash, target)
@@ -32,7 +34,9 @@ module TablePrint
32
34
  cells = {}
33
35
  handleable_columns(hash).each do |method|
34
36
  display_method = (prefix == "" ? method : "#{prefix}.#{method}")
35
- if method.is_a? Proc
37
+ if target.is_a? Hash
38
+ cell_value = target[method.to_sym]
39
+ elsif method.is_a? Proc
36
40
  cell_value = method.call(target)
37
41
  else
38
42
  cell_value ||= target.send(method)
@@ -4,6 +4,8 @@ module TablePrint
4
4
  def self.default_display_methods(target)
5
5
  return target.class.columns.collect(&:name) if target.class.respond_to? :columns
6
6
 
7
+ return target.keys if target.is_a? Hash
8
+
7
9
  methods = []
8
10
  target.methods.each do |method_name|
9
11
  method = target.method(method_name)
@@ -1,4 +1,4 @@
1
1
  module TablePrint
2
- VERSION = "1.0.1"
2
+ VERSION = "1.1.0"
3
3
  end
4
4
 
@@ -46,5 +46,6 @@ describe TablePrint::Printable do
46
46
 
47
47
  it "uses column information when available (eg, from ActiveRecord objects)"
48
48
  end
49
+
49
50
  end
50
51
 
@@ -35,6 +35,19 @@ describe TablePrint::Printer do
35
35
  cols.first.name.should == 'title'
36
36
  end
37
37
 
38
+ it "pulls the column names off the array of hashes" do
39
+ data = [{:name => "User 1",
40
+ :surname => "Familyname 1"
41
+ },
42
+ {:name => "User 2",
43
+ :surname => "Familyname 2"}]
44
+
45
+ p = Printer.new(data)
46
+ cols = p.columns
47
+ cols.length.should == 2
48
+ cols.collect(&:name).sort.should == ['name', 'surname']
49
+ end
50
+
38
51
  it "pulls out excepted columns" do
39
52
  Sandbox.add_class("Post")
40
53
  Sandbox.add_attributes("Post", :title, :author)
@@ -12,6 +12,7 @@ Gem::Specification.new do |gem|
12
12
  gem.summary = "Turn objects into nicely formatted columns for easy reading"
13
13
  gem.homepage = "http://tableprintgem.com"
14
14
  gem.version = TablePrint::VERSION
15
+ gem.license = 'MIT'
15
16
 
16
17
  gem.files = `git ls-files`.split($\)
17
18
  gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
metadata CHANGED
@@ -1,92 +1,98 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: table_print
3
- version: !ruby/object:Gem::Version
4
- version: 1.0.1
3
+ version: !ruby/object:Gem::Version
4
+ hash: 19
5
5
  prerelease:
6
+ segments:
7
+ - 1
8
+ - 1
9
+ - 0
10
+ version: 1.1.0
6
11
  platform: ruby
7
- authors:
12
+ authors:
8
13
  - Chris Doyle
9
14
  autorequire:
10
15
  bindir: bin
11
16
  cert_chain: []
12
- date: 2012-12-01 00:00:00.000000000Z
13
- dependencies:
14
- - !ruby/object:Gem::Dependency
17
+
18
+ date: 2013-01-28 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
15
21
  name: cat
16
- requirement: !ruby/object:Gem::Requirement
17
- none: false
18
- requirements:
19
- - - ~>
20
- - !ruby/object:Gem::Version
21
- version: 0.2.1
22
- type: :development
23
22
  prerelease: false
24
- version_requirements: !ruby/object:Gem::Requirement
23
+ requirement: &id001 !ruby/object:Gem::Requirement
25
24
  none: false
26
- requirements:
25
+ requirements:
27
26
  - - ~>
28
- - !ruby/object:Gem::Version
27
+ - !ruby/object:Gem::Version
28
+ hash: 21
29
+ segments:
30
+ - 0
31
+ - 2
32
+ - 1
29
33
  version: 0.2.1
30
- - !ruby/object:Gem::Dependency
31
- name: cucumber
32
- requirement: !ruby/object:Gem::Requirement
33
- none: false
34
- requirements:
35
- - - ~>
36
- - !ruby/object:Gem::Version
37
- version: 1.2.1
38
34
  type: :development
35
+ version_requirements: *id001
36
+ - !ruby/object:Gem::Dependency
37
+ name: cucumber
39
38
  prerelease: false
40
- version_requirements: !ruby/object:Gem::Requirement
39
+ requirement: &id002 !ruby/object:Gem::Requirement
41
40
  none: false
42
- requirements:
41
+ requirements:
43
42
  - - ~>
44
- - !ruby/object:Gem::Version
43
+ - !ruby/object:Gem::Version
44
+ hash: 29
45
+ segments:
46
+ - 1
47
+ - 2
48
+ - 1
45
49
  version: 1.2.1
46
- - !ruby/object:Gem::Dependency
47
- name: rspec
48
- requirement: !ruby/object:Gem::Requirement
49
- none: false
50
- requirements:
51
- - - ~>
52
- - !ruby/object:Gem::Version
53
- version: 2.11.0
54
50
  type: :development
51
+ version_requirements: *id002
52
+ - !ruby/object:Gem::Dependency
53
+ name: rspec
55
54
  prerelease: false
56
- version_requirements: !ruby/object:Gem::Requirement
55
+ requirement: &id003 !ruby/object:Gem::Requirement
57
56
  none: false
58
- requirements:
57
+ requirements:
59
58
  - - ~>
60
- - !ruby/object:Gem::Version
59
+ - !ruby/object:Gem::Version
60
+ hash: 35
61
+ segments:
62
+ - 2
63
+ - 11
64
+ - 0
61
65
  version: 2.11.0
62
- - !ruby/object:Gem::Dependency
63
- name: rake
64
- requirement: !ruby/object:Gem::Requirement
65
- none: false
66
- requirements:
67
- - - ~>
68
- - !ruby/object:Gem::Version
69
- version: 0.9.2
70
66
  type: :development
67
+ version_requirements: *id003
68
+ - !ruby/object:Gem::Dependency
69
+ name: rake
71
70
  prerelease: false
72
- version_requirements: !ruby/object:Gem::Requirement
71
+ requirement: &id004 !ruby/object:Gem::Requirement
73
72
  none: false
74
- requirements:
73
+ requirements:
75
74
  - - ~>
76
- - !ruby/object:Gem::Version
75
+ - !ruby/object:Gem::Version
76
+ hash: 63
77
+ segments:
78
+ - 0
79
+ - 9
80
+ - 2
77
81
  version: 0.9.2
78
- description: TablePrint turns objects into nicely formatted columns for easy reading.
79
- Works great in rails console, works on pure ruby objects, autodetects columns, lets
80
- you traverse ActiveRecord associations. Simple, powerful.
82
+ type: :development
83
+ version_requirements: *id004
84
+ description: TablePrint turns objects into nicely formatted columns for easy reading. Works great in rails console, works on pure ruby objects, autodetects columns, lets you traverse ActiveRecord associations. Simple, powerful.
81
85
  email: archslide@gmail.com
82
86
  executables: []
87
+
83
88
  extensions: []
89
+
84
90
  extra_rdoc_files: []
85
- files:
91
+
92
+ files:
86
93
  - .document
87
94
  - .gitignore
88
95
  - .rspec
89
- - .rvmrc
90
96
  - .travis.yml
91
97
  - Gemfile
92
98
  - LICENSE.txt
@@ -95,6 +101,7 @@ files:
95
101
  - features/adding_columns.feature
96
102
  - features/configuring_output.feature
97
103
  - features/excluding_columns.feature
104
+ - features/printing_hash.feature
98
105
  - features/sensible_defaults.feature
99
106
  - features/support/step_definitions/before.rb
100
107
  - features/support/step_definitions/steps.rb
@@ -123,33 +130,43 @@ files:
123
130
  - spec/table_print_spec.rb
124
131
  - table_print.gemspec
125
132
  homepage: http://tableprintgem.com
126
- licenses: []
133
+ licenses:
134
+ - MIT
127
135
  post_install_message:
128
136
  rdoc_options: []
129
- require_paths:
137
+
138
+ require_paths:
130
139
  - lib
131
- required_ruby_version: !ruby/object:Gem::Requirement
140
+ required_ruby_version: !ruby/object:Gem::Requirement
132
141
  none: false
133
- requirements:
134
- - - ! '>='
135
- - !ruby/object:Gem::Version
136
- version: '0'
137
- required_rubygems_version: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ hash: 3
146
+ segments:
147
+ - 0
148
+ version: "0"
149
+ required_rubygems_version: !ruby/object:Gem::Requirement
138
150
  none: false
139
- requirements:
140
- - - ! '>='
141
- - !ruby/object:Gem::Version
142
- version: '0'
151
+ requirements:
152
+ - - ">="
153
+ - !ruby/object:Gem::Version
154
+ hash: 3
155
+ segments:
156
+ - 0
157
+ version: "0"
143
158
  requirements: []
159
+
144
160
  rubyforge_project:
145
161
  rubygems_version: 1.8.19
146
162
  signing_key:
147
163
  specification_version: 3
148
164
  summary: Turn objects into nicely formatted columns for easy reading
149
- test_files:
165
+ test_files:
150
166
  - features/adding_columns.feature
151
167
  - features/configuring_output.feature
152
168
  - features/excluding_columns.feature
169
+ - features/printing_hash.feature
153
170
  - features/sensible_defaults.feature
154
171
  - features/support/step_definitions/before.rb
155
172
  - features/support/step_definitions/steps.rb
data/.rvmrc DELETED
@@ -1,81 +0,0 @@
1
- #!/usr/bin/env bash
2
-
3
- # This is an RVM Project .rvmrc file, used to automatically load the ruby
4
- # development environment upon cd'ing into the directory
5
-
6
- # First we specify our desired <ruby>[@<gemset>], the @gemset name is optional.
7
- environment_id="ruby-1.8.7-p358"
8
-
9
- #
10
- # Uncomment the following lines if you want to verify rvm version per project
11
- #
12
- # rvmrc_rvm_version="1.10.2" # 1.10.1 seams as a safe start
13
- # eval "$(echo ${rvm_version}.${rvmrc_rvm_version} | awk -F. '{print "[[ "$1*65536+$2*256+$3" -ge "$4*65536+$5*256+$6" ]]"}' )" || {
14
- # echo "This .rvmrc file requires at least RVM ${rvmrc_rvm_version}, aborting loading."
15
- # return 1
16
- # }
17
- #
18
-
19
- #
20
- # Uncomment following line if you want options to be set only for given project.
21
- #
22
- # PROJECT_JRUBY_OPTS=( --1.9 )
23
- #
24
- # The variable PROJECT_JRUBY_OPTS requires the following to be run in shell:
25
- #
26
- # chmod +x ${rvm_path}/hooks/after_use_jruby_opts
27
- #
28
-
29
- #
30
- # First we attempt to load the desired environment directly from the environment
31
- # file. This is very fast and efficient compared to running through the entire
32
- # CLI and selector. If you want feedback on which environment was used then
33
- # insert the word 'use' after --create as this triggers verbose mode.
34
- #
35
- if [[ -d "${rvm_path:-$HOME/.rvm}/environments" \
36
- && -s "${rvm_path:-$HOME/.rvm}/environments/$environment_id" ]]
37
- then
38
- \. "${rvm_path:-$HOME/.rvm}/environments/$environment_id"
39
-
40
- if [[ -s "${rvm_path:-$HOME/.rvm}/hooks/after_use" ]]
41
- then
42
- . "${rvm_path:-$HOME/.rvm}/hooks/after_use"
43
- fi
44
- else
45
- # If the environment file has not yet been created, use the RVM CLI to select.
46
- if ! rvm --create "$environment_id"
47
- then
48
- echo "Failed to create RVM environment '${environment_id}'."
49
- return 1
50
- fi
51
- fi
52
-
53
- #
54
- # If you use an RVM gemset file to install a list of gems (*.gems), you can have
55
- # it be automatically loaded. Uncomment the following and adjust the filename if
56
- # necessary.
57
- #
58
- # filename=".gems"
59
- # if [[ -s "$filename" ]]
60
- # then
61
- # rvm gemset import "$filename" | grep -v already | grep -v listed | grep -v complete | sed '/^$/d'
62
- # fi
63
-
64
- # If you use bundler, this might be useful to you:
65
- # if [[ -s Gemfile ]] && ! command -v bundle >/dev/null
66
- # then
67
- # printf "%b" "The rubygem 'bundler' is not installed. Installing it now.\n"
68
- # gem install bundler
69
- # fi
70
- # if [[ -s Gemfile ]] && command -v bundle
71
- # then
72
- # bundle install
73
- # fi
74
-
75
- if [[ $- == *i* ]] # check for interactive shells
76
- then
77
- echo "Using: $(tput setaf 2)$GEM_HOME$(tput sgr0)" # show the user the ruby and gemset they are using in green
78
- else
79
- echo "Using: $GEM_HOME" # don't use colors in interactive shells
80
- fi
81
-