Class: DLDInternet::Formatters::Basic

Inherits:
Object
  • Object
show all
Defined in:
lib/dldinternet/formatters/basic.rb

Overview

Basic formatter

Direct Known Subclasses

Awesome, Table

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(obj, format, options) ⇒ Basic

Returns a new instance of Basic.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/dldinternet/formatters/basic.rb', line 20

def initialize(obj, format, options)
  @options = options
  @object = obj
  @format = format
  @title  = options[:title] || nil
  @is_a_hash = @object.is_a?(Hash)
  @columns = options[:columns] || nil
  if @columns
    @columns = Hashie::Mash.new(Hash[@columns.split(/\s*,\s*/).map{ |c| [c, nil]}])
    @columns.dup.map{ |h,v|
      submap(h, v)
    }
  end
end

Instance Attribute Details

#columnsObject (readonly)

Returns the value of attribute columns.



15
16
17
# File 'lib/dldinternet/formatters/basic.rb', line 15

def columns
  @columns
end

#formatObject (readonly)

Returns the value of attribute format.



12
13
14
# File 'lib/dldinternet/formatters/basic.rb', line 12

def format
  @format
end

#is_a_hashObject (readonly)

Returns the value of attribute is_a_hash.



16
17
18
# File 'lib/dldinternet/formatters/basic.rb', line 16

def is_a_hash
  @is_a_hash
end

#objectObject (readonly)

Returns the value of attribute object.



13
14
15
# File 'lib/dldinternet/formatters/basic.rb', line 13

def object
  @object
end

#optionsObject (readonly)

Returns the value of attribute options.



18
19
20
# File 'lib/dldinternet/formatters/basic.rb', line 18

def options
  @options
end

#titleObject (readonly)

Returns the value of attribute title.



14
15
16
# File 'lib/dldinternet/formatters/basic.rb', line 14

def title
  @title
end

#widthsObject

:reek:DuplicateMethodCall false



70
71
72
# File 'lib/dldinternet/formatters/basic.rb', line 70

def widths
  @widths
end

Instance Method Details

#format_it(item = nil) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/dldinternet/formatters/basic.rb', line 50

def format_it(item=nil)
  item ||=
      if @is_a_hash
        if @columns.nil?
          object
        else
          hsh = {}
          object.each do |key,val|
            hsh[key] = val  if @columns.keys.include?(key)
          end
          hsh
        end
      else
        object
      end

  format_item item
end

#header_it(item = nil) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/dldinternet/formatters/basic.rb', line 35

def header_it(item=nil)
  item ||=
      if @is_a_hash
        if @columns.nil?
          object.keys
        else
          @columns.keys
        end
      else
        object.to_s
      end

  format_item item, true
end

#table_widthsObject

:reek:DuplicateMethodCall false



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/dldinternet/formatters/basic.rb', line 88

def table_widths
  if object.is_a?(Array) && object.size > 0 && object[0].is_a?(Hash)
    tws = nil
    object.each do |obj|
      fmt = self.class.new(obj, format, options)
      tws ||= fmt.widths
      fmt.widths.each { |c,w|
        if tws[c] < w
          tws[c] = w
        end
      }
    end
    @widths = tws
  else
    widths
  end
end