These are chat archives for dry-rb/chat

21st
Apr 2016
Tim Riley
@timriley
Apr 21 2016 04:25
dry-web just got its first rubygems release: http://rubygems.org/gems/dry-web
Tim Riley
@timriley
Apr 21 2016 05:00
I also started documenting the skeleton that we use for icelab dry-web projects: https://github.com/icelab/dry-web-skeleton
Fran Worley
@fran-worley
Apr 21 2016 07:52
@timriley looks good I'll try and find the time to have a play with it at the weekend!
Tim Riley
@timriley
Apr 21 2016 08:15
@fran-worley cool! Would love your feedback on how things feel. In the next 2 months, I want to focus on massively improving the dry-web experience for newcomers.
S/massively/as best I can/ :)
Tim Cooper
@coop
Apr 21 2016 08:47
Tim Riley
@timriley
Apr 21 2016 08:57
Oh, thanks @coop!
Benjamin Klotz
@tak1n
Apr 21 2016 09:01
@timriley awesome, I think I will soon start a toy project with dry-web etc :)
Tim Cooper
@coop
Apr 21 2016 09:10
It was a good explaination without touting that “it is better”. Well done.
Fran Worley
@fran-worley
Apr 21 2016 09:28
@timriley you have the power of words my friend. I avoid trying to put my thoughts in comments as I just sound opinionated... Well said!
Tim Riley
@timriley
Apr 21 2016 09:36
Ta :blush:
@tak1n great! Would love to hear how you go. Let me know if you have question.
BTW, at Icelab we're currently building our now company site as a dry-web app started from this skeleton. It's still very early, but it may serve as a helpful point of reference. It's on GitHub as icelab/berg.
Benjamin Klotz
@tak1n
Apr 21 2016 09:39
@timriley cool thx :D
Tim Riley
@timriley
Apr 21 2016 09:39
I hope to bring our other young example app (icelab/Alpinist) up to date soon too. that one hasn't been touched since December, and we've progressed our dry-web setup a fair bit since then.
Benjamin Klotz
@tak1n
Apr 21 2016 09:40
yea I have seen it, I was curious how dry-v was used in dry-web and in alpinist you subclass Dry::V::Schema which is no longer possible I think
Tim Riley
@timriley
Apr 21 2016 09:46
Yeah. Old school and now broken :grimacing:
Fran Worley
@fran-worley
Apr 21 2016 09:54
@timriley is it worth adding your explanation of dry-v #113 to the dry-rb.org site?
It might be useful to just explain the purpose of the
Tim Riley
@timriley
Apr 21 2016 10:15
Yeah, I reckon.
Kuba Krzempek
@kubakrzempek
Apr 21 2016 11:18

Hi everyone, I'm writing a form object powered by dry-validation. Let's say the form has two keys - :code and :organization_id.
I want to validate whether a pair [:code, :organization_id] is unique in my db, yet I'm failing in doing so.
First I though about using rule(uniq_code: [:code, :organization_id]) but it clearly wants me to run predicates on the keys I've passed into it, so I decided to go with my custom predicate and ended up with the following

Create = Dry::Validation.Form do
  key(:code).required(uniq?: "organization_id")
  key(:name).required
  key(:countries_id).required
  key(:description).required
  key(:organization_id).required
  key(:active).required

  configure do
    option :market_repo, WheelsApp.instance["repositories.market"]

    def uniq?(organization_id, value)
      # query logic here
    end
  end

How can I pass to uniq? method a value of :organization_id that comes from object I've passed into the form?

I tried with value(:organziation_id) but that returns something like an ast graph
Tim Riley
@timriley
Apr 21 2016 11:37
@kubakrzempek if you want to work on the two at once, I think you need to make a "high level rule" (it's in the docs) which could then pass those two values to a predicate to check for uniqueness of both
Simon Schmid
@sled
Apr 21 2016 11:37
not sure whether it works though ;)
Kuba Krzempek
@kubakrzempek
Apr 21 2016 12:01
@timriley @sled thank you for your answers. As you suggested I went back on "high level rule" track and it looks like this now
https://gist.github.com/kubakrzempek/f6c15643b9adae7e3de27a6b946aee30
but it still doesn't work, because sequel queries expect organization_id and code to be values, not Dry::Validation::Schema::Rule objects.
I commented the unique? method's body and returned false explicitly, and the rule(:uniq_code_organization) failed, because it didn't expected to receive a FalseClass object.
Do you know any other way to implement block assigned to rule?
Andy Holland
@AMHOL
Apr 21 2016 12:04
@kubakrzempek you know you passed the arguments to unique? in the wrong order?
@kubakrzempek I'm not sure how to do what you're trying to do but looking at http://dry-rb.org/gems/dry-validation/high-level-rules/ and http://dry-rb.org/gems/dry-validation/custom-predicates/ what you have so far isn't right
Christopher Dennl-Ortega Arrieta
@cdennl
Apr 21 2016 12:11
@AMHOL no it is not wrong order
Andy Holland
@AMHOL
Apr 21 2016 12:12
It's also better to have your repositories return materialized results, rather than having query logic leak out into other parts of your application
Christopher Dennl-Ortega Arrieta
@cdennl
Apr 21 2016 12:12
@AMHOL the order is reversed
Andy Holland
@AMHOL
Apr 21 2016 12:12
Ahh OK
Christopher Dennl-Ortega Arrieta
@cdennl
Apr 21 2016 12:12
got trapped by this as well a week ago
but solnic meant this is intended
Simon Schmid
@sled
Apr 21 2016 12:16
    rule(code_organization_uniq: [:code, :organization_id]) do |code, organization_id|
      unique?(code, organization_id)
    end
can you give me the .inspect output of the arguments?
is it a <#Rule ...> object?
Andy Holland
@AMHOL
Apr 21 2016 12:17
It looks like the return value from rule should be a predicate
Simon Schmid
@sled
Apr 21 2016 12:18
it looks like the .value method evaluates a predicate and just returns true/false
Andy Holland
@AMHOL
Apr 21 2016 12:20
I'm thinking maybe this isn't supported yet
dry-rb/dry-validation#105
Christopher Dennl-Ortega Arrieta
@cdennl
Apr 21 2016 12:26
@kubakrzempek your gist should work, if not then it is a sequel problem, because thats how i wrote my own predicates as well, take a look:
panigrah
@panigrah
Apr 21 2016 12:27
in the new dry-v how do i check for property is empty or if present then it is inclusion: {in: 0..5}
Christopher Dennl-Ortega Arrieta
@cdennl
Apr 21 2016 12:29
module Dagavel::Predicates
  include Dry::Logic::Predicates

  predicate( :is_conversion_correct? ) do |exchange_rate_usd_points, points, usd|
    points_in_usd = ( Auction.convert_points_to_money( points, exchange_rate_usd_points ) * 100 ).floor
    usd = ( usd * 100 ).floor

    usd == points_in_usd
  end
end

...

rule( start_bid_conversion_is_correct: [ :start_bid_in_points, :start_bid_in_usd, :exchange_rate_usd_points ] ) do
  |start_bid_in_points, start_bid_in_usd, exchange_rate_usd_points|
        ( start_bid_in_points.int? & start_bid_in_usd.float? & exchange_rate_usd_points.int? ).then(
            start_bid_in_usd.is_conversion_correct?( start_bid_in_points, exchange_rate_usd_points )
        )
      end
Andy Holland
@AMHOL
Apr 21 2016 12:30
@cdennl @kubakrzempek what version/branch are you both using?
Christopher Dennl-Ortega Arrieta
@cdennl
Apr 21 2016 12:31
reform 2-2 branch, dry-v 0.7.4
0.7.3 sorry
Andy Holland
@AMHOL
Apr 21 2016 12:32
Your rule returns the result of a predicate tho, so it's not the same as @kubakrzempek's
Christopher Dennl-Ortega Arrieta
@cdennl
Apr 21 2016 12:32
yes you have to define a predicate
but he defined one as well
he just used inline definition
where mine resides in a module because i need it several times
Andy Holland
@AMHOL
Apr 21 2016 12:35

Looks like changing rule to

    rule(code_organization_uniq: [:code, :organization_id]) do |code, organization_id|
      code.unique?(organization_id)
    end

Should fix it

Left a comment on the gist @kubakrzempek
Kuba Krzempek
@kubakrzempek
Apr 21 2016 12:36
@AMHOL that works!!!
Thank you a milion!
Andy Holland
@AMHOL
Apr 21 2016 12:36
Cool, np, and thanks @cdennl for pointing that out
Simon Schmid
@sled
Apr 21 2016 12:36
half of dry-*'s awesomeness is this channel ;)
Kuba Krzempek
@kubakrzempek
Apr 21 2016 12:36
Thank all of you for your help!
Christopher Dennl-Ortega Arrieta
@cdennl
Apr 21 2016 12:36
np
Andy Holland
@AMHOL
Apr 21 2016 12:37
@sled :D
Christopher Dennl-Ortega Arrieta
@cdennl
Apr 21 2016 12:37
yeah predicates must run on rules it seems
Andy Holland
@AMHOL
Apr 21 2016 12:39
@panigrah what do you mean by "empty"?
nil, and empty array, an empty string?
panigrah
@panigrah
Apr 21 2016 12:39
@AMHOL it can be nil or empty string
Andy Holland
@AMHOL
Apr 21 2016 12:41
Or an integer?
Christopher Dennl-Ortega Arrieta
@cdennl
Apr 21 2016 12:41
optional(:my_string).maybe(size?: 0..5) in the old syntax
@panigrah
sorry didnt read that you asked forn ew syntax
Andy Holland
@AMHOL
Apr 21 2016 12:44
Ahh, sorry, I was thinking the value should be between 1 and 5
panigrah
@panigrah
Apr 21 2016 13:09

thanks! it also works for optional(:my_number).maybe(inclusion?: 1..5) BUT if I do the below

       schema = Dry::Validation.Schema do
          optional(:num).maybe(inclusion?: 1..5)
       end
       errors = schema.call(abc: '1', num: 6)

