Sha256: fed5843b9e0e65ed1a4706411ff5608f28bbd5c5d25e2bcf6e06708896f153e1

Contents?: true

Size: 1.03 KB

Versions: 1

Compression:

Stored size: 1.03 KB

Contents

require 'gdatastore_mapper/relation/query_methods'

module GdatastoreMapper
  class Relation < Array
    include QueryMethods

    attr_accessor :klass, :association

    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

1 entries across 1 versions & 1 rubygems

Version Path
gdatastore_mapper-0.1.8 lib/gdatastore_mapper/relation.rb