Module: DataMapper::Maker
- Defined in:
- lib/dm-maker.rb,
lib/dm-maker/version.rb
Constant Summary collapse
- REL_TYPES =
{ :to_one => [DataMapper::Associations::ManyToOne::Relationship, DataMapper::Associations::OneToOne::Relationship], :to_many => [DataMapper::Associations::ManyToMany::Relationship, DataMapper::Associations::OneToMany::Relationship] }
- VERSION =
"1.2.0"
Class Method Summary collapse
-
.create_instance(klass, data, cache = {}) ⇒ Object
TODO: document $id / $ref.
- .load_yaml(yaml) ⇒ Object
-
.make(data) ⇒ Object
returns a hash of instances by class if any errors occur, the respective instances are stored in a special “_errors” member.
Class Method Details
.create_instance(klass, data, cache = {}) ⇒ Object
TODO: document $id / $ref
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/dm-maker.rb', line 39 def self.create_instance(klass, data, cache={}) # TODO: document $id / $ref if ref = data.delete("$ref") raise ArgumentError if data.length > 0 return cache[ref] end id = data.delete("$id") data.each { |key, value| rel_name = key.to_sym if rel = klass.relationships[rel_name] if REL_TYPES[:to_many].include? rel.class assoc_class = klass.new. # XXX: hacky send(rel_name).relationship.child_model value = value.map { |d| if custom_class = d.delete("$class") # TODO: document and test assoc_class = custom_class.constantize end create_instance(assoc_class, d, cache) } elsif REL_TYPES[:to_one].include? rel.class if custom_class = value.delete("$class") # TODO: document and test assoc_class = custom_class.constantize else assoc_class = (rel.child_model == klass or klass < rel.child_model) ? rel.parent_model : rel.child_model end value = create_instance(assoc_class, value, cache) end end data[key] = value # XXX: dangerous while iterating over that same hash!? } instance = klass.new(data) cache[id] = instance if id return instance end |
.load_yaml(yaml) ⇒ Object
77 78 79 80 81 82 83 84 |
# File 'lib/dm-maker.rb', line 77 def self.load_yaml(yaml) yaml = ERB.new(yaml).result # TODO: document # hack to retain order in Ruby 1.8 # TODO: document expectations/constraints (only required for $id / $ref) yaml.split("\n\n").each_with_object(ActiveSupport::OrderedHash.new) { |yml, hsh| hsh.merge! YAML.load(yml) } end |
.make(data) ⇒ Object
returns a hash of instances by class if any errors occur, the respective instances are stored in a special “_errors” member
24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/dm-maker.rb', line 24 def self.make(data) data = load_yaml(data) if data.class == String cache = {} res = { "_errors" => [] } return data.each_with_object(res) do |(class_name, instances), hsh| klass = class_name.constantize hsh[class_name] = instances.map { |instance_data| instance = create_instance(klass, instance_data, cache) hsh["_errors"] << instance unless instance.save instance } end end |