dm-maker

DataMapper extension to generate instances from YAML

API

‘DataMapper::Maker.make(src)`, where `src` is either an object or a YAML string

when using references (see below), order of occurrence matters; instances must be defined before they can be referenced

if any errors occur, the respective instance is stored in an “_errors” member of the returned hash (unless ‘raise_on_save_failure` is used)

Examples

(extracted from tests, which provide a more comprehensive overview)

Person:
-
  name: John Doe
  age: 11
-
  name: Jane Doe
  age: 13

associations:

Person:
  name: John
  cars:
  -
    name: Herbie
    manufacturer:
      name: VW
  -
    name: KITT
    manufacturer:
      name: Knight Industries

(note that associated instances may include a ‘$class` attribute, which might be required due to Single Table Inheritance)

ERB expansion:

Person:
<% 2.times do |i| %>
-
  name: Johnny <%= i + 5 %>
  age: <%= i %>
<% end %>

references:

Family:
-
  $id: f1
  name: Doe

Person:
-
  name: John
  family: { $ref: f1 }
-
  name: Jane
  family: { $ref: f1 }