I am trying to make a sort of "enum". Here is my implementation:
# Format of input hash to AnEnum::initialize is :
# {
# Symbol => [Fixnum => String]
# }
# Example:
# {
# :active => [1 => "Active"]
# }
class AnEnum
##values = nil
def initialize(hash)
##values = hash
end
def values
##values
end
def [](symbol)
values[symbol][0] # return the number for the symbol. e.g. 1
end
def text(symbol)
values[symbol][1] # return the text for the symbol. e.g. "Active"
end
end
Example Usage:
class MyClass1
##status = AnEnum.new({
:open => [1, 'Active'],
:closed => [2, 'Closed']
})
def self.Status
##status
end
end
# test it (it works!)
MyClass1.Status[:open] # => 1
MyClass1.Status.text(:open) # => "Active"
This works, but I want to make it more "elegant" and "dynamic" :
Is it possible to define AnEnum in MyClass2 like this:
class MyClass2
define_enum "Status", :as => {
:open => [1, 'Active'],
:closed => [2, 'Closed']
}
end
So that these will work:
MyClass2.Status[:open] # => 1
MyClass2.Status.text(:open) # => "Active"
Thus, the ##status and self.Status defined in MyClass1 above are automatically included in the class by the "macro"-like call to define_enum.
define_enum is intended to be working like for example the before_filter call in Rails.
Is this possible??
That's great if you're tackling this problem for your own personal gain, however if it's because you actually need this functionality, there are tons of Ruby gems out there that already do this. If you need each "State" to exhibit different behavior, I have written a useful gem called classy_enum. Otherwise, here are a ton of others.
To answer your question though, yes it is definitely possible to add class methods or macros as you are describing. A high level overview would look something like:
module MyEnum
def define_enum(name, states)
... meta code here ...
end
end
Then in your class:
MyClass
extend MyEnum
define_enum :name, :state1 => [], :state2 => []
end
The "meta code" is where it gets tricky depending on what you are trying to do. If you are going to go this route, I would still recommend checking out how others have done it first. You've got a few things in your example that are a little odd, such as capitalized method names (def self.Status) and class variables ##my_var.
Look at this: http://code.dblock.org/ShowPost.aspx?id=184 (slight improvement over http://www.rubyfleebie.com/enumerations-and-ruby/). Lets you write the following.
class Gender
include Enum
Gender.define :MALE, "male"
Gender.define :FEMALE, "female"
end
And of course
Gender.all
Gender::MALE
Related
I have a Notifications module which have classes like 1)car 2)bike 3)Aeroplane. I have a serialized column in UserFeature model.And I have a module 'Notifications' which has list of 11 classes in it.
Notifications
1)car
2)bike
3)Aeroplane
The hash structure of the column notifications in UserFeature model must be
{:car => {:mirror => :true, :door => :true}
:bike => {:x=> :true, :x => :true}
:Aeroplane => {:p => :true, :q => :true}
}
I can access user_object.Notifications
But so as to access user_object.car and also user_object.mirror I need to write getter/setter methods { Defining getter/setter dynamically because I dont want to write getter/setter for every method and also I am unsure about the number of methods I have -> which in future may extend }
Notifications.constants.each do |notification_class|
class_methods = "Notifications::#{notification_class}".constantize.methods(false)
class_methods.each do |method|
method_name = method[0..-4].split('(')[0]
setter_getter_name = "#{notification_class.to_s.underscore}_#{method_name}"
define_method("#{setter_getter_name}=") do |value|
self.notifications = GlobalUtils.form_hash(self.notifications, "#{notification_class}".to_sym, "#{method_name}".to_sym)
self[:notifications]["#{notification_class}".to_sym][ "#{method_name}".to_sym] = value
end
define_method("#{setter_getter_name}") do
self.notifications.fetch("#{notification_class_name}".to_sym, {}).fetch("#{method_name}".to_sym)
end
end
end
But still when i try to access user_object.mirror,
undefined method for #<UserFeature000043645345>
What I am doing wrong?
I need to do this using getter/setter method only
An OpenStruct is a data structure, similar to a Hash, that allows the definition of arbitrary attributes with their accompanying values. This is accomplished by using Ruby’s metaprogramming to define methods on the class itself.
example:
require 'ostruct'
hash = { "country" => "Australia", :population => 20_000_000 }
data = OpenStruct.new(hash)
p data # -> <OpenStruct country="Australia" population=20000000>
Use Ruby OpenStruct class. It will fulfill your requirements without defining such bunch of code.
Edit1, example:
require 'ostruct'
class Aeroplane < OpenStruct; end
a = Aeroplane.new(:p => :true, :q => :true)
a.p # => true
I have only a vague idea on phrasing this, so question as needed:
I have a set of values I'm passing in my rails controller on a regular basis to a widget that differs slightly from page to page, from what I pass to it. This is is starting to get unwieldy for every controller, so I added a small class to help concatenate that process a bit (basic starting gist below).
#return dashcontroller hash from more succinct cues
module DashControl
class DashControl
attr_accessor :title, :instance, :actions
def initialize(title='default title', instance='default instance', actions={})
#title = title
#instance = instance
initialize_actions(actions)
end
def initialize_actions(actions)
actions.kind_of?(Hash) ? #actions = actions : initialize_tag(actions)
end
def initialize_tag(tag)
case tag
when :manage_default
#actions = {:statusactions => [],
:formactions => [ ['#tabaccount', 'addaccount'],
['#tabuser', 'addusers'],
['#tabadd','adddomain'] ],
:linkactions => [ [] ],
:filteractions => [ [] ] }
when :none
#actions = {}
#when
# #actions = {}
else
#actions = #actions
end
end
def dashcontroller
{:title => #title, :instance => #instance, :actions => #actions }
end
end
end
So basically I just need to pass an instance of this.dashcontroller and I get the hash I need with a lot less chaos in my controllers . The issue is with the #instance variable. I want to pass in the instance I'm using e.g. #book, #account, etc, and have it come out as #book, #account, etc. Instead, I get the contents of whatever I put into there as :instance => (contents of that instance). It doesn't seem right to me as before I was just using e.g. #account, and then using that, but looking at it might not make any sort of difference in the widget, as I juggle things and work on my code-fu.
Basically my question is how to push an instance variable through a class like this, and still have it accessibile as it went in without having to do any backflips and transformations on the other side. There is probably a better way, but this is what I'm working with at the moment.
edit: pseudo-code
DashControl::DashControl.new("Catchy Title", #book, :none).dashcontroller
#=> {:title => "Catchy Title", :instance => #book, :actions => {} }
I think I can work with it, like I said its more an issue of my understanding of how things flow than an actual bug or anything difficult. I'd like to not have to do more gymnastics on the other end with the instance stuff, though the contents are there and that is all I really need, I just need some input on thinking it through to be less of a mess. I really need to refine what I'm sending through this, or use this to further refine what I'm sending on is the bottom line lesson to take away right now.
edit:
I ended up tossing this, but it was a learning experience...I went back the widget and I know more than when I originally set up the widget, so I've been able to set that up to take only the instance variable and bootstrap what it needs without adding another class, cleaning up my controllers and handing a lot back to the widget where I suspect it should/could have been to start.
Based on your code and example, this fits:
# No need to put a class in a namespace of the same name, just make the module a class
# Also, if you inherit from a struct, it can save you a lot of typing. It defines the setters and getters for you.
class DashControl < Struct.new(:title, :instance, :actions)
# since it looks like you always access it the same way, create a class method
# which does this initialization and invocation
def self.for(*args)
new(*args).dashcontroller
end
def initialize(title='default title', instance='default instance', actions=:none)
# here, we can use our own defaults and normalization and pass the results up to the struct
super title, instance, normalize(actions)
end
# didn't make sense to call this initialize_tag, as it was initializing actions
# also there was already an initialize actions method which just checked for the case of a hash
# but then elsewhere you checked for other things. Better to just put it all in one method and return it
# (then you aren't setting it every time you want to ask it to calculate that value)
# also using guard clauses (the if statements that return early) instead of the case, as they are easier to understand
def normalize(actions)
return Hash.new if actions == :none
return actions unless actions == :manage_default
default_actions
end
# the value of default_actions is complicated and noisy, separate it out to its own method
# this prevents it from cluttering the code around it, and also allows us to access,
# and to do this without the side effects of setting values.
def default_actions
{ :statusactions => [],
:formactions => [ ['#tabaccount', 'addaccount'],
['#tabuser', 'addusers'],
['#tabadd','adddomain'] ],
:linkactions => [ [] ],
:filteractions => [ [] ] }
end
# use the getters instead of the ivars (I consider this a generally best practice -- and you could have
# done it before, since you declared the attr_accessor, even though I'm accessing it through the struct)
def dashcontroller
{:title => title, :instance => instance, :actions => actions }
end
end
DashControl.for # => {:title=>"default title", :instance=>"default instance", :actions=>{}}
DashControl.for('Catchy Title', '#book', :none) # => {:title=>"Catchy Title", :instance=>"#book", :actions=>{}}
DashControl.for('Catchy Title', '#book', :manage_default) # => {:title=>"Catchy Title", :instance=>"#book", :actions=>{:statusactions=>[], :formactions=>[["#tabaccount", "addaccount"], ["#tabuser", "addusers"], ["#tabadd", "adddomain"]], :linkactions=>[[]], :filteractions=>[[]]}}
DashControl.for('Catchy Title', '#book', a: 'b') # => {:title=>"Catchy Title", :instance=>"#book", :actions=>{:a=>"b"}}
DashControl.for('Catchy Title', '#book', 123) # => {:title=>"Catchy Title", :instance=>"#book", :actions=>123}
Here's some of my production code (I had to force line breaks):
task = Task.find_or_create_by_username_and_timestamp_and_des \
cription_and_driver_spec_and_driver_spec_origin(username,tim \
estamp,description,driver_spec,driver_spec_origin)
Yes, I'm trying to find or create a unique ActiveRecord::Base object. But in current form it's very ugly. Instead, I'd like to use something like this:
task = Task.SOME_METHOD :username => username, :timestamp => timestamp ...
I know about find_by_something key=>value, but it's not an option here. I need all values to be unique. Is there a method that'll do the same as find_or_create_by, but take a hash as an input? Or something else with similat semantics?
Rails 3.2 first introduced first_or_create to ActiveRecord. Not only does it have the requested functionality, but it also fits in the rest of the ActiveRecord relations:
Task.where(attributes).first_or_create
In Rails 3.0 and 3.1:
Task.where(attributes).first || Task.create(attributes)
In Rails 2.1 - 2.3:
Task.first(:conditions => attributes) || Task.create(attributes)
In the older versions, you could always write a method called find_or_create to encapsulate this if you'd like. Definitely done it myself in the past:
class Task
def self.find_or_create(attributes)
# add one of the implementations above
end
end
I also extend the #wuputah's method to take in an array of hashes, which is very useful when used inside db/seeds.rb
class ActiveRecord::Base
def self.find_or_create(attributes)
if attributes.is_a?(Array)
attributes.each do |attr|
self.find_or_create(attr)
end
else
self.first(:conditions => attributes) || self.create(attributes)
end
end
end
# Example
Country.find_or_create({:name => 'Aland Islands', :iso_code => 'AX'})
# take array of hashes
Country.find_or_create([
{:name => 'Aland Islands', :iso_code => 'AX'},
{:name => 'Albania', :iso_code => 'AL'},
{:name => 'Algeria', :iso_code => 'DZ'}
])
This is more of a style question, I'm wondering what other people do.
Let's say I have a field in my database called "status" for a blog post. And I want it to have several possible values, like "draft", "awaiting review", and "posted", just as an example.
Obviously we don't want to "hard code" in these magic values each time, that wouldn't be DRY.
So what I sometimes do is something like this:
class Post
STATUS = {
:draft => "draft",
:awaiting_review => "awaiting review",
:posted => "posted"
}
...
end
Then I can write code referring to it later as STATUS[:draft] or Post::STATUS[:draft] etc.
This works ok, but there are a few things I don't like about it.
If you have a typo and call something like STATUS[:something_that_does_not_exist] it won't throw an error, it just returns nil, and may end up setting this in the database, etc before you ever notice a bug
It doesn't look clean or ruby-ish to write stuff like if some_var == Post::STATUS[:draft] ...
I dunno, something tells me there is a better way, but just wanted to see what other people do. Thanks!
You can use Hash.new and give it a block argument which is called if a key is unknown.
class Post
STATUS = Hash.new{ |hash, key| raise( "Key #{ key } is unknown" )}.update(
:draft => "draft",
:awaiting_review => "awaiting review",
:posted => "posted" )
end
It's a bit messy but it works.
irb(main):007:0> Post::STATUS[ :draft ]
=> "draft"
irb(main):008:0> Post::STATUS[ :bogus ]
RuntimeError: Key bogus is unknown
from (irb):2
from (irb):8:in `call'
from (irb):8:in `default'
from (irb):8:in `[]'
from (irb):8
This is a common problem. Consider something like this:
class Post < ActiveRecord::Base
validates_inclusion_of :status, :in => [:draft, :awaiting_review, :posted]
def status
read_attribute(:status).to_sym
end
def status= (value)
write_attribute(:status, value.to_s)
end
end
You can use a third-party ActiveRecord plugin called symbolize to make this even easier:
class Post < ActiveRecord::Base
symbolize :status
end
You could use a class method to raise an exception on a missing key:
class Post
def self.status(key)
statuses = {
:draft => "draft",
:awaiting_review => "awaiting review",
:posted => "posted"
}
raise StatusError unless statuses.has_key?(key)
statuses[key]
end
end
class StatusError < StandardError; end
Potentially, you could also use this method to store the statuses as integers in the database by changing your strings to integers (in the hash), converting your column types, and adding a getter and a setter.
I do it like this:
class Post
DRAFT = "draft"
AWAITING_REPLY = "awaiting reply"
POSTED = "posted"
STATUSES = [DRAFT, AWAITING_REPLY, POSTED]
validates_inclusion_of :status, :in => STATUSES
...
end
This way you get errors if you misspell one. If I have multiple sets of constants, I might do something like DRAFT_STATUS to distinguish.
Take a look at the attribute_mapper gem.
There's a related article that shows how you can handle the problem declaratively, like this (borrowed from the article):
class Post < ActiveRecord::Base
include AttributeMapper
map_attribute :status, :to => {
:draft => 1,
:reviewed => 2,
:published => 3
}
end
...which looks rather stylish.
Even though this is an old post, for somebody stumbling across this, you can use the fetch method on Hash, which raises an error (when no default is passed) if the given key is not found.
STATUS = {
:draft => "draft",
:awaiting_review => "awaiting review",
:posted => "posted"
}
STATUS.fetch(:draft) #=> "draft"
STATUS.fetch(:invalid_key) #=> KeyError: key not found: invalid_key
I have an object now:
class Items
attr_accessor :item_id, :name, :description, :rating
def initialize(options = {})
options.each {
|k,v|
self.send( "#{k.to_s}=".intern, v)
}
end
end
I have it being assigned as individual objects into an array...
#result = []
some loop>>
#result << Items.new(options[:name] => 'name', options[:description] => 'blah')
end loop>>
But instead of assigning my singular object to an array... how could I make the object itself a collection?
Basically want to have the object in such a way so that I can define methods such as
def self.names
#items.each do |item|
item.name
end
end
I hope that makes sense, possibly I am overlooking some grand scheme that would make my life infinitely easier in 2 lines.
A few observations before I post an example of how to rework that.
Giving a class a plural name can lead to a lot of semantic issues when declaring new objects, as in this case you'd call Items.new, implying you're creating several items when in fact actually making one. Use the singular form for individual entities.
Be careful when calling arbitrary methods, as you'll throw an exception on any misses. Either check you can call them first, or rescue from the inevitable disaster where applicable.
One way to approach your problem is to make a custom collection class specifically for Item objects where it can give you the information you need on names and such. For example:
class Item
attr_accessor :item_id, :name, :description, :rating
def initialize(options = { })
options.each do |k,v|
method = :"#{k}="
# Check that the method call is valid before making it
if (respond_to?(method))
self.send(method, v)
else
# If not, produce a meaningful error
raise "Unknown attribute #{k}"
end
end
end
end
class ItemsCollection < Array
# This collection does everything an Array does, plus
# you can add utility methods like names.
def names
collect do |i|
i.name
end
end
end
# Example
# Create a custom collection
items = ItemsCollection.new
# Build a few basic examples
[
{
:item_id => 1,
:name => 'Fastball',
:description => 'Faster than a slowball',
:rating => 2
},
{
:item_id => 2,
:name => 'Jack of Nines',
:description => 'Hypothetical playing card',
:rating => 3
},
{
:item_id => 3,
:name => 'Ruby Book',
:description => 'A book made entirely of precious gems',
:rating => 1
}
].each do |example|
items << Item.new(example)
end
puts items.names.join(', ')
# => Fastball, Jack of Nines, Ruby Book
Do you know the Ruby key word yield?
I'm not quite sure what exactly you want to do. I have two interpretations of your intentions, so I give an example that makes two completely different things, one of them hopefully answering your question:
class Items
#items = []
class << self
attr_accessor :items
end
attr_accessor :name, :description
def self.each(&args)
#items.each(&args)
end
def initialize(name, description)
#name, #description = name, description
Items.items << self
end
def each(&block)
yield name
yield description
end
end
a = Items.new('mug', 'a big cup')
b = Items.new('cup', 'a small mug')
Items.each {|x| puts x.name}
puts
a.each {|x| puts x}
This outputs
mug
cup
mug
a big cup
Did you ask for something like Items.each or a.each or for something completely different?
Answering just the additional question you asked in your comment to tadman's solution: If you replace in tadman's code the definition of the method names in the class ItemsCollection by
def method_missing(symbol_s, *arguments)
symbol, s = symbol_s.to_s[0..-2], symbol_s.to_s[-1..-1]
if s == 's' and arguments.empty?
select do |i|
i.respond_to?(symbol) && i.instance_variables.include?("##{symbol}")
end.map {|i| i.send(symbol)}
else
super
end
end
For his example data you will get following outputs:
puts items.names.join(', ')
# => Fastball, Jack of Nines, Ruby Book
puts items.descriptions.join(', ')
# => Faster than a slowball, Hypothetical playing card, A book made entirely of precious gems
As I don't know about any way to check if a method name comes from an attribute or from another method (except you redefine attr_accessor, attr, etc in the class Module) I added some sanity checks: I test if the corresponding method and an instance variable of this name exist. As the class ItemsCollection does not enforce that only objects of class Item are added, I select only the elements fulfilling both checks. You can also remove the select and put the test into the map and return nil if the checks fail.
The key is the return value. If not 'return' statement is given, the result of the last statement is returned. You last statement returns a Hash.
Add 'return self' as the last line of initialize and you're golden.
Class Item
def initialize(options = {})
## Do all kinds of stuff.
return self
end
end