These are chat archives for dry-rb/chat

28th
Jul 2016
Sergey Kukunin
@Kukunin
Jul 28 2016 02:13
hello guys. thanks for the great gems set
Tema Bolshakov
@bolshakov
Jul 28 2016 11:04

Hello, folks. Cold you explain to me, how to use container? I have a gem which may be configured with
custom class implementations, but defaults implementation is also provided. For examle:

Subscriptions.dependencies[:CancellationManager] = Subscriptions::DefaultCancellationManager.new

I want to replace my DI implementation with dry-cantainer, but could not figure out that is the
right way to register default dependency value in a gem, and provide ability to override it in an app?

Jean-Michel Garnier
@jmgarnier
Jul 28 2016 11:54
Hi, I have a similar question. When and where to initialize the container in a Rails app? I want to access a service configured in the container from a legacy class. In other words, how do you "inject" the container in legacy services?
Piotr Solnica
@solnic
Jul 28 2016 15:42
@jmgarnier you can find a whole rails skeleton here with dry-container/auto-inject setup https://github.com/solnic/rom-rails-skeleton
@bolshakov you can provide a dry-container and in an app its own container could override the defaults, we have Container#import method and IIRC will override exisiting stuff /cc @AMHOL
Andy Holland
@AMHOL
Jul 28 2016 16:03
Yeah #import will do that, just does a Hash#merge on the container
Piotr Solnica
@solnic
Jul 28 2016 16:04
how about we add some proper docs for this gem :laughing:
Andy Holland
@AMHOL
Jul 28 2016 16:06
Dunno what to add beyond http://dry-rb.org/gems/dry-container/ ?
Piotr Solnica
@solnic
Jul 28 2016 16:18
oh :D
Alexey Kuznetsov
@semenovDL
Jul 28 2016 16:40

Hi, guys! I try to migrate from reform 2.1 to 2.2 and to dry-validation, but get this error:

ArgumentError: +validates+ is not a valid predicate name
from /Users/semenovDL/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/dry-validation-0.9.3/lib/dry/validation/predicate_registry.rb:98:in `raise_unknown_predicate_error'

# Gemfile
gem 'rails', '~> 4.2.3'
# ...
gem 'dry-validation', '~> 0.9.3'
gem 'reform', '~> 2.2', '>= 2.2.1'

# config/initializers/dry_types.rb
module Types
  include Dry::Types.module
end

# config/initializers/reform.rb
require 'reform/form/dry'

Reform::Form.class_eval do
  include Reform::Form::Dry
end

# app/forms/basic_form.rb
class BasicForm < Reform::Form
  property :name
  validates :name, presence: true
end

Form have Reform::Form::Dry::Validations in ancestors. So the block record in Heritage.
What happend then?
Can you help me with it?

Alexey Kuznetsov
@semenovDL
Jul 28 2016 16:52
Find answer with Trailblazer community.
Fran Worley
@fran-worley
Jul 28 2016 17:14
@semenovDL that's because you've not written any dry-validations!! If you want to use dry-validations you need to replace your validates line with a validation block. It's all on the docs on the trailblazer site.
Piotr Solnica
@solnic
Jul 28 2016 17:27
hmm, why does it raise this error though? it’s like the class body was evaluated in the context of dry-v’s DSL
Alexey Kuznetsov
@semenovDL
Jul 28 2016 17:32
@solnic Probably reform store validations in Heritage hash and execute in context of current validator (dry-v in this case) https://github.com/apotonick/declarative/blob/master/lib/declarative/heritage.rb#L16
Fran Worley
@fran-worley
Jul 28 2016 17:37
No @semenovDL validates is a class method that just creates a validation block with validates as a line in it.
Alexey Kuznetsov
@semenovDL
Jul 28 2016 18:54
@fran-worley i talk about storing and evaluating block, that was created with :validates
Fran Worley
@fran-worley
Jul 28 2016 19:13
Yeah but in effect it is a validation block which is a schema which is why dry-v raises the error. Essentially @solnic it's not a dry-v problem it's an expected error as you shouldn't call dry-v that way.
Alexey Kuznetsov
@semenovDL
Jul 28 2016 19:34
@fran-worley :+1:
Darcy Laycock
@Sutto
Jul 28 2016 23:43
Hi all - I was wondering if anyone would be able to tell me if there is an easier way (vs. a custom predicate?) in dry-validation to basically take a free form hash, and apply validations to each key value
Piotr Solnica
@solnic
Jul 28 2016 23:47
@Sutto hey Darcy, good to see you here :) my standard question now: what's your usecase?
Darcy Laycock
@Sutto
Jul 28 2016 23:48
@solnic hey solnic! (Awesome work to you and everyone on the libraries btw - they're lovely!)
basically, we deal with a lot of external data that we normalize it all to standard json structure (hence dry-validation is a perfect fit for validating it) - One of the fields is structured roughly like:
{"stan": ["x", "y"], "netflix": ["12331"]}
where the keys vary on a per object basis, but are all strings - ditto on the values (we want to validate they're arrays of strings) - so we basically don't know keys up front, but want to validate the general rules
as another example, we do similar for a bunch of stuff - e.g. a scores field that is {"imdb": 75.0} etc
Piotr Solnica
@solnic
Jul 28 2016 23:58
@Sutto not supported yet :(
There's a feature request reported about this IIRC