Sha256: a7fd9aa017181c595189bf2b8df9273edaf233d904beb93a2b84b12f97ab0893

Contents?: true

Size: 1.14 KB

Versions: 94

Compression:

Stored size: 1.14 KB

Contents

class ReeDao::PersistAssoc
  include Ree::FnDSL

  fn :persist_assoc do
    link :demodulize, from: :ree_string
    link :underscore, from: :ree_string
  end

  contract(
    -> (v) {
      v.class.ancestors.include?(ReeDto::EntityDSL)
    },
    Sequel::Dataset,
    Ksplat[
      root_setter?: Symbol,
      child_assoc?: Symbol,
    ] => nil
  )
  def call(agg_root, assoc_dao, **opts)
    setter_method = if opts[:root_setter].nil?
      name = underscore(demodulize(agg_root.class.name))
      "#{name}_id="
    else
      "#{opts[:root_setter]}"
    end

    assoc_name = if opts[:child_assoc].nil?
      dto_class = assoc_dao
        .opts[:schema_mapper]
        .dto(:db_load)

      name = underscore(demodulize(dto_class.name))
      "#{name}s"
    else
      opts[:child_assoc]
    end

    agg_root.send(assoc_name).each do |child|
      if !child.respond_to?(setter_method)
        raise ArgumentError.new("#{child.class} does not respond to `#{setter_method}` method")
      end

      child.send(setter_method, agg_root.id)

      if child.id
        assoc_dao.update(child)
      else
        assoc_dao.put(child)
      end
    end

    nil
  end
end

Version data entries

94 entries across 94 versions & 1 rubygems

Version Path
ree_lib-1.0.124 lib/ree_lib/packages/ree_dao/package/ree_dao/functions/persist_assoc.rb
ree_lib-1.0.123 lib/ree_lib/packages/ree_dao/package/ree_dao/functions/persist_assoc.rb
ree_lib-1.0.122 lib/ree_lib/packages/ree_dao/package/ree_dao/functions/persist_assoc.rb
ree_lib-1.0.121 lib/ree_lib/packages/ree_dao/package/ree_dao/functions/persist_assoc.rb
ree_lib-1.0.120 lib/ree_lib/packages/ree_dao/package/ree_dao/functions/persist_assoc.rb
ree_lib-1.0.119 lib/ree_lib/packages/ree_dao/package/ree_dao/functions/persist_assoc.rb
ree_lib-1.0.118 lib/ree_lib/packages/ree_dao/package/ree_dao/functions/persist_assoc.rb
ree_lib-1.0.117 lib/ree_lib/packages/ree_dao/package/ree_dao/functions/persist_assoc.rb
ree_lib-1.0.116 lib/ree_lib/packages/ree_dao/package/ree_dao/functions/persist_assoc.rb
ree_lib-1.0.115 lib/ree_lib/packages/ree_dao/package/ree_dao/functions/persist_assoc.rb
ree_lib-1.0.114 lib/ree_lib/packages/ree_dao/package/ree_dao/functions/persist_assoc.rb
ree_lib-1.0.113 lib/ree_lib/packages/ree_dao/package/ree_dao/functions/persist_assoc.rb
ree_lib-1.0.112 lib/ree_lib/packages/ree_dao/package/ree_dao/functions/persist_assoc.rb
ree_lib-1.0.111 lib/ree_lib/packages/ree_dao/package/ree_dao/functions/persist_assoc.rb
ree_lib-1.0.110 lib/ree_lib/packages/ree_dao/package/ree_dao/functions/persist_assoc.rb
ree_lib-1.0.109 lib/ree_lib/packages/ree_dao/package/ree_dao/functions/persist_assoc.rb
ree_lib-1.0.108 lib/ree_lib/packages/ree_dao/package/ree_dao/functions/persist_assoc.rb
ree_lib-1.0.107 lib/ree_lib/packages/ree_dao/package/ree_dao/functions/persist_assoc.rb
ree_lib-1.0.106 lib/ree_lib/packages/ree_dao/package/ree_dao/functions/persist_assoc.rb
ree_lib-1.0.105 lib/ree_lib/packages/ree_dao/package/ree_dao/functions/persist_assoc.rb