module HasMeta::DynamicMethods::ClassMethods

Public Instance Methods

find_object_from(attribute) click to toggle source
# File lib/has_meta/dynamic_methods.rb, line 71
def find_object_from attribute
  begin
    attribute.to_s.classify.constantize
  rescue
    nil
  end
end
method_missing(method, *args, &block) click to toggle source
Calls superclass method
# File lib/has_meta/dynamic_methods.rb, line 46
def method_missing method, *args, &block
  # TODO: refactor this to not be as cluttery and dense
  attribute = self.meta_attributes.select { |x| method.match /(?<=^find_by_)#{x}(?=$|(?=_id$))/ }.pop
  if attribute
    object = find_object_from(attribute)
    if object and method =~ /_id$/
      conditions = {key: "#{attribute}_id", meta_model_type: self}.
        merge! MetaData.generate_value_hash(args.first)
        MetaData.where(conditions).map do |x|
          self.find_by_id(x.meta_model_id)
        end
    elsif !object
      conditions = {key: "#{attribute}", meta_model_type: self}.
        merge! MetaData.generate_value_hash(args.first)
      MetaData.where(conditions).map do |x|
        self.find_by_id(x.meta_model_id)
      end
    else
      super
    end
  else
    super
  end
end
respond_to?(method, include_private=false) click to toggle source
Calls superclass method
# File lib/has_meta/dynamic_methods.rb, line 37
def respond_to? method, include_private=false
  attribute = self.meta_attributes.select { |x| method.match(/(?<=^find_by_)#{x}(?=$|(?=_id$))/) }.pop
  if attribute
    find_object_from(attribute) ? !method.match(/_id$/).nil? : !method.match(/#{attribute}$/).nil?
  else
    super
  end
end