Sha256: ec6d022701323db2ae0c003dadbd25338ad02893e2ec43ae6a567e4e4e0cd4de

Contents?: true

Size: 1.11 KB

Versions: 3

Compression:

Stored size: 1.11 KB

Contents

module Writer
  class SymbolMatrix
    attr_accessor :source

    def initialize source
      @source = source
    end

    def serialization prefix = nil
      result = ""
      @source.each do |key, value|
        unless value.is_a? Hash
          result += " #{prefix}#{key}:#{value}"
        else
          result += " " + Writer::SymbolMatrix.new(value).serialization("#{prefix}#{key}.")
        end
      end
      result[1..-1]
    end

    alias :smas :serialization

    def hash
      the_hash = {}
      @source.each do |key, value|
        if value.respond_to? :to
          the_hash[key] = value.to.hash
        else
          the_hash[key] = value
        end
      end
      the_hash
    end

    def json
      @source.to_json
    end

    def yaml
      string_key_hash.to_yaml
    end

    def string_key_hash 
      the_hash = {}
      @source.each do |key, value|
        if value.respond_to? :to
          the_hash[key.to_s] = value.to.string_key_hash
        else
          the_hash[key.to_s] = value
        end
      end
      the_hash
    end
  end
end

class SymbolMatrix < Hash
  include Discoverer::Writer
end

Version data entries

3 entries across 3 versions & 2 rubygems

Version Path
gamera-symbolmatrix-1.2.1 lib/writer/symbolmatrix.rb
symbolmatrix-1.2.0 lib/writer/symbolmatrix.rb
symbolmatrix-1.1.3 lib/writer/symbolmatrix.rb