These are chat archives for dry-rb/chat

6th
Jul 2016
Tim Riley
@timriley
Jul 06 2016 00:36
I’m interested in us having a dry-fixtures kind of thing to help with this
for both object construction and DB population
not sure if that’s one or two gems :)
But right now, @joevandyk, we just have helper methods in our test suite to help with this
Joe Van Dyk
@joevandyk
Jul 06 2016 01:03
@timriley i
I think it would be two gems
One I'd assume would be in the ROM project
Tim Riley
@timriley
Jul 06 2016 01:08
hmm, perhaps, yeah
Tim Riley
@timriley
Jul 06 2016 01:32
I might actually explore making an adapter for https://github.com/paulelliott/fabrication first
Dmitriy Plekhanov
@onemanstartup
Jul 06 2016 08:10
How about https://github.com/ginty/cranky ? It has no dependencies
Piotr Solnica
@solnic
Jul 06 2016 08:36
We can add it as a test helper to dry-struct (we are extracting struct from types btw)
Piotr Solnica
@solnic
Jul 06 2016 08:48
we could do stuff like define types for common data with meta info, and use that to generate values automatically
ie Email = Types::Strict::String.meta(email: true) etc
Piotr Solnica
@solnic
Jul 06 2016 09:56
def initialize_placeholders!
  @placeholders = paths.reduce({}) do |hash, path|
    curr_idx = 0
    last_idx = path.size - 1
    node = hash

    while curr_idx <= last_idx do
      key = path[curr_idx]
      node = (node[key] || node[key] = curr_idx < last_idx ? {} : [])
      curr_idx += 1
    end

    hash
  end
end
dear lord what have I done :laughing:
Oskar Szrajer
@gotar
Jul 06 2016 09:58

# TODO: Refactor me ;(

add this and will be ok

:P
Piotr Solnica
@solnic
Jul 06 2016 10:02
actually no, this must be like that :P
Alexander Gorkunov
@gorkunov
Jul 06 2016 10:10
Trying to build unique_email predicate for dry-validation. For the start I used !User.where(email: value).exists?. This works great for user create but fails when I try update existing user. I see two ways: improve predicate and pass user_id somehow or improve validation rules and remove email from validation if there are no changes. What do you think?
Tim Riley
@timriley
Jul 06 2016 10:12
@gorkunov we’ve done something similar for slug uniqueness checks, I can show you how we did it
We pass the previous value as well as the new one. If they’re the same (i.e. if the user hasn’t tried to update the unique value) then we don’t run the uniqueness check.
Alexander Gorkunov
@gorkunov
Jul 06 2016 10:16
I see, thanks @timriley!
Tim Riley
@timriley
Jul 06 2016 10:16
I’m sure there are other approaches, too, @gorkunov, this is just one that worked for us :)
Chris Richards
@cmrichards
Jul 06 2016 15:20

I think the rule given on the high-level rules page in the dry-validation docs should be this :

rule(barcode_only_or_sample_and_job: [:barcode, :job_number, :sample_number]) do |barcode, job_num, sample_num|
    (barcode.filled? & (job_num.none? & sample_num.none?)) |
    (barcode.none?  & (job_num.filled? & sample_num.filled?))
end

Would it work? Either the barcode field is present, or both the job_num and sample_num fields are present. Anything else is invalid.

Piotr Solnica
@solnic
Jul 06 2016 17:07
just tried to keep the example shorter
would be nice to come up with sth simpler
ie 2 values instead of three
funny part - docs don’t document ~50% of the features O_o
Piotr Solnica
@solnic
Jul 06 2016 18:17
@fran-worley so, I’ve been thinking about what should be improved in dry-logic and there are 2 major things we could do
  • come up with a better AST - like we briefly talked about yesterday, but there’s more to it…ie dry-v now adds its own :error nodes, this should not be needed if dry-logic had a better AST; so ie we could add :failure and :success result nodes, and see where we don’t need to provide result “names” (which can be a symbol or a path array) so that the AST could shrink
  • tweak things a bit so we could apply rules in 2 modes: full tracing of success/failures or failures exclusively - and this will make dry-v work super fast even when everything is valid (it’s currently a huge bottleneck)
now, previously I’d be scared like hell when changing anything in the dry-logic AST but after refactoring message compilers in dry-v it’s no longer the case :)
Fran Worley
@fran-worley
Jul 06 2016 18:18
This I like, can you chuck this in a dry-logic issue so I don't loose it. I'm out right now without my laptop!
Piotr Solnica
@solnic
Jul 06 2016 18:19
oh sorry sure will do :)
Piotr Solnica
@solnic
Jul 06 2016 19:48
just made some tweaks and it seems like dry-v is getting faster the more fields it has to validate :laughing:
dry-rb/dry-validation (master«) % COUNT=2 be ruby benchmarks/benchmark_sch
Warming up --------------------------------------
ActiveModel::Validations
                       293.000  i/100ms
dry-validation / schema
                         1.066k i/100ms
Calculating -------------------------------------
ActiveModel::Validations
                          2.923k (± 3.7%) i/s -     14.650k in   5.018563s
dry-validation / schema
                         10.716k (± 3.9%) i/s -     54.366k in   5.081371s

Comparison:
dry-validation / schema:    10715.7 i/s
ActiveModel::Validations:     2923.0 i/s - 3.67x slower

dry-rb/dry-validation (master«) % COUNT=8 be ruby benchmarks/benchmark_sch
Warming up --------------------------------------
ActiveModel::Validations
                        77.000  i/100ms
dry-validation / schema
                       317.000  i/100ms
Calculating -------------------------------------
ActiveModel::Validations
                        778.5953.1%) i/s -      3.927k in   5.048209s
dry-validation / schema
                          3.255k (± 3.7%) i/s -     16.484k in   5.070958s

Comparison:
dry-validation / schema:     3255.2 i/s
ActiveModel::Validations:      778.6 i/s - 4.18x slower

dry-rb/dry-validation (master«) % COUNT=12 be ruby benchmarks/benchmark_sc
Warming up --------------------------------------
ActiveModel::Validations
                        50.000  i/100ms
dry-validation / schema
                       215.000  i/100ms
Calculating -------------------------------------
ActiveModel::Validations
                        501.7634.8%) i/s -      2.550k in   5.094998s
dry-validation / schema
                          2.182k (± 3.3%) i/s -     10.965k in   5.030971s

Comparison:
dry-validation / schema:     2181.9 i/s
ActiveModel::Validations:      501.8 i/s - 4.35x slower
notice the more fields we validate (configured via COUNT) the better the result compared to AM::V :)
Peter Leitzen
@splattael
Jul 06 2016 20:31
👍🏻🤓
Steve
@dnd
Jul 06 2016 22:59
how do you pass a hash with string keys when creating a new dry-types struct? dry-types throws an error indicating it's looking for a symbolized version of the key.
Tim Riley
@timriley
Jul 06 2016 23:01
Steve
@dnd
Jul 06 2016 23:11
@timriley thanks. Will it cause the constructor to accept both types, or will it then only accept string keys?
or is it even possible to pass it in to .new()?
Tim Riley
@timriley
Jul 06 2016 23:22
@dnd yep, it accepts both types