I get a NoMethodError. input.rb:86:inoptions_for_inclusion?': undefined method join' for 0..5:Range (NoMethodError)

Andy Holland
@AMHOL
Apr 21 2016 13:41
@panigrah are you using the master branch?
Maybe try included_in? dry-rb/dry-logic@e649fad
Sorry I'm not much help, I haven't actually used dry-validation myself yet
panigrah
@panigrah
Apr 21 2016 13:44
i wasn't on master. Switched to master and it works now. Thanks for the tip.
Andy Holland
@AMHOL
Apr 21 2016 13:44
Cool
panigrah
@panigrah
Apr 21 2016 13:45
and one other change I made - passed the 1..5 in an array. optional(:age).maybe(inclusion?:[1..5])
Andy Holland
@AMHOL
Apr 21 2016 13:58
Hmm, would think it should work with a range
panigrah
@panigrah
Apr 21 2016 13:59
So this works optional(:age).maybe(inclusion?: [1, 2, 3, 4, 5]) but this fails optional(:age).maybe(inclusion?: 1..5) on master. I spoke too soon - it was failing even on master - but the error was getting redirected so didnt realize.
Andy Holland
@AMHOL
Apr 21 2016 14:01
Yeah, looking at dry-logic it doesn't support a range argument
Fran Worley
@fran-worley
Apr 21 2016 15:31
@AMHOL can you raise an issue and I'll update the predicate before the next ruby gems release?
Andy Holland
@AMHOL
Apr 21 2016 15:44
@fran-worley had a second look and I'm not 100% sure that's the case now
So Range#include? should work I think?
Fran Worley
@fran-worley
Apr 21 2016 15:56
I'll try it in my unreleased branch of Dry-V. If that works then it will be fixed when we can release dry logic on ruby gems.
I don't see that it will make much difference as I've not actually changed anything but the names really...
Simon Schmid
@sled
Apr 21 2016 16:03
there's something interesting here:
```
  predicate(:included_in?) do |list, input|
    list.include?(input)
  end
  predicate(:includes?) do |value, input|
    input.include?(value)
  end
why are the arguments the other way around?
Fran Worley
@fran-worley
Apr 21 2016 16:04
Yeah, that is not actually being used in DRY-V yet
dry-rb/dry-logic#9
the idea being: input_value.included_in?(list) vs input_value.includes?(value)
Simon Schmid
@sled
Apr 21 2016 16:06
ah now I get it :)
Fran Worley
@fran-worley
Apr 21 2016 16:06
so we had to rename inclusion? and exclusion? to make it clear what the 2 do
Fran Worley
@fran-worley
Apr 21 2016 16:12
@panigrah try optional(:age).maybe(inclusion?: (1..5)) I've got it working when the range is wrapped in parentheses
Andrew Kozin
@nepalez
Apr 21 2016 20:29

One more thing provided by @marshall-lee for initializer in our commercial project is the ar_param or ar_option.
It allows to set object dependency as either model or model id.

In Rails projects this proves to be very suitable (example below is over-simplified):

class DestroyUser
  ar_param :user

  def call
    user.destroy
  end

  def self.call(*args)
    new(*args).call
  end
end

user_id = 1
user = User.find 1

# This methods are the same
DestroyUser.call user_id
DestroyUser.call user

Under the hood the method either gets an activerecord instance or finds its by id (or by another argument):

ar_param :user, find_by: :key

The question is what do you think about implementing this behaviour in a submodule of dry-initializer (dry-initializer/rails)?

Andy Holland
@AMHOL
Apr 21 2016 20:35
@nepalez wouldn't want something specifically targeting rails in a dry core gem, but I don't see any harm to add it as an external plugin, perhaps dry-initializer could benefit for some kind of plugin system anyway (assuming it doesn't have one already)
Andrew Kozin
@nepalez
Apr 21 2016 20:39
@AMHOL well, for one feature I'd say plugin system to be an overkill, but maybe you're right
Andy Holland
@AMHOL
Apr 21 2016 20:54
I think it makes sense to make it extendible, if there's this use case there will be more
Nikita Shilnikov
@flash-gordon
Apr 21 2016 21:03
guys, I raised an issue about replacing kleisli with something else. Any thoughts are very welcome dry-rb/dry-types#80 :)
Tim Riley
@timriley
Apr 21 2016 22:19
@nepalez yeah, I think explicit support of AR is outside of dry-rb's mission. So having another gem to add these methods in would seem better to me.
@flash-gordon I feel generally in favor of this. Will comment on the GH issue later on :)
panigrah
@panigrah
Apr 21 2016 23:30
@fran-worley i get the same error NoMethodError (undefined methodjoin' for 1..5:Range):`
Fran Worley
@fran-worley
Apr 21 2016 23:33
@panigrah I'll have a look tomorrow. I know it works with my updated predicates branch but it's not ready for release yet.
panigrah
@panigrah
Apr 21 2016 23:39
thanks Fran! when do you all plan to release the new schema docs?