Module: Philiprehberger::DataMapper::Parsable

Included in:
Mapping
Defined in:
lib/philiprehberger/data_mapper/parsable.rb

Instance Method Summary collapse

Instance Method Details

#from_csv(csv_string, headers: true) ⇒ Object



9
10
11
12
# File 'lib/philiprehberger/data_mapper/parsable.rb', line 9

def from_csv(csv_string, headers: true)
  rows = CSV.parse(csv_string, headers: headers)
  rows.map { |row| map(row_to_hash(row)) }
end

#from_json(json_string) ⇒ Object



14
15
16
17
18
19
20
21
# File 'lib/philiprehberger/data_mapper/parsable.rb', line 14

def from_json(json_string)
  parsed = JSON.parse(json_string)

  case parsed
  when Array then map_all(parsed.map { |h| symbolize_keys(h) })
  when Hash  then map(symbolize_keys(parsed))
  end
end