module Pod4::Tweaking::ClassMethods

Public Instance Methods

set_custom_list(method) click to toggle source
# File lib/pod4/tweaking.rb, line 70
def set_custom_list(method)
  raise ArgumentError, "Bad custom interface method" unless interface.respond_to?(method)
  raise ArgumentError, "Method already exists on the model" \
    if (self.instance_methods - self.class.instance_methods).include?(method)

  define_class_method(method) do |*args|
    mname = "#{interface.class.name}.#{method}"
    rows  = interface.send(method, *args)

    raise Pod4Error, "#{mname} did not return an array" unless rows.is_a? Array
    raise Pod4Error, "#{mname} did not return an array of records" \
      unless rows.all?{|r| r.is_a?(Hash) || r.is_a?(Octothorpe) }

    raise Pod4Error, "#{mname} returned some records with no ID" \
      unless rows.all?{|r| r.has_key? interface.id_fld }

    rows.map do |r|
      rec = self.new r[interface.id_fld] 
      rec.map_to_model r 
      rec
    end
  end

end