These are chat archives for dry-rb/chat

5th
May 2017
Piotr Rybarczyk
@Argonus
May 05 2017 09:45
Hi all. I ma trying to make custom rule it should look like this: rule(variant_presence: [:variant_id, :sku]) do |variant_id, sku| variant_id.filled? | (sku.filled? & Spree::Variant.where(sku: value(:sku)).any?) end But i have +arel+ is not a valid predicate name error. I really cant find nothing about this in documentation.
Nikita Shilnikov
@flash-gordon
May 05 2017 09:59
@Argonus you need to define a custom predicate for this
schema = Dry::Validation.Schema do
  configure do
    def sku_exist?(value)
      Spree::Variant.where(sku: value).limit(1).any?
    end
  end

  rule(...) do |...|
    sku.filled? & sku_exist?(value(:sku))
  end
end
like this
you can't use arbitrary ruby code in rule blocks becasuse it is evaluated before you apply it to your data
Piotr Rybarczyk
@Argonus
May 05 2017 10:04
Thanks :) just found that. As @musaffa said, that would be great to have some guides. I really love idea of dry-rb.
Piotr Rybarczyk
@Argonus
May 05 2017 10:29
@flash-gordon One more thing, when i try to do result.errors i have got error like this
NoMethodError: undefined method<<' for #<Hash:0x00559f085ca4c0>` there is something about rules erros which i should know
Nikita Shilnikov
@flash-gordon
May 05 2017 10:32
Don't think so
Piotr Rybarczyk
@Argonus
May 05 2017 10:48
I think that there is issue when you would like to use custom predicate with rule.
Nikita Shilnikov
@flash-gordon
May 05 2017 10:55
Or I wrote a broken example :) I'm not dry-v-savvy tbf :)
I'll double-check this a bit later
Piotr Solnica
@solnic
May 05 2017 10:57
@Argonus for arbitrary ruby code you can use validate(variant_presence: [:variant_id, :sku]) { |v,s| … }
rule blocks are evaluated in the context of the DSL, it’s a BasicObject and syntax is limited
Piotr Rybarczyk
@Argonus
May 05 2017 11:19
@solnic thanks it's working perfectly.
Nikita Shilnikov
@flash-gordon
May 05 2017 11:28
@Kukunin psst, looks like I successfully run dry-struct 0.3.0 in production ;)
I'll update changelogs tonight
Sergey Kukunin
@Kukunin
May 05 2017 15:43
@flash-gordon great. I'm updating right now. Having small issue with new rom-sql and sequel deprecations
great job, anyway!
Nikita Shilnikov
@flash-gordon
May 05 2017 15:45
@Kukunin you can pin to sequel 4.45 for now, we'll deal with warnings in a while
Sergey Kukunin
@Kukunin
May 05 2017 15:47
thanks for the advice. will do
Joe Van Dyk
@joevandyk
May 05 2017 18:43
Does dry-struct or other library provide a way to modify a dry-struct? It's immutable so I guess a simple way to create a new one based off an existing one but with a few fields changed
Nikita Shilnikov
@flash-gordon
May 05 2017 18:51
100% correct :)
Thanks to @Kukunin it is as simple as struct_value.new(field_1: new_value)
Joe Van Dyk
@joevandyk
May 05 2017 19:13
oooh thx
should that be documented at http://dry-rb.org/gems/dry-struct/ ?
hmm didn't seem to work
[24] pry(main)> wave.class.superclass
=> Dry::Struct
[25] pry(main)> wave.new
NoMethodError: undefined method `new' for #<PackingWaves::PackingWave:0x000000069824d8>
@flash-gordon ^
ah is this super new?
upgraded and it worked, nm
Nikita Shilnikov
@flash-gordon
May 05 2017 19:23
It' super hot! :)
I'm about to write changelogs)