table_print 1.3.3 → 1.4.0

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,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- OTNlNTA4MWFhMDUyZGZkMWE1MWQyZTY5NjUzNzI4MTg3NjRkMzE1ZA==
4
+ ZTFmNjhlMDhlOTlmNTI4NmRkOTAyODg3YTI5MDg0YWNkMDlmYzI3NA==
5
5
  data.tar.gz: !binary |-
6
- OTQ4NjZkNzVlMTgyMjkxMmQxNWRkNWQ1ODE3NWM3YmZiOTE0YjFkOA==
6
+ OGZhMjE2NDgwOGQ1MzU2NTcwMGVhYTBhNjg1Zjg3YzkwM2ViNzcxZg==
7
7
  !binary "U0hBNTEy":
8
8
  metadata.gz: !binary |-
9
- YjFjZjU5ZDFkYTZjM2VmNTA3MTMzN2VmY2ZjMDgxNWEwN2E0OTgwMTVhNWIy
10
- NGRlYWM0ZTc3MTI5ZmQ0OTcxNzBkNmU1MGZmYmQ4Mzc0ZDc1N2E0ZjQ4OTlj
11
- ZGZkNTAxMjdmZTYzMGJiNDMwYjBhOWMwMWU3YjJjYjQzNjE0MzM=
9
+ ZDliMWVkYjZhMmFkMWYzYTliOWEzMDBkZGVhODEzMjJhYWIwMzY5Y2E2YmIx
10
+ OTc2MDE0ZDNjOWIzODM5MTlhZmJhMzY5M2U5M2FmNmI0OTVjY2FmNTVjNWJh
11
+ ODY2ZjRhMDg4MjYyZTllN2ZiODZmNDY2YTUyY2QxMTZlZmJiN2Y=
12
12
  data.tar.gz: !binary |-
13
- ZjYxNjkxOGMwODYzZmRiNjMyNzI3YTU1OWM0YTI4MTQyY2IzZGQxN2FhMDcw
14
- NGJhYTExYjVhMDBjNDk0ZjhiNWQxYjM0YWY4MjhmZTBlYzZlODcxNmY2NGY3
15
- Yjk0NWQ1MzRmOTU1MzI1NDRlYThmZGE4ODg5M2I0YjI4MTg4MjI=
13
+ NzMyMTk5Yzc0NWM1Y2U1MmM3YzAyMjRiYjE2ZGRiNWY3ZTQ5NDk1OGRiNGFh
14
+ YmYwNTRkOTEwMDY5Y2ZiNTcwOWU3YmFkYjhiZjE4NzFmZmVlNmRlZGJhODE0
15
+ MGFkYjMwMTQzZjE1MzEwOTg1YzQwMGQ1ZDRhOTEwOThkYWU1M2Q=
@@ -0,0 +1,51 @@
1
+ Feature: Printing hash
2
+
3
+ Scenario: A simple array of structs
4
+ Given an array of structs named data with
5
+ |title | author |
6
+ |First post! | Ryan |
7
+ |Second post! | John |
8
+ |Third post! | Peter |
9
+ When I 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
+ """
18
+
19
+ Scenario: A lambda column
20
+ Given an array of structs named data with
21
+ |title | author |
22
+ |First post! | Ryan |
23
+ |Second post! | John |
24
+ |Third post! | Peter |
25
+ When I table_print data, [:include => {:two => lambda{|hash| hash[:author]*2}}]
26
+ Then the output should contain
27
+ """
28
+ TITLE | AUTHOR | TWO
29
+ -------------|--------|-----------
30
+ First post! | Ryan | RyanRyan
31
+ Second post! | John | JohnJohn
32
+ Third post! | Peter | PeterPeter
33
+ """
34
+
35
+ Scenario: A method on the object
36
+ Given an array of structs named data with
37
+ |title | author |
38
+ |First post! | Ryan |
39
+ |Second post! | John |
40
+ |Third post! | Peter |
41
+ When I table_print data, [:include => :size]
42
+ Then the output should contain
43
+ """
44
+ TITLE | AUTHOR | SIZE
45
+ -------------|--------|-----
46
+ First post! | Ryan | 2
47
+ Second post! | John | 2
48
+ Third post! | Peter | 2
49
+ """
50
+
51
+
@@ -25,6 +25,17 @@ Given /^a variable named (.*) with$/ do |variable, table|
25
25
  @objs.send("#{variable.downcase}=", table.hashes)
26
26
  end
27
27
 
28
+ Given /^an array of structs named (.*) with$/ do |variable, table|
29
+ @objs ||= OpenStruct.new
30
+ struct = Struct.new(*(table.column_names.collect(&:to_sym)))
31
+ data = table.hashes.collect do |hsh|
32
+ obj = struct.new
33
+ hsh.each {|k,v| obj.send "#{k}=", v}
34
+ obj
35
+ end
36
+ @objs.send("#{variable.downcase}=", data)
37
+ end
38
+
28
39
  When /^I instantiate a (.*) with (\{.*\})$/ do |klass, args|
29
40
  @objs ||= OpenStruct.new
30
41
  @objs.send("#{klass.downcase}=", Sandbox.const_get_from_string(klass).new(eval(args)))
@@ -5,7 +5,8 @@ module TablePrint
5
5
  return target.class.columns.collect(&:name) if target.class.respond_to? :columns
6
6
 
7
7
  return target.keys if target.is_a? Hash
8
-
8
+ return target.members.collect(&:to_sym) if target.is_a? Struct
9
+
9
10
  methods = []
10
11
  target.methods.each do |method_name|
11
12
  method = target.method(method_name)
@@ -1,4 +1,4 @@
1
1
  module TablePrint
2
- VERSION = "1.3.3"
2
+ VERSION = "1.4.0"
3
3
  end
4
4
 
data/lib/table_print.rb CHANGED
@@ -30,7 +30,7 @@ module TablePrint
30
30
  group.set_column(c.name, c)
31
31
  end
32
32
 
33
- group_data = (@data.first.is_a? Hash) ? [@data] : @data
33
+ group_data = (@data.first.is_a?(Hash) || @data.first.is_a?(Struct)) ? [@data] : @data
34
34
  group_data.each do |data|
35
35
  group.add_children(Fingerprinter.new.lift(columns, data))
36
36
  end
@@ -45,6 +45,16 @@ describe TablePrint::Printable do
45
45
  end
46
46
 
47
47
  it "uses column information when available (eg, from ActiveRecord objects)"
48
+
49
+ it "uses the members method when passed a Struct" do
50
+ test_struct = Struct.new(:foo, :bar)
51
+ obj = test_struct.new
52
+ obj.foo = 1
53
+ obj.bar = 2
54
+ TablePrint::Printable.default_display_methods(obj).should == [:foo, :bar]
55
+ end
56
+
57
+
48
58
  end
49
59
 
50
60
  end
@@ -50,6 +50,15 @@ describe TablePrint::Printer do
50
50
  cols.first.name.should == 'title'
51
51
  end
52
52
 
53
+ it 'pull the column names off of the array of Structs' do
54
+ struct = Struct.new(:name, :surname)
55
+ data = [struct.new("User 1", "Familyname 1"), struct.new("User 2", "Familyname 2")]
56
+ p = Printer.new(data)
57
+ cols = p.send(:columns)
58
+ cols.length.should == 2
59
+ cols.collect(&:name).sort.should == ['name', 'surname']
60
+ end
61
+
53
62
  context 'when keys are symbols' do
54
63
  it "pulls the column names off the array of hashes" do
55
64
  data = [{:name => "User 1",
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: table_print
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.3
4
+ version: 1.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chris Doyle
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-09-04 00:00:00.000000000 Z
11
+ date: 2013-09-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: cat
@@ -87,6 +87,7 @@ files:
87
87
  - features/excluding_columns.feature
88
88
  - features/multibyte.feature
89
89
  - features/printing_hash.feature
90
+ - features/printing_struct.feature
90
91
  - features/sensible_defaults.feature
91
92
  - features/support/step_definitions/before.rb
92
93
  - features/support/step_definitions/steps.rb
@@ -143,6 +144,7 @@ test_files:
143
144
  - features/excluding_columns.feature
144
145
  - features/multibyte.feature
145
146
  - features/printing_hash.feature
147
+ - features/printing_struct.feature
146
148
  - features/sensible_defaults.feature
147
149
  - features/support/step_definitions/before.rb
148
150
  - features/support/step_definitions/steps.rb