hirb 0.4.2 → 0.4.3

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.rdoc CHANGED
@@ -1,3 +1,6 @@
1
+ == 0.4.3
2
+ * Remove Formatter::TO_A_EXCEPTIONS and replace with Formatter.to_a_classes
3
+
1
4
  == 0.4.2
2
5
  * Fix bug with Tempfile and to_a_exceptions
3
6
 
@@ -2,15 +2,17 @@ module Hirb
2
2
  # A Formatter object formats an output object (using Formatter.format_output) into a string based on the views defined
3
3
  # for its class and/or ancestry.
4
4
  class Formatter
5
- TO_A_EXCEPTIONS = [Hash, IO]
6
- POSSIBLE_TO_A_EXCEPTIONS = %w{Tempfile}
7
-
8
5
  class<<self
9
6
  # This config is used by Formatter.format_output to lazily load dynamic views defined with Hirb::DynamicView.
10
7
  # This hash has the same format as Formatter.config.
11
8
  attr_accessor :dynamic_config
9
+
10
+ # Array of classes whose objects respond to :to_a and allow the first
11
+ # element of the converted array to determine the output class.
12
+ attr_accessor :to_a_classes
12
13
  end
13
14
  self.dynamic_config = {}
15
+ self.to_a_classes = %w{Array Set ActiveRecord::Relation}
14
16
 
15
17
  def initialize(additional_config={}) #:nodoc:
16
18
  @klass_config = {}
@@ -54,13 +56,11 @@ module Hirb
54
56
  _format_output(output, options, &block)
55
57
  end
56
58
 
57
- # Array of classes whose objects respond to :to_a and are exceptions to the Formatter's array algorithm.
58
- def to_a_exceptions
59
- @to_a_exceptions ||= TO_A_EXCEPTIONS + POSSIBLE_TO_A_EXCEPTIONS.select {|e|
60
- Object.const_defined?(e) }.map {|e| Object.const_get(e) }
59
+ #:stopdoc:
60
+ def to_a_classes
61
+ @to_a_classes ||= self.class.to_a_classes.map {|e| Util.any_const_get(e) }.compact
61
62
  end
62
63
 
63
- #:stopdoc:
64
64
  def _format_output(output, options, &block)
65
65
  output = options[:output_method] ? (output.is_a?(Array) ?
66
66
  output.map {|e| call_output_method(options[:output_method], e) } :
@@ -84,7 +84,7 @@ module Hirb
84
84
  end
85
85
 
86
86
  def determine_output_class(output)
87
- output.respond_to?(:to_a) && !to_a_exceptions.any? {|e| output.is_a?(e) } ?
87
+ output.respond_to?(:to_a) && to_a_classes.any? {|e| output.is_a?(e) } ?
88
88
  Array(output)[0].class : output.class
89
89
  end
90
90
 
data/lib/hirb/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Hirb
2
- VERSION = '0.4.2'
2
+ VERSION = '0.4.3'
3
3
  end
@@ -72,19 +72,9 @@ describe "Formatter" do
72
72
  @formatter.parse_console_options(options).should == expected_options
73
73
  end
74
74
 
75
- it "#determine output_class bypasses optional exceptions for to_a" do
76
- require 'tempfile'
77
- @formatter.determine_output_class(Tempfile.new('')).should == Tempfile
78
- end
79
-
80
- it "#determine_output_class bypasses exceptions for to_a" do
81
- @formatter.determine_output_class(STDOUT).should == IO
82
- @formatter.determine_output_class({:a=>1}).should == Hash
83
- end
84
-
85
- it "#determine_output_class bypasses subclasses of exceptions for to_a" do
86
- class Hash2 < Hash; end
87
- @formatter.determine_output_class(Hash2[1=>2]).should == Hash2
75
+ it "#determine_output_class recognizes subclasses of to_a classes" do
76
+ class Array2 < Array; end
77
+ @formatter.determine_output_class(Array2.new(%w{ok dude})).should == String
88
78
  end
89
79
  end
90
80
 
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hirb
3
3
  version: !ruby/object:Gem::Version
4
- hash: 11
4
+ hash: 9
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 4
9
- - 2
10
- version: 0.4.2
9
+ - 3
10
+ version: 0.4.3
11
11
  platform: ruby
12
12
  authors:
13
13
  - Gabriel Horner
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-03-27 00:00:00 -04:00
18
+ date: 2011-03-28 00:00:00 -04:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency