Module: Plato::Repo::HashCodec

Extended by:
HashCodec, StringCodec
Included in:
HashCodec
Defined in:
lib/plato/repo.rb

Instance Method Summary collapse

Instance Method Details

#read(path) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/plato/repo.rb', line 135

def read(path)
  string = super

  {}.tap do |result|
    headers, body = string.split(/\n\n/, 2)

    headers.split("\n").each do |line|
      header, val = line.split(/:\s*/, 2)

      result.update hash_key_for(header) => deserialize_value(val)
    end

    result['body'] = body.chomp if body
  end
end

#write(path, hash) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/plato/repo.rb', line 119

def write(path, hash)
  hash = stringify_keys(hash)
  body = hash.delete('body')

  data = [].tap do |buffer|
    buffer << hash.map do |key, value|
      "#{header_for(key)}: #{Sanitize.header(value)}"
    end.join("\n")

    buffer << "\n\n" << Sanitize.body(body) if body
    buffer << "\n" unless buffer.last =~ /\n\Z/
  end.join

  super(path, data)
end