These are chat archives for dry-rb/chat

10th
Jan 2017
Adam Davies
@adz
Jan 10 2017 01:04

Following on from experimenting with dry-validations…
How would you verify uniqueness when scoped to another record?

Say “account” has many “users”, and I want users.name to be unqiue within an account….

Tim Riley
@timriley
Jan 10 2017 02:06
@adz I’d write a predicate like unique_within_account?(account_id, user_name)
Adam Davies
@adz
Jan 10 2017 02:07
Thanks… how would that look when called? I’m a little confused about that
Tim Riley
@timriley
Jan 10 2017 02:07
lemme set something up to test :)
Adam Davies
@adz
Jan 10 2017 02:07
Much appreciated !
(there’s more if click through)
Adam Davies
@adz
Jan 10 2017 02:24
got it...
how would it look if “unique_within_account?” had a third param?
name.unique_within_account?(account_id)
-> ?
Tim Riley
@timriley
Jan 10 2017 02:25
the “value under test” is always the last param for custom predicates
so it’d be def unique_within?(first, second, the_value)
and fo name.unique_within?(“foo”, “bar”), first would be ”foo”, second would be ”bar”
is that what you’re after?
Adam Davies
@adz
Jan 10 2017 02:26
ah ok… yeah
trying different things and I think there are issues with docs
Tim Riley
@timriley
Jan 10 2017 02:26
Sorry about that :grimacing:
Adam Davies
@adz
Jan 10 2017 02:27
it shows like
required(:email).filled(scoped_unique?: :email, scope?: { active: true })
… i guess . does it change when under the ‘filled’?
defined inside configure:
def scoped_unique?(attr_name, scope, value)
Tim Riley
@timriley
Jan 10 2017 02:28
well, it’s just a different way of running that predicate
.filled(prdicate_name?: :args_go_here)
Adam Davies
@adz
Jan 10 2017 02:28
i understand it changes to “filled? & …"
Tim Riley
@timriley
Jan 10 2017 02:28
lemme try
I don’t think you can do that short-hand use of the predicates if you’re depending on another input value
the high-level rules work because they only run if the input values they require have passed any other validations
in that way they’re “safe”
and e.g. our predicate method can rely on its args being a certain type
Adam Davies
@adz
Jan 10 2017 02:33
In examples above I’m referring to docs here http://dry-rb.org/gems/dry-validation/comparison-with-activemodel/
Tim Riley
@timriley
Jan 10 2017 02:33
yeah. that’s passing static values as predicate args
Adam Davies
@adz
Jan 10 2017 02:34
Ok i get that… but i read it as ‘scope?’ being a diff predicate

so… if there are two args
`.filled(prdicate_name?: :args_go_here)

would it be:

`.filled(prdicate_name?: [:arg1, :arg2])

Tim Riley
@timriley
Jan 10 2017 02:34
yes
they’re splatted
Adam Davies
@adz
Jan 10 2017 02:34
so it doesn’t make sense in the example, right?
Tim Riley
@timriley
Jan 10 2017 02:35
no :(
Adam Davies
@adz
Jan 10 2017 02:35

required(:email).filled(scoped_unique?: :email, scope?: { active: true })

should be

required(:email).filled(scoped_unique?: [:email, { active: true }])

Tim Riley
@timriley
Jan 10 2017 02:36
yeah
Adam Davies
@adz
Jan 10 2017 02:36
its hard when you don’t know how the DSL is structured… i’m kind of inferring from examples
getting clearer
Ok — @timriley i updated my PR to the docs to correct
dry-rb/dry-rb.org#138 let me know if that is correct now?
Adam Davies
@adz
Jan 10 2017 02:42

One more thing — is it possible to infer the key of the predicate being tested?

that “required(:email).filled(unique?: :email)” has to repeat the ‘email’ key as an arg

Tim Riley
@timriley
Jan 10 2017 03:00
@adz AFAIK the predicates don’t know which key they’re being used on, no
I thought there might’ve been a GH issue about this, but I can’t find it.
Adam Davies
@adz
Jan 10 2017 04:28
ok
Adam Davies
@adz
Jan 10 2017 07:46
Anyone use dry pipeline? Seems like dry transaction supersedes it with similar functionality
Tim Riley
@timriley
Jan 10 2017 07:46
Yeah, dry-pipeline never really went anywhere
rom has something like it built internally for function composition but it’s not separate
Adam Davies
@adz
Jan 10 2017 07:47
I feel the urge to chain functions and seems to fit
Tim Riley
@timriley
Jan 10 2017 07:47
yeah, I use dry-monads and return monads to do that
maybe the pipeline thing is useful in other kinds of cases
Adam Davies
@adz
Jan 10 2017 07:47
Doing elm has corrupted me such that I'm uncomfortable with mutations :0
Tim Riley
@timriley
Jan 10 2017 07:47
:)
Adam Davies
@adz
Jan 10 2017 07:48
This message was deleted
This message was deleted
First time doing rails for
A year or so
... Dry stuff feels very nice :smiley:
Tim Riley
@timriley
Jan 10 2017 07:50
Good to hear! Still a way to go, but we’re getting there :)
Rafael
@lastk
Jan 10 2017 14:10
how do I require a field only if the value of another field is equals to X ?
Vasilis Spilka
@vasspilka
Jan 10 2017 16:08

hey there, a ruby question, how do we change namespace scope..
I remember you did something like

something < self
end

then you could define without saying def self.method

Rafael
@lastk
Jan 10 2017 16:10
class < self
Vasilis Spilka
@vasspilka
Jan 10 2017 16:11
oww yes, can I also use it to change the namespace in general
for example in order to avoid using Module::Class somewhere
becouse now that I think of it thats not what I wanted...
Rafael
@lastk
Jan 10 2017 16:13
Im not sure what you want to do
Vasilis Spilka
@vasspilka
Jan 10 2017 16:13
ok, let me get into more detail
I have the following
module Ranking
  class Ranking

    # bla bla bla stuff

    def get_score(user)
      Metrics::METRICS.reduce(0) do |points_total, (metric, points)|
        points_total + metric.calculate(user, @scope) * points
      end
    end
  end
end
module Ranking
  module Metrics
    METRICS = {
      Metrics::Messages       => 30,
      Metrics::Activity       => 40,
     ......
    }
  end
end
what I want to avoid is having to say Metrics::Xxxxxx in the METRICS constant
Vasilis Spilka
@vasspilka
Jan 10 2017 16:18
I know I could just add "Metrics::" and the eval, but... I don't know seems like there should be a way to avoid that meta
Rafael
@lastk
Jan 10 2017 16:19
it shouldnt be just: METRICS == { Messages => 30 } ?
Vasilis Spilka
@vasspilka
Jan 10 2017 16:20
Well it could, but then the Ranking class that is in a different naming scope would fail to find them
Hmmm... I guess I could restructure the order of the modules.. but.. owww I just want better namespace handling :'( :sob:
Vasilis Spilka
@vasspilka
Jan 10 2017 16:28
seems I can do this metric = Object.const_get("Metrics").const_get(metric.to_s), hurts my eyes
Vasilis Spilka
@vasspilka
Jan 10 2017 16:39
Hmmm... thats interesting... turns out I'm a whiny prick and it worked all along like you mention @lastk.
I must have been doing something different earlier or was just ignorant.. sry
Rafael
@lastk
Jan 10 2017 16:40
haaha, np :)
Andy Holland
@AMHOL
Jan 10 2017 18:02
@vasspilka pretty sure you can write that like: Metrics.const_get(metric)