Class: DLDInternet::Formatters::Basic
- Inherits:
-
Object
- Object
- DLDInternet::Formatters::Basic
- Defined in:
- lib/dldinternet/formatters/basic.rb
Overview
Basic formatter
Instance Attribute Summary collapse
-
#columns ⇒ Object
readonly
Returns the value of attribute columns.
-
#format ⇒ Object
readonly
Returns the value of attribute format.
-
#is_a_hash ⇒ Object
readonly
Returns the value of attribute is_a_hash.
-
#object ⇒ Object
readonly
Returns the value of attribute object.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#title ⇒ Object
readonly
Returns the value of attribute title.
-
#widths ⇒ Object
:reek:DuplicateMethodCall false.
Instance Method Summary collapse
- #format_it(item = nil) ⇒ Object
- #header_it(item = nil) ⇒ Object
-
#initialize(obj, format, options) ⇒ Basic
constructor
A new instance of Basic.
-
#table_widths ⇒ Object
:reek:DuplicateMethodCall false.
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 = @object = obj @format = format @title = [:title] || nil @is_a_hash = @object.is_a?(Hash) @columns = [: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
#columns ⇒ Object (readonly)
Returns the value of attribute columns.
15 16 17 |
# File 'lib/dldinternet/formatters/basic.rb', line 15 def columns @columns end |
#format ⇒ Object (readonly)
Returns the value of attribute format.
12 13 14 |
# File 'lib/dldinternet/formatters/basic.rb', line 12 def format @format end |
#is_a_hash ⇒ Object (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 |
#object ⇒ Object (readonly)
Returns the value of attribute object.
13 14 15 |
# File 'lib/dldinternet/formatters/basic.rb', line 13 def object @object end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
18 19 20 |
# File 'lib/dldinternet/formatters/basic.rb', line 18 def @options end |
#title ⇒ Object (readonly)
Returns the value of attribute title.
14 15 16 |
# File 'lib/dldinternet/formatters/basic.rb', line 14 def title @title end |
#widths ⇒ Object
: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_widths ⇒ Object
: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, ) tws ||= fmt.widths fmt.widths.each { |c,w| if tws[c] < w tws[c] = w end } end @widths = tws else widths end end |