Module: ConstantTableSaver::NameClassMethods

Defined in:
lib/constant_table_saver.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_id, *arguments, &block) ⇒ Object



168
169
170
171
172
173
174
175
# File 'lib/constant_table_saver.rb', line 168

def method_missing(method_id, *arguments, &block)
  if @constant_record_methods.nil?
    define_named_record_methods
    send(method_id, *arguments, &block) # retry
  else
    super
  end
end

Instance Method Details

#define_named_record_methodsObject



154
155
156
157
158
159
160
161
162
# File 'lib/constant_table_saver.rb', line 154

def define_named_record_methods
  @constant_record_methods = [] # dummy so respond_to? & method_missing don't call us again if reading an attribute causes another method_missing
  @constant_record_methods = all.collect do |record|
    method_name = "#{constant_table_options[:name_prefix]}#{record[constant_table_options[:name]].downcase.gsub(/\W+/, '_')}#{constant_table_options[:name_suffix]}"
    next if method_name.blank?
    (class << self; self; end;).instance_eval { define_method(method_name) { record } }
    method_name.to_sym
  end.compact.uniq
end

#respond_to?(method_id, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


164
165
166
# File 'lib/constant_table_saver.rb', line 164

def respond_to?(method_id, include_private = false)
  super || (@constant_record_methods.nil? && @attribute_methods_generated && define_named_record_methods && super)
end