Andrew
Andrew

Reputation: 43113

Ruby: Metaprogramming methods from a hash

I'm interested in creating an object from a hash that's generated by a Yaml file.

The Yaml file might look something like this:

foos:
  first:
    code: f
    name: The First Foo
    icon: path/to/file.png
  second:
    code: s
    name: The Second Foo
    icon: path/to/file.png
bars:
  dive:
    code: d
    name: Dive Bar
    location: 100 Main St.
  college:
    code: c
    name: College Bar
    location: 100 University Ave.

So, the yaml file basically defines a set of attributes which belong to categories. The categories have a name (foos, bars), and each attribute has at least a code and a name.

So, what I'd like to be able to do is create an "Attributes" model that turns the category names into methods which can call the items within that category as simple objects.

I'd like to be able to do something like:

Attributes = Attributes.new(...yaml_file...)
Attributes.foos #returns an array of foos
Attributes.foo(:f) #returns the foo with a code (f)
Attributes.foo(:s).name #returns "The Second Foo"

I'm kind of lost as to how to approach this.

I know how to setup my Attribute model to load the hash into an instance variable, but what I don't know how to do is to use the keys from that hash to create methods named for the keys, and to pass on the individual items from each category to create a hash of objects so I can chain these in dot syntax.

I'm aware that with the hash I could already do

attributes[:foos][:first][:icon] 

But I'd like to use this model-from-hash as a starting point where I can add other useful methods to the attributes model later. Plus I'm fairly new to ruby and I'd really like to learn how to do something like this just for the sake of doing it.

Thanks for any help!

Upvotes: 3

Views: 997

Answers (1)

Dean Povey
Dean Povey

Reputation: 9446

Assuming that your model is processed and stored in a hash of hashes, then you can use method_missing to implement your scheme. An extremely rough sketch of this is:

class Attributes {
  def init(*args) {
    @hash = # ....
  }
  def method_missing(symbol, *args)
    result = @hash[symbol]
    if (result && args.length) {
      return result[args[0]]
    }
    return result
  }
}

Upvotes: 1

Related Questions