These are chat archives for dry-rb/chat

9th
Dec 2015
Piotr Solnica
@solnic
Dec 09 2015 11:19 UTC
morning
things are happening in dry-data:
irb(main):010:0> Dry::Data['strict.string'].constrained(format: /^f/)['foo']
=> "foo"
irb(main):011:0> Dry::Data['strict.string'].constrained(format: /^f/)['boo']
Dry::Data::ConstraintError: "boo" violates constraints
:dancers:
it uses…dry-validation rule system :)
Andy Holland
@AMHOL
Dec 09 2015 13:13 UTC
Nice :)
@AMHOL ^
that plus defining types under constants
Andy Holland
@AMHOL
Dec 09 2015 13:20 UTC
That's pretty cool, also nice that both these libraries are enhancing each other :)
Piotr Solnica
@solnic
Dec 09 2015 13:22 UTC
@AMHOL part of the grand plan™
Andy Holland
@AMHOL
Dec 09 2015 13:22 UTC
:laughing:
Piotr Solnica
@solnic
Dec 09 2015 13:22 UTC
I suspect I’ll extract core stuff from validation to dry-predicates or sth
there are some really cool things that can be done with constrained types
ie you can build a data generator that would create random data based on constraints
Piotr Solnica
@solnic
Dec 09 2015 13:29 UTC
constants are cool too in this case, since you can do things like Types::Optional | Types::Strict::String
ie Bool is actually defined like this
Types::Strict::True | Types::Strict::False
gonna add a built-in Enum type too, I need it
Piotr Solnica
@solnic
Dec 09 2015 13:36 UTC
I will finally have a place for defining various app core concepts as types
wdyt about an interface like that for enum:
States = String.enum(%w(draft published))
Andy Holland
@AMHOL
Dec 09 2015 13:39 UTC
Not sure whether splat args would be nicer?
Piotr Solnica
@solnic
Dec 09 2015 13:39 UTC
oh, yeah!
ugh, lunch time, bbiab :)
Andy Holland
@AMHOL
Dec 09 2015 13:39 UTC
But looks good to me :)
OK TTYL :wave:
Hannes Nevalainen
@kwando
Dec 09 2015 14:18 UTC
would it be unreasonable of Dry::Data['hash'].schema to support nested hashes?
Piotr Solnica
@solnic
Dec 09 2015 14:19 UTC
@kwando it does support nested hashes
Hannes Nevalainen
@kwando
Dec 09 2015 14:19 UTC
hmm, let me try that again then..
Hannes Nevalainen
@kwando
Dec 09 2015 14:31 UTC
@solnic ah.. I was using it wrong
  schema = Dry::Data['hash'].schema(
          username: 'string',
          accounts: {
              skype: 'string'
          }
      )
^^ I thought that was going to work
  schema = Dry::Data['hash'].schema(
          username: 'string',
          accounts: Dry::Data['hash'].schema({
              skype: 'string'
          })
      )
that works as expected
Piotr Solnica
@solnic
Dec 09 2015 14:32 UTC
yes, that;s the correct usage
Hannes Nevalainen
@kwando
Dec 09 2015 14:33 UTC
cool =)
Piotr Solnica
@solnic
Dec 09 2015 15:26 UTC
statuses = Types::Strict::String.enum(“draft”, “published”, “archived”)
statuses[1] # => “published”
statuses[“published”] # => “published”
statuses[“#trollface”] # => ConstraintError
statuses[nil] # => ConstraintError
@AMHOL ^^ :dancer:
Andy Holland
@AMHOL
Dec 09 2015 15:26 UTC
Nice one :)
Piotr Solnica
@solnic
Dec 09 2015 15:26 UTC
as a bonus, values are frozen
Andy Holland
@AMHOL
Dec 09 2015 15:26 UTC
I saw that
Piotr Solnica
@solnic
Dec 09 2015 15:26 UTC
oh wait, values array is frozen, gotta freeze values themselves too
FREEZE ALL THE THINGS
Andy Holland
@AMHOL
Dec 09 2015 15:27 UTC
I was gonna ask that lol
only the first
Piotr Solnica
@solnic
Dec 09 2015 15:30 UTC
@AMHOL that part was ok, feeze mutates
but why call it freeze!, let’s surprise people
Andy Holland
@AMHOL
Dec 09 2015 15:30 UTC
lol
Piotr Solnica
@solnic
Dec 09 2015 15:31 UTC
gonna monkey patch freeze! and make it raise an error “sorry, due to the climate change we can’t freeze anything"
“try Object#melt instead"
Andy Holland
@AMHOL
Dec 09 2015 15:31 UTC
:p
Piotr Solnica
@solnic
Dec 09 2015 15:45 UTC
lol I forgot to add Symbol type :joy:
Andy Holland
@AMHOL
Dec 09 2015 15:46 UTC
lol
@AMHOL ^^ how does that sound?
Andy Holland
@AMHOL
Dec 09 2015 15:51 UTC
Sounds good, how come it only makes sense for strings?
Piotr Solnica
@solnic
Dec 09 2015 15:51 UTC
@AMHOL I dunno, stupid right, I’ll remove this sentence after I finish adding support for sum-types in enum :joy:
I’m in the middle of writing a spec for it
string.enum(tea | coffee)
sth like this will work
I think O_o
Andy Holland
@AMHOL
Dec 09 2015 15:54 UTC
That's cool
Piotr Solnica
@solnic
Dec 09 2015 15:54 UTC
I am not sure now
Andy Holland
@AMHOL
Dec 09 2015 15:54 UTC
Was thinking enums of value objects myself
Yeah, doesn't make sense does it
I think enums of symbols/classes would be useful
Piotr Solnica
@solnic
Dec 09 2015 15:59 UTC
I could add that
although we don’t have Class type either
Andy Holland
@AMHOL
Dec 09 2015 16:00 UTC
Think that could be useful?
Piotr Solnica
@solnic
Dec 09 2015 16:00 UTC
could be, I suppose
ok abandoning the idea of sumtype for enum
doesn’t feel like it makes sense, plain sum-type is enough
Piotr Solnica
@solnic
Dec 09 2015 16:10 UTC
@AMHOL I added symbol and class, let’s see if people will use those
Andy Holland
@AMHOL
Dec 09 2015 16:10 UTC
:+1:
Piotr Solnica
@solnic
Dec 09 2015 16:11 UTC
ok, gonna run specs in my app to see if I haven’t screwed up anything
seeing first F already :joy:
Andy Holland
@AMHOL
Dec 09 2015 16:11 UTC
Damn
Piotr Solnica
@solnic
Dec 09 2015 16:11 UTC
oh, it’s just that one unstable spec
victory, kind-of
I’ve got a spec that fails only on my machine
from time to time
like, every 6th time
it’s probably some awkward leftover in the db between the spec runs
since it’s a feature spec...
Piotr Solnica
@solnic
Dec 09 2015 16:22 UTC
dry-data 0.3.0 is out :)
@AMHOL I like the fact integrating with dry-validation took 10LOC
Piotr Solnica
@solnic
Dec 09 2015 16:37 UTC
@AMHOL you looked at fast_attributes right?
Andy Holland
@AMHOL
Dec 09 2015 16:42 UTC
I did
Took me ages to un-see it :joy:
Piotr Solnica
@solnic
Dec 09 2015 16:42 UTC
I wonder what kind of tricks they do
Andy Holland
@AMHOL
Dec 09 2015 17:46 UTC
@solnic what does the i18n gem actually do beyond a container for a hash of translations and the ability to resolve those with a hash of arguments to interpolate?
Piotr Solnica
@solnic
Dec 09 2015 18:26 UTC
@AMHOL it actually supports few backends, like storing stuff in the db etc
Andy Holland
@AMHOL
Dec 09 2015 18:27 UTC
Ahh OK
Piotr Solnica
@solnic
Dec 09 2015 18:27 UTC
it also does localization
it’s quite a lot of functionality
Andy Holland
@AMHOL
Dec 09 2015 18:29 UTC
Ahh OK, guess I haven't scratched the surface
Piotr Solnica
@solnic
Dec 09 2015 18:30 UTC
yeah, it’s a huge gem
Tim Riley
@timriley
Dec 09 2015 18:52 UTC
enums and constrained types :clap:
Here’s one attempt I found at an i18n replacement: https://github.com/ai/r18n
Piotr Solnica
@solnic
Dec 09 2015 20:59 UTC
@timriley :) it’s shaping up, finally
still a lot of work tbd though, but functionality is already pretty good
Piotr Solnica
@solnic
Dec 09 2015 21:30 UTC
btw I was wondering what kind of interface would be nice for a struct constructor that selects a struct class or its sub-class based on attributes
kind-of like STI in AR where a given field determines the type
Tim Riley
@timriley
Dec 09 2015 21:31 UTC
ahh, interesting