Sha256: 98d3fe7bad1435d8dec7c6e6868325ba1108216fc9b85b08c4c3bb3b6a503260

Contents?: true

Size: 1.38 KB

Versions: 5

Compression:

Stored size: 1.38 KB

Contents

module ESA
  class ContextProvider
    def self.provided_types
      []
    end

    def self.check_subcontexts(context, namespace, options = {})
      existing = existing_subcontexts(context, namespace)
      contained = contained_subcontexts(context, namespace, existing, options)

      created = contained - existing
      created.each do |sub|
        sub.save if sub.new_record? or sub.changed?
      end

      unregistered = contained - context.subcontexts
      context.subcontexts += unregistered

      removed = existing - contained
      context.subcontexts -= removed

      removed.each(&:destroy) if context.can_be_persisted?

      contained
    end

    def self.context_id(context, options = {})
      []
    end

    def self.contained_ids(context, options = {})
      []
    end

    def self.existing_subcontexts(context, namespace, options = {})
      context.subcontexts.where(type: provided_types, namespace: namespace).all
    end

    def self.contained_subcontexts(context, namespace, existing, options = {})
      contained_ids = contained_ids(context, options)
      existing_ids = existing.map{|sub| context_id(sub, options)}

      new_ids = contained_ids - existing_ids

      new_subcontexts = new_ids.map do |id|
        instantiate(context, namespace, id, options)
      end

      new_subcontexts + existing.select{|sub| context_id(sub).in? contained_ids}
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
event_sourced_accounting-0.1.6 lib/esa/context_provider.rb
event_sourced_accounting-0.1.4 lib/esa/context_provider.rb
event_sourced_accounting-0.1.3 lib/esa/context_provider.rb
event_sourced_accounting-0.1.1 lib/esa/context_provider.rb
event_sourced_accounting-0.1.0 app/models/esa/context_provider.rb