Sha256: 50ba99eeaccc0a4978f0ecac3705f351f88d7959d7527a54ed13991e735f4388

Contents?: true

Size: 931 Bytes

Versions: 4

Compression:

Stored size: 931 Bytes

Contents

module GdatastoreMapper
  class Relation < Array
    def initialize(klass, association)
      @klass = klass
      @association = association
    end

    def new attributes
      belonging_attr = attributes.merge(@association.owner_attributes)
      @association.belonging_klass.new(belonging_attr)
    end

    def create attributes
      belonging = create_belonging attributes
      update_owner belonging
      belonging
    end

    private

    def create_belonging attributes
      belonging_attr = attributes.merge(@association.owner_attributes)
      @association.belonging_klass.create(belonging_attr)
    end

    def update_owner belonging
      existing_ids = @association.owner.send(@association.belonging_id)
      existing_ids = [] if existing_ids.nil?
      owner_attr = {}
      owner_attr[@association.belonging_id] = (existing_ids << belonging.id)
      @association.owner.update(owner_attr)
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
gdatastore_mapper-0.1.7 lib/gdatastore_mapper/relation.rb
gdatastore_mapper-0.1.6 lib/gdatastore_mapper/relation.rb
gdatastore_mapper-0.1.5 lib/gdatastore_mapper/relation.rb
gdatastore_mapper-0.1.4 lib/gdatastore_mapper/relation.rb