Class: DataFormatter::Collection
- Inherits:
-
Object
- Object
- DataFormatter::Collection
- Defined in:
- lib/data_formatter/collection.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#indentation ⇒ Object
readonly
Returns the value of attribute indentation.
-
#items ⇒ Object
readonly
Returns the value of attribute items.
-
#lang ⇒ Object
readonly
Returns the value of attribute lang.
Instance Method Summary collapse
-
#initialize(args) ⇒ Collection
constructor
A new instance of Collection.
- #to_s ⇒ Object
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
#indentation ⇒ Object (readonly)
Returns the value of attribute indentation.
4 5 6 |
# File 'lib/data_formatter/collection.rb', line 4 def indentation @indentation end |
#items ⇒ Object (readonly)
Returns the value of attribute items.
4 5 6 |
# File 'lib/data_formatter/collection.rb', line 4 def items @items end |
#lang ⇒ Object (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_s ⇒ Object
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 |