Comments (4)
Although this seemed nice at first, on a second thought I think this is just syntactic sugar. And while that alone is not bad (syntactic sugar can be good), I think in this case there's little benefit in return. You could also write something like this:
class ProductImporter < ActiveImporter::Base
imports Product
column('Category', :category) { |v| normalize(v) }
column('Section', :section) { |v| normalize(v) }
...
private
def normalize(value)
value.strip.downcase
end
end
We could even make the following notation work too, if it's not already:
column 'Category', :category, -> (v) { normalize(v) }
column 'Section', :section, -> (v) { normalize(v) }
I'd even say this is more clear to a new user coming to the library. Your proposal would need people read that feature's documentation, or maybe infer what's happening from the names of things, and trust that what they're inferring is correct.
Would you consider this an acceptable alternative @lporras?
from active_importer.
BTW, I should mention that if what you're doing in some of these helpers is merely invoking a method to the actual value, I suspect this notation is also available:
column 'Category', :category, &:downcase
That would be equivalent to:
column('Category', :category) { |v| v.downcase }
It's not applicable to your case above because normalize
invokes two methods in sequence to the value of the column, but it might be worth knowing for some cases where you wouldn't even need to declare the helper function to begin with.
from active_importer.
@gnapse, thanks for your quick response, I'm going to test using this:
&:downcase
notation, and if it works then I think i could add the normalize method to the String class and leave it like this:
column 'Category', :category, &:normalize
and about the question related to syntactic sugar, I really like this way you proposed:
column 'Category', :category, -> (v) { normalize(v) }
column 'Section', :section, -> (v) { normalize(v) }
from active_importer.
@lporras I would strongly advise against this:
add the normalize method to the String
Monkey-patching is generally being avoided now more than before. It sounds like a good idea, but you can end up polluting the namespace of a class just for too little in return. I stress the idea that the gain in a slightly cleaner and shorter syntax is not enough reason to do it.
At the very least I'd urge you to use the newer Ruby language feature called refinements. It allows you to limit a monkey-patching to just inside the scope inside a specific module. Code outside that module is not affected.
from active_importer.
Related Issues (20)
- Gem required error - google_drive HOT 2
- Returning a summary message HOT 2
- Skip completely empty rows by default HOT 4
- Headers errors kill the process HOT 5
- Ability to specify the model to import to. HOT 8
- Blank 'last column' header causes fatal error. HOT 4
- Number in cell add decimal number HOT 1
- Hey, tell me how to use your gem to update the model HOT 3
- Model should not be save if not changed HOT 2
- Populate information without to specify all the column names HOT 2
- Import multiple .xls sheets HOT 9
- Problem when import xlsx -> Ole::Storage::FormatError - OLE2 signature is invalid HOT 3
- ActiveRecord::RecordInvalid HOT 1
- Import values without headers HOT 3
- Case insensitive headers HOT 3
- Transactional ActiveRecord::RecordInvalid rescue? HOT 5
- CSV delimiter HOT 3
- Import same model from excel with unknown sheets number HOT 3
- RuntimeError - Duplicate importer column HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from active_importer.