Class: AwesomeDump::Formatter

Inherits:
Object
  • Object
show all
Defined in:
lib/ad/awesome_dump.rb

Instance Method Summary collapse

Constructor Details

#initialize(awesome_method, options = {}) ⇒ Formatter

Returns a new instance of Formatter.



63
64
65
66
# File 'lib/ad/awesome_dump.rb', line 63

def initialize(awesome_method, options = {})
  @awesome = awesome_method
  @options = options
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_id, object) ⇒ Object

Catch-all method to format an arbitrary object.



168
169
170
# File 'lib/ad/awesome_dump.rb', line 168

def method_missing(method_id, object)
  return object(object)
end

Instance Method Details

#array(a) ⇒ Object

Format an array.



69
70
71
72
73
# File 'lib/ad/awesome_dump.rb', line 69

def array(a)
  return [] if a == []
  
  return a.inject([]) { |arr, item| arr << @awesome.call(item) }
end

#bigdecimal(n) ⇒ Object Also known as: rational

Format BigDecimal and Rational objects by convering them to Float.



147
148
149
# File 'lib/ad/awesome_dump.rb', line 147

def bigdecimal(n)
  return object(n.to_f) #, :as => :bigdecimal
end

#class(c) ⇒ Object

Format Class object.



125
126
127
128
129
130
131
132
# File 'lib/ad/awesome_dump.rb', line 125

def class(c)
  if [:quote, :safe].include?(@options[:escape])
    sc = c.superclass
    return %{#{c} < #{sc}} if sc && sc != Object
    return c.to_s
  end
  return c
end

#dir(d) ⇒ Object

Format Dir object.



141
142
143
144
# File 'lib/ad/awesome_dump.rb', line 141

def dir(d)
  ls = `ls -alF #{d.path.shellescape}`
  return object(d) #, :with => ls.empty? ? nil : "\n#{ls.chop}")
end

#falseclass(n) ⇒ Object



99
100
101
102
# File 'lib/ad/awesome_dump.rb', line 99

def falseclass(n)
  return 'false' if @options[:escape] == :quote
  return false
end

#file(f) ⇒ Object

Format File object.



135
136
137
138
# File 'lib/ad/awesome_dump.rb', line 135

def file(f)
  ls = File.directory?(f) ? `ls -adlF #{f.path.shellescape}` : `ls -alF #{f.path.shellescape}`
  return object(f) #, :with => ls.empty? ? nil : "\n#{ls.chop}")
end

#fixnum(f) ⇒ Object



84
85
86
87
# File 'lib/ad/awesome_dump.rb', line 84

def fixnum(f)
  return f.to_s if @options[:escape] == :quote
  return f
end

#hash(h) ⇒ Object

Format a hash. If @options if negative left align hash keys.



76
77
78
79
80
81
82
# File 'lib/ad/awesome_dump.rb', line 76

def hash(h)
  return {} if h == {}
  return h.keys.inject({}) do |hash, key|
    hash.store @awesome.call(key), @awesome.call(h[key])
    hash
  end
end

#nilclass(n) ⇒ Object



89
90
91
92
# File 'lib/ad/awesome_dump.rb', line 89

def nilclass(n)
  return 'nil' if @options[:escape] == :quote
  return nil
end

#object(o, with = nil) ⇒ Object

Format an arbitrary object.



153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/ad/awesome_dump.rb', line 153

def object(o, with=nil)
  o_hash = case @options[:escape]
    when :quote then {'class' => o.class.to_s, 'object_id' => o.object_id.to_s}
    when :safe  then {'class' => o.class.to_s, 'object_id' => o.object_id}
    else             {:class => o.class, :object_id => o.object_id}
  end
  o.instance_variables.sort.each do |iv|
    iv_k = [:quote, :safe].include?(@options[:escape]) ? iv.to_s : iv.to_sym
    o_hash[iv_k] = @awesome.call(o.instance_variable_get(iv))
  end
  o_hash = with.merge(o_hash) if with
  return o_hash
end

#string(s) ⇒ Object



109
110
111
112
# File 'lib/ad/awesome_dump.rb', line 109

def string(s)
  return %{"#{s}"} if @options[:escape] == :quote
  return s
end

#struct(s) ⇒ Object

Format a Struct. If @options if negative left align hash keys.



115
116
117
118
119
120
121
122
# File 'lib/ad/awesome_dump.rb', line 115

def struct(s)
  h = {}
  s.each_pair do |m, v|
    m_k = [:quote, :safe].include?(@options[:escape]) ? m.to_s : m.to_sym
    h[m_k] = @awesome.call(v)
  end
  return object(s, h)
end

#symbol(s) ⇒ Object



104
105
106
107
# File 'lib/ad/awesome_dump.rb', line 104

def symbol(s)
  return s.to_s.include?(':') ? %{:"#{s}"} : %{:#{s}} if [:quote, :safe].include?(@options[:escape])
  return s
end

#trueclass(n) ⇒ Object



94
95
96
97
# File 'lib/ad/awesome_dump.rb', line 94

def trueclass(n)
  return 'true' if @options[:escape] == :quote
  return true
end