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

Access other attributes named like reserverd words in ruby (new, end, when) #16

Open
cutalion opened this issue Nov 10, 2017 · 4 comments

Comments

@cutalion
Copy link

cutalion commented Nov 10, 2017

This factory won't work.

Factory.define(:announcement) do |f|
  f.when { 10.hours.since..12.hours.since } # time range
  f.start_time { |when| when.begin.strftime('%H:%M') }
  f.end_time { |when| when.end.strftime('%H:%M') }
end

syntax error, unexpected keyword_when, expecting '|'

@GustavoCaso
Copy link
Member

Hi, @cutalion thanks for the issue ❤️

I was looking into and well I have to say I think is not a limitation of the library but more of a limitation of the language.

[].each do |when|
  puts when
end

Traceback (most recent call last):
syntax error, unexpected keyword_when, expecting '|'
[].each do |when|

I don't think is the good approach to try working against ruby 🙉

On another topic, I was trying to use ranges in the library and end up with a working example.

factories.define(:announcement) do |f|
  f.range { ROM::SQL::Postgres::Values::Range.new(3, 9, :'[]') } # range
  f.begin { |range| range.lower }
  f.end { |range| range.upper }
end

announcement = factories[:announcement]

expect(announcement.begin).to eq 3
expect(announcement.end).to eq 9

I found a little verbose having to type all that code to create a range type, might be interesting to have some method to create ranges or something a little more generic for the users? WDYT? @solnic

@cutalion
Copy link
Author

cutalion commented Mar 23, 2018

Hi @GustavoCaso, you're of course right about language limitations.
But I think that the issue is with the way of passing parameters, with API itself.

Consider the following example:

Factory.define(:announcement) do |f|
  f.when { 10.hours.since..12.hours.since } # time range
  f.start_time { |attrs| attrs[:when].begin.strftime('%H:%M') }
  f.end_time { |attrs| attrs[:when].end.strftime('%H:%M') }
end

It wouldn't have such limitations.

@cutalion
Copy link
Author

cutalion commented Mar 23, 2018

Btw, I had to deal with the similar issue in dry-validation.

      validate(start_lt_end: [:when]) do |when_|
        when_.begin < when_.end
      end

Here at least it's possible to pick a name for a variable. In rom-factory attribute is picked after the local variable name (nice trick).

I know this a very bad name for attribute in ruby, but it's the legacy database and it's first (two) places where it hit me. I've never had such issues in rails/activerecord/fabricator/etc

@GustavoCaso
Copy link
Member

@cutalion I will see what I can do ❤️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants