These are chat archives for dry-rb/chat

17th
Nov 2017
Tab Loid
@Tab10id
Nov 17 2017 06:26

@solnic Hi. I have some questions about error message generation in dry-validation.
I think that way of getting tokens for messages is too implicit. I really confident that naming tokens exactly like predicate arguments is not intuitive=).
In result we have no possible to control error message formating.
Example:

configure do
  def self.messages
    super.merge(
      en: {errors: {good?: '%{item_name} is not good=('}}
    )
  end

  def good?(item)
    item.pretty? && item.funny?
  end
end

required(:items).each(:good?)

I want to get beautifull error message but I have no item_name token. I have only item but can't use it (I want to use something like item.name or format(item) instead of item.to_s)
similar request from discourse: https://discourse.dry-rb.org/t/dry-validation-messages-formating/361
By theory I can decorate my object and define to_s for this purpose but it's kinda hacky and it not solve problem when I need to have several arguments from predicate for generate right message.
I spent 5 hours yesterday in debugger but I still not fully understand how tokens are generated but I think that we have a bigger problem with this in custom validations (am I right?). Have you any ideas about all of that or the right way is moving message generation from dry-validation?

Piotr Solnica
@solnic
Nov 17 2017 08:02
@abinoam @jodosha we good pushing dry-inflector 0.1.0? :)
asking because I just merged this rom-rb/rom@ca76a02
and hoping to release rom 4.1.0 soooooooooon
@Tab10id you gotta wait for 1.0.0, I won't have time to improve anything in 0.x versions
@Tab10id btw if things go well, I'll have time for dry-v work during xmas break
ernepriv
@ernepriv
Nov 17 2017 08:09

