Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set of enum #2

Open
sled opened this issue Apr 6, 2016 · 4 comments
Open

Set of enum #2

sled opened this issue Apr 6, 2016 · 4 comments
Labels

Comments

@sled
Copy link

sled commented Apr 6, 2016

A flood of questions:

  • How can we realise something like a set of a specific type, i.e
    attribute :selected_months, :set, class_name: Porro::Types::Enum.new(1..12)
  • How to set a default value for an attribute, e.g
    attribute :something, :string, default: 'a meaningful default'
  • Shouldn't we initialize an empty collection by default, e.g
    attribute :friends, :set, class_name: :string causes an_instance.friends to return nil wouldn't it make more sense to have something like an_instance.friends << "barry the dog"
@sled sled added the question label Apr 6, 2016
@sled sled assigned lwe Apr 6, 2016
@lwe
Copy link
Contributor

lwe commented Apr 6, 2016

@sled using the alternative DSL a lot of these are already possible, will give more input tomorrow.

@sled
Copy link
Author

sled commented Apr 6, 2016

thanks! the first one I already figured out how to do:

attribute :peak_months, Porro.set(Porro::Types::Enum.new(1..12))

2.3.0 :111 > a.peak_months = Set.new([55,1,66,12])
 => #<Set: {55, 1, 66, 12}>
2.3.0 :112 > a.peak_months
 => #<Set: {1, 12}>

However it still allows adding invalid values afterwards (due to the naked Set returned):

2.3.0 :113 > a.peak_months << 55
 => #<Set: {1, 12, 55}>
2.3.0 :114 > a.peak_months
 => #<Set: {1, 12, 55}>

Would be cool if we could find an easy way around this (immutable? cloning? overloading?), otherwise we could rely on validations.

@lwe
Copy link
Contributor

lwe commented Apr 7, 2016

Jup, was thinking about using a proxy or something, but as there is an unlimited number of methods which modify a Set/Array/Enumerable I decided if's not worth the trouble. Yes, we should handle this using validations, makes more sense.

Isn't there a Default.new wrapper class already?

@lwe
Copy link
Contributor

lwe commented Apr 7, 2016

and yes I agree we should probably provide an empty list by default, care to take a PR?

sled added a commit that referenced this issue Apr 7, 2016
@lwe lwe removed their assignment Sep 29, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants