Class: Improvise::IO::DictionaryWriter
- Inherits:
-
Object
- Object
- Improvise::IO::DictionaryWriter
- Defined in:
- lib/improvise/io/dictionarywriter.rb
Instance Attribute Summary collapse
-
#format ⇒ Object
Returns the value of attribute format.
-
#gzip ⇒ Object
Returns the value of attribute gzip.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(opts = {}) ⇒ DictionaryWriter
constructor
A new instance of DictionaryWriter.
- #write(io, dictionary) ⇒ Object
Constructor Details
#initialize(opts = {}) ⇒ DictionaryWriter
9 10 11 12 |
# File 'lib/improvise/io/dictionarywriter.rb', line 9 def initialize(opts={}) @format = opts[:format] || :marshal @gzip = opts[:gzip] || false end |
Instance Attribute Details
#format ⇒ Object
Returns the value of attribute format.
7 8 9 |
# File 'lib/improvise/io/dictionarywriter.rb', line 7 def format @format end |
#gzip ⇒ Object
Returns the value of attribute gzip.
7 8 9 |
# File 'lib/improvise/io/dictionarywriter.rb', line 7 def gzip @gzip end |
Class Method Details
.write(io, dictionary, opts = {}) ⇒ Object
33 34 35 |
# File 'lib/improvise/io/dictionarywriter.rb', line 33 def self.write(io, dictionary, opts={}) DictionaryWriter.new(opts).write(io, dictionary) end |
Instance Method Details
#write(io, dictionary) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/improvise/io/dictionarywriter.rb', line 14 def write(io, dictionary) dictionary_serialized = nil if @format == :json dictionary_serialized = dictionary.to_json else dictionary_serialized = Marshal.dump(dictionary) end if @gzip gz = Zlib::GzipWriter.new(io) gz.write(dictionary_serialized) gz.close else io.write(dictionary_serialized) io.close end end |