These are chat archives for dry-rb/chat

2nd
Nov 2015
Tim Riley
@timriley
Nov 02 2015 02:53
Trying to stay ahead of the curve and use Dry::Data as a form attributes object
ended up needing to build a shim like this:
class FormAttributes < Dry::Data::Struct
  def self.new(attributes)
    schema_defaults = schema.keys.map { |key| [key, nil] }.to_h

    super(schema_defaults.merge(Hash(attributes)))
  end

  def [](attribute_name)
    send(attribute_name)
  end
end
Just so it's not so strict about missing values when initializing
and #[] in place so I can use it with a ROM::Model::Validator
Tim Riley
@timriley
Nov 02 2015 03:17
Him, except I can't use a Dry::Data::Struct as an input for a ROM command
Tim Riley
@timriley
Nov 02 2015 10:12
I handled it this way in the interim:
App.register "transformations.to_h" do |input|
  input.to_h
end

App.register("transactions.sign_up_customer") do
  CallSheet(container: App) do
    map :process, with: "attributes.customer_sign_up"
    try :validate, with: "validators.customer", catch: ROM::Model::ValidationError
    map :prepare_for_persist, with: "transformations.to_h"
    tee :persist, with: "persistence.commands.create_customer"
  end
end
Added a #to_h to that FormAttributes class and that prepare_for_persiststep in the business transaction.
Does not feel ideal, but it works for now at least ¯_(ツ)_/¯
Piotr Solnica
@solnic
Nov 02 2015 10:31
@timriley I haven’t finish dry-data wrt web form handling
first cut of this lib is targeting defining domain data types, as the second step I was planning to work on form-specific logic (with custom builtin types/coercions dedicated for this concern)
Tim Riley
@timriley
Nov 02 2015 10:33
@solnic yeah, I did see you mention something like this! I figured if we could get it going well enough as-is, then it'll be a nice transition later on to the built-in form handling stuff :)
Piotr Solnica
@solnic
Nov 02 2015 10:42
@timriley I’ll be updating it very soon, just need to get the rodakase basic stuff up and running
Tim Riley
@timriley
Nov 02 2015 10:46
No problems. I'll happily roadtest the thing for you.