Module: Pod4::Tweaking::ClassMethods

Includes:
Metaxing
Defined in:
lib/pod4/tweaking.rb

Instance Method Summary collapse

Methods included from Metaxing

#define_class_method, #metaclass

Instance Method Details

#set_custom_list(method) ⇒ Object

Raises:

  • (ArgumentError)


70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# 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