For example, lets say I have an integer in a table that will hold -1,
0 or 1
I want these to map to :negative, :neutral, :positive
Or is there a better way to accomplish this. I want something similar
to an enum in C, so that from code i can can talk about things in
terms of abstract values, but that are stored in one column in the DB.
The other way I could do this would be to have another table mapping
the values, but this seems unnecessarily expensive.
For example, lets say I have an integer in a table that will hold -1,
0 or 1
I want these to map to :negative, :neutral, :positive
If you will just want to compare if an object has one of these values,
then instead of have lots of “object.value == :negative” and so forth,
you could define some predicates on your class:
def negative?
value == -1
end
etc. Then instead of using the symbols, you hide the values in these
predicates.
You can define similar setters:
def negative
value = -1
end
Depending on how you intend using the values, there’s a number of things
you can do to hide the implementation from users of the model.
Instead of:
VALUE_MAP.invert[read_attribute(:value)]
It’s a bit more efficient/elegant to use
VALUE_MAP.key[read_attribute(:value)]
If you have a larger map and/or efficiency is important you could also
use an array:
VALUE_MAP = [:negative,:neutral,:positive]
def value=(sym);self[:value]=VALUE_MAP.index(sym)-1;end
def value;VALUE_MAP[read_attribute(:value)+1];end
-j
Can anyone recommend a blogging engine that would suit my needs? My
website
would require multiple blogs, each “owned” by a different user, with
each
being separately customizable. Right now I’m making use of separate
WordPress installations for each user, but I would rather find a
solution
that could be integrated with the rest of the application instead of
switching back and forth between Rails and PHP (for WordPress).
I’ve looked at several blogging engines, but most of them seem geared
towards just one blog, or one blog with multiple authors and not
multiple
blogs with one author each. Is there anything out there which I could
use
and modify, or am I pretty much stuck writing my own engine to
accomplish
it? As far as the blog itself goes, we don’t need much more than the
ability for authors to write posts, others to post comments, and the
standard search/archive stuff.
Instead of:
VALUE_MAP.invert[read_attribute(:value)]
It’s a bit more efficient/elegant to use
VALUE_MAP.key[read_attribute(:value)]
If you have a larger map and/or efficiency is important you could also
use an array:
VALUE_MAP = [:negative,:neutral,:positive]
def value=(sym);self[:value]=VALUE_MAP.index(sym)-1;end
def value;VALUE_MAP[read_attribute(:value)+1];end
-j
Fjan,
I like that solution much better than mine, I kick myself for not having
not thought of the array solution instead of the map! You may have won
this round but the war isn’t over!
I get a NoMethodError when using key. I’m using Ruby 1.8.6
Sorry, you are right, it’s a new method in Ruby 1.9, I didn’t realize
that. A hash invert is kind of an expensive thing to do on every call
so then I’d rather do the invert once and store it in a constant, or
use the array version.
@ilan
The real Ruby-ists don’t worry about performance they tell me