Module: Rubyfocus::IDRef::ClassMethods

Defined in:
lib/rubyfocus/includes/idref.rb

Instance Method Summary collapse

Instance Method Details

#idref(*names) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/rubyfocus/includes/idref.rb', line 4

def idref *names
  names.each do |name|
    name_id = "#{name}_id".to_sym
    attr_accessor name_id
    define_method(name) do
      return document && document.find(send(name_id))
    end

    define_method("#{name}=") do |o|
      if o.nil?
        self.send("#{name}_id=", nil)
      elsif o.respond_to?(:id)
        self.send("#{name}_id=", o.id)
      else
        raise ArgumentError, "#{self.class}##{name}= called with argument #{o}, which does not respond to :id."
      end
    end
  end
end