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.



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

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)
    }
    @object = columnize_item(@object)
  end
  # unless @columns.nil?
  #   @object = columnize_item(@object)
  # end
end

Instance Attribute Details

#columnsObject (readonly)

Returns the value of attribute columns.



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

def columns
  @columns
end

#formatObject (readonly)

Returns the value of attribute format.



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

def format
  @format
end

#is_a_hashObject (readonly)

Returns the value of attribute is_a_hash.



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

def is_a_hash
  @is_a_hash
end

#objectObject (readonly)

Returns the value of attribute object.



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

def object
  @object
end

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

#titleObject (readonly)

Returns the value of attribute title.



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

def title
  @title
end

#widthsObject

:reek:DuplicateMethodCall false



84
85
86
# File 'lib/dldinternet/formatters/basic.rb', line 84

def widths
  @widths
end

Instance Method Details

#format_it(item = nil) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/dldinternet/formatters/basic.rb', line 64

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



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/dldinternet/formatters/basic.rb', line 40

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

  format_item item, true
end

#table_widthsObject

:reek:DuplicateMethodCall false



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/dldinternet/formatters/basic.rb', line 104

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