Ruby: attr_accessor generated methods - how to ite

2019-03-15 23:15发布

问题:

I need a Class which has an semi-automatic 'to_s' method (to generate XML in fact). I would like to iterate through all the automatic methods set up in my 'attr_accessor' line:

class MyClass
    attr_accessor :id,:a,:b,:c
end

c=MyClass.new

So far I'm doing a basic:

c.methods - Object.methods

=> ["b", "b=", "c", "c=", "id=", "a", "a="]

I am facing a few challenges:

  1. 'id' may cause a slight headache - as Object already seems to have an 'id'.
  2. The 'c.methods' call above, returns Strings - I'm not getting any other meta-data ? (In Java 'method' is an object, where I could perform further reflection).
  3. I have one-to-many relationships I have to deal with ('c' is an array type of other object types).

This is what I'm trying to do: I want to design a simple Object which has a 'to_s' which will build up an XML fragment: for instance.

<id> 1 </id>
<a> Title </a>
<b> Stuff </b>
<c>
    <x-from-other-object>
    <x-from-other-object>
    ....
</c>

And then inherit my data-classes from that simple object: so that (hopefully) I get a mechansim to build up an entire XML doc.

I'm sure I'm re-inventing the wheel here as well...so other tried-and-tested approaches welcome.

回答1:

To get method objects from a string, you can use the methods method or instance_method (where method would be called on an object and instance_method on a class). The only interesting information it gives you is arity, though (as opposed to java where it'd also give you the types of the return value and the arguments, which of course isn't possible in ruby).

Your title suggests that you only want to iterate over methods created by attr_accessor, but your code will iterate over every method defined in your class, which could become a problem if you wanted to add additional non-accessor methods to your class.

To get rid of that problem and the problem with id, you could use your own wrapper around attr_accessor which stores which variables it created accessors for, like so:

module MyAccessor
  def my_attr_accessor *attrs
    @attrs ||= []
    @attrs << attrs
    attr_accessor *attrs
  end

  def attrs
    @attrs
  end
end

class MyClass
  extend MyAccessor
  my_attr_accessor :id,:a,:b,:c

  def to_s
    MyClass.attrs.each do |attr|
      do_something_with(attr, send(attr))
    end
  end
end

For problem 3 you can just do

if item.is_a? Array
  do_something
else
  do_something_else
end


回答2:

I use this technique to convert custom objects to JSON. May be the snippet below will be helpful since the question was for to_xml implementation.

There is a little magic here using self.included in a module. Here is a very nice article from 2006 about module having both instance and class methods http://blog.jayfields.com/2006/12/ruby-instance-and-class-methods-from.html

The module is designed to be included in any class to provide to_json functionality. It intercepts attr_accessor method rather than uses its own in order to require minimal changes for existing classes.

to_json implementation is based on this answer

module JSONable
  module ClassMethods
    attr_accessor :attributes

    def attr_accessor *attrs
      self.attributes = Array attrs
      super
    end
  end

  def self.included(base)
    base.extend(ClassMethods)
  end

  def as_json options = {}
    self.class.attributes.inject({}) do |hash, attribute|
      hash[attribute] = self.send(attribute)
      hash
    end
  end

  def to_json *a
    as_json.to_json *a
  end
end


class CustomClass
  include JSONable
  attr_accessor :b, :c 

  def initialize b: nil, c: nil
    self.b, self.c = b, c
  end
end

a = CustomClass.new(b: "q", c: 23)
puts JSON.pretty_generate a

{
  "b": "q",
  "c": 23
}