Class: DataFormatter::Collection

Inherits:
Object
  • Object
show all
Defined in:
lib/data_formatter/collection.rb

Direct Known Subclasses

ArrayCollection, HashCollection

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Collection

Returns a new instance of Collection.



6
7
8
9
10
11
# File 'lib/data_formatter/collection.rb', line 6

def initialize(args)
  @indentation = args.fetch(:indentation)
  @separator = args.fetch(:separator, ",")
  @lang = args.fetch(:lang, "ruby")
  @items = prepare(args.fetch(:data))
end

Instance Attribute Details

#indentationObject (readonly)

Returns the value of attribute indentation.



4
5
6
# File 'lib/data_formatter/collection.rb', line 4

def indentation
  @indentation
end

#itemsObject (readonly)

Returns the value of attribute items.



4
5
6
# File 'lib/data_formatter/collection.rb', line 4

def items
  @items
end

#langObject (readonly)

Returns the value of attribute lang.



4
5
6
# File 'lib/data_formatter/collection.rb', line 4

def lang
  @lang
end

Instance Method Details

#to_sObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/data_formatter/collection.rb', line 13

def to_s

  return inline if items.length < 2 

  [].tap do |output|
    output << open
    indentation.increase
    items.each_with_index do |item, i| 
      output << indent(item) + separator(i)
    end
    indentation.decrease
    output << indent(close)
  end.map do |x| 
    x.to_s.force_encoding("UTF-8") 
  end.join("\n")
end