Hi, i have a problem to combine different classes in relation each other, so after i set them how rom.rb teaches me (http://rom-rb.org/4.0/learn/core/combines/#basic-combine), when i call

repo = Attrezzature::Repositories::Myclass.new
repo.myclass.combine(:other_class).by_id(183).to_a

i obtain:

KeyError: nil attribute doesn't exist in dipendenti schema
from /home/ernesto/.rvm/gems/ruby-2.3.3/gems/rom-core-4.0.2/lib/rom/schema.rb:198:in `[]'
Tab Loid
@Tab10id
Nov 17 2017 08:10
@solnic I want to know is the error message generation is part of dry-validation gem or you see this as another gem. As I see main purpose of dry-validation is to say that something right or wrong. Fully formatting errors seems to me as another problem.
Piotr Solnica
@solnic
Nov 17 2017 08:11
@ernepriv wrong chat :)
@Tab10id it's part of dry-v but it's separated internally
dry-v produces result objects which are converted into error messages
ernepriv
@ernepriv
Nov 17 2017 08:12
@solnic i edit it :P
Tab Loid
@Tab10id
Nov 17 2017 08:12
@solnic thanks for answers
ernepriv
@ernepriv
Nov 17 2017 08:12
@solnic can you help me?
Luca Guidi
@jodosha
Nov 17 2017 08:14
@solnic @abinoam I quickly went through the issues/PRs, everything LGTM. :sparkles: :100: Do you want me to push the first release now?
Piotr Solnica
@solnic
Nov 17 2017 08:15
[1] pry(main)> s = Dry::Validation.Schema { required(:name).maybe(:str?) }
[2] pry(main)> s.(name: 1).message_set
=> #<Dry::Validation::MessageSet:0x007fd34005d5f0
 @failures=[#<Dry::Validation::Message predicate=:str? path=[:name] text="must be a string" options={:args=>[], :input=>1, :rule=>:name, :check=>nil}>],
 @hints=[],
 @messages=[#<Dry::Validation::Message predicate=:str? path=[:name] text="must be a string" options={:args=>[], :input=>1, :rule=>:name, :check=>nil}>],
 @options={:failures=>true},
 @paths=[[:name]],
 @placeholders={:name=>[]}>
@Tab10id ^
@jodosha yeah let's DO THIS :)
I can add it quickly to dry-rb.org website too
[3] pry(main)> s.(name: 1).to_ast
=> [:set, [[:failure, [:name, [:failure, [:name, [:key, [:name, [:predicate, [:str?, [[:input, 1]]]]]]]]]]]]
@Tab10id also this ^
Tab Loid
@Tab10id
Nov 17 2017 08:17
@solnic Yeah I'm already seen this, thanks
@solnic Actually I already spent about half day in debugger around this=)
Piotr Solnica
@solnic
Nov 17 2017 08:23
hah ok ;)
irb(main):008:0> i = Dry::Inflector.new
irb(main):009:0> i.extend(Transproc::Registry)
irb(main):012:0> gimme_a_class = i[:camelize] >> i[:constantize]
irb(main):013:0> gimme_a_class['foo_bar']
=> FooBar
@jodosha FYI ^^
Luca Guidi
@jodosha
Nov 17 2017 08:27
@solnic OMG :scream_cat: I :heart: it!
Piotr Solnica
@solnic
Nov 17 2017 08:28
inorite ;)
Luca Guidi
@jodosha
Nov 17 2017 08:29
ahahahha I mean, we need to standardize that, but I love that we're a step away from it.
Piotr Solnica
@solnic
Nov 17 2017 08:29
yeah sure thing
just showing off that this just works
@jodosha anyhow, added dry-inflector to the website (not published yet)
Gustavo Caso
@GustavoCaso
Nov 17 2017 08:37
Congrats everyone for the effort for dry-inflector
Luca Guidi
@jodosha
Nov 17 2017 08:45
@GustavoCaso TY!
Tim Riley
@timriley
Nov 17 2017 09:10
@jodosha @solnic yeah, awesome work wrangling this. It came together so quickly.
Piotr Solnica
@solnic
Nov 17 2017 09:14
@timriley speaking of which, is menus an exception wrt pluralization? ie menus => menus but plexus => plexuses etc
Tim Riley
@timriley
Nov 17 2017 09:16
menus is just plural for menu, isn’t it?
I don’t know why you’d want to pluralize an already-plural, which is what you’re saying with menus => menus
@solnic ^
Piotr Solnica
@solnic
Nov 17 2017 09:25
@timriley yes, it happens a lot in rom
and so it stopped working because dry-inflector does menus => menuses whereas inflecto/as::i would not do it
Tim Riley
@timriley
Nov 17 2017 09:28
so something in rom is trying to pluralize an already-pluralized word?
I wouldn’t expect an inflector to be able to reliably detect when something is plural and then choose not to pluralize it again
that’s just too hard (IMO)
Easy to do naively
Hard to do properly
Piotr Solnica
@solnic
Nov 17 2017 09:34
@timriley well it is what it is, so far it worked with other inflection backends
we do it, because you may create a rel alias for association names, ie belongs_to :user translates to many_to_one :users, as: :user
then whenever input with the key :user is being handled, we gotta find matching relation
Tim Riley
@timriley
Nov 17 2017 09:36
so you need to pluralize user from that original belongs_to declaration?
Piotr Solnica
@solnic
Nov 17 2017 09:36
but there are cases, where it's already pluralized
Tim Riley
@timriley
Nov 17 2017 09:36
so you’re allowing people to write both belongs_to :user AND belongs_to :users?
Piotr Solnica
@solnic
Nov 17 2017 09:36
so pluralize(:users) happens
Tim Riley
@timriley
Nov 17 2017 09:36
jeepers
Anyway, yeah, menus => menuses is totally incorrect
but I also think that we should try to avoid pluralizing already-plurals if we can :grimacing:
Piotr Solnica
@solnic
Nov 17 2017 09:40
interesting
I can't reproduce this problem anymore
oh wait nevermind ;)
used the wrong branch
Piotr Solnica
@solnic
Nov 17 2017 10:31
w00t w00t :)
Gustavo Caso
@GustavoCaso
Nov 17 2017 10:31
:clap: :clap:
Nikita Shilnikov
@flash-gordon
Nov 17 2017 10:42
awesome!
believe it or not, we now have an instance-level inflector :)
Piotr Solnica
@solnic
Nov 17 2017 10:47
only took 15 years
Luca Guidi
@jodosha
Nov 17 2017 13:02
😜
Son Dang
@sondnm
Nov 17 2017 14:04
big thanks to the contributors of dry-inflector gem :)

I'm looking at ordinalize method and found out that it returns nil with cases like:

inflector = Dry::Inflector.new
inflector.ordinalize(17) # nil
inflector.ordinalize(24) # nil
inflector.ordinalize(25) # nil
inflector.ordinalize(34) # nil

should it be a correct behaviour?

Wojciech Maciejak
@wmaciejak
Nov 17 2017 16:08
Hi guys! I created container with mixin, is there any way to stub it?
class Container
  class DryContainer
    include Dry::Container::Mixin
  end

  attr_reader :app, :container

  class << self
    attr_reader :instance
  end

  def self.configure
    container = DryContainer.new
    yield(container)
    @instance = new(Rails.application, container)
    freeze
  end

  def initialize(app, container)
    @app = app
    @container = container
  end

  delegate :[], to: :container
end
Nikita Shilnikov
@flash-gordon
Nov 17 2017 16:13
@wmaciejak http://dry-rb.org/gems/dry-container/testing/ did you see this?
Luca Guidi
@jodosha
Nov 17 2017 16:47
@sondnm Nope, it's not correct, I'm writing a fix.
@sondnm dry-rb/dry-inflector#14
Son Dang
@sondnm
Nov 17 2017 17:04
@jodosha cool!
Abinoam P. Marques Jr.
@abinoam
Nov 17 2017 17:11
@sondnm Good catch!
Son Dang
@sondnm
Nov 17 2017 17:17
thanks :smile:
Abinoam P. Marques Jr.
@abinoam
Nov 17 2017 17:27
FYI this bugs also affects Inflecto gem! :scream:
But, thanks to you and @jodosha will not affect Dry::Inflector anymore :sunglasses:
Son Dang
@sondnm
Nov 17 2017 17:31
I'm happy that helps :sparkles:
Luca Guidi
@jodosha
Nov 17 2017 21:13
I just merged the fix on master. I'll wrap a new release on tomorrow.