These are chat archives for dry-rb/chat

13th
Feb 2017
Oskar Szrajer
@gotar
Feb 13 2017 07:47
inside Athit
gotar ~/Programowanie/Athit (master) ~ $ tree system/
system/
├── athit
│   ├── application.rb
│   ├── assets.rb
│   ├── container.rb
│   ├── import.rb
│   └── settings.rb
├── boot
│   ├── bugsnag.rb
│   ├── i18n.rb
│   ├── monitor.rb
│   └── rom.rb
└── boot.rb

2 directories, 10 files
@timriley probably you guys will update berg in the (i hope near) feature. So I will check there and compare where I might have a problem
Alejandro E. Babio
@alejandrobabio
Feb 13 2017 15:51

Hi, I'm thinking on move business logic to a dry-transaction in a Rails app. As first try I built a class for create a Post:

class Post
  class Container
    extend Dry::Container::Mixin

    register :build_params, -> (params) {
      Dry::Monads.Right(params[:post])
    }

    register :validate, -> (input) {
      if input[:body].nil? || input[:title].nil?
        Dry::Monads.Left(:not_valid)
      else
        Dry::Monads.Right(input)
      end
    }

    register :persist, -> (input) {
      Post.create Dry::Monads.Right(input)
    }
  end

  class CreatePost
    def initialize(params)
      save_user = Dry.Transaction(container: Container) do
        step :build_params
        step :validate
        step :persist
      end
      save_user.call(params)
    end
  end
end

It should create a new Post when it's called with:

Post::CreatePost.new(post: {body: 'ab', title: 'AB'})

But I'm getting the error:

NoMethodError: undefined method `Right' for Dry::Monads:Module
from /home/alejandro/.rvm/gems/ruby-2.3.3@rails501/gems/dry-transaction-0.9.0/lib/dry/transaction/api.rb:40:in `call'

Have you some idea of what I'm doing wrong? I tried to build something pretty similar to the basic example of dry-transaction.

Nikita Shilnikov
@flash-gordon
Feb 13 2017 15:54
@alejandrobabio can be easily fixed with require 'dry/monads' :)
Alejandro E. Babio
@alejandrobabio
Feb 13 2017 15:55
Good! Thank you!
Nikita Shilnikov
@flash-gordon
Feb 13 2017 15:55
not all the monads are required for dry-transaction hence those methods are not required by default
Alejandro E. Babio
@alejandrobabio
Feb 13 2017 15:57
Thanks for say that. Now it's working.