class Aduki::AttrFinder

Public Class Methods

attr_finder_text(finder, id, name, klass) click to toggle source
# File lib/aduki/attr_finder.rb, line 36
  def self.attr_finder_text finder, id, name, klass
    id_method = "#{name}_#{id}"
    <<EVAL
remove_method :#{id_method}  if method_defined?(:#{id_method})
remove_method :#{id_method}= if method_defined?(:#{id_method}=)
remove_method :#{name}       if method_defined?(:#{name})
remove_method :#{name}=      if method_defined?(:#{name}=)

attr_reader :#{id_method}

def #{id_method}= x
  @#{id_method}= x
  @#{name} = nil
end

def #{name}
  @#{name} ||= #{klass}.#{finder}(@#{id_method}) unless @#{id_method}.nil? || @#{id_method} == ''
  @#{name}
end

def #{name}= x
  @#{name} = x
  @#{id_method} = x ? x.#{id} : nil
end
EVAL
  end
attr_finders_text(finder, id, *args) click to toggle source
# File lib/aduki/attr_finder.rb, line 30
def self.attr_finders_text finder, id, *args
  hashify_args(args).map { |name, klass|
    attr_finder_text finder, id, name, klass
  }.join("\n")
end
camelize(str ;) click to toggle source
# File lib/aduki/attr_finder.rb, line 3
def self.camelize    str ; str.to_s.camelize    ; end
hashify_args(a) click to toggle source
# File lib/aduki/attr_finder.rb, line 18
def self.hashify_args a
  return a.first if a.first.is_a? Hash
  a.inject({ }) { |hash, arg|
    if arg.is_a?(Hash)
      hash.merge arg
    else
      hash[arg] = camelize arg.to_s
      hash
    end
  }
end
one2many_attr_finder_text(finder, id, name, options={ }) click to toggle source
# File lib/aduki/attr_finder.rb, line 63
  def self.one2many_attr_finder_text finder, id, name, options={ }
    singular = singularize name.to_s
    klass    = options[:class_name] || camelize(singular)
    id_method = "#{singular}_#{pluralize id}"
    <<EVAL
remove_method :#{id_method}  if method_defined?(:#{id_method})
remove_method :#{id_method}= if method_defined?(:#{id_method}=)
remove_method :#{name}       if method_defined?(:#{name})
remove_method :#{name}=      if method_defined?(:#{name}=)

attr_reader :#{id_method}

def #{id_method}= x
  @#{id_method} = x
  @#{name}      = nil
end

def #{name}
  @#{name} ||= #{klass}.#{finder} @#{id_method} unless @#{id_method}.nil?
  @#{name}
end

def #{name}= x
  @#{id_method} = x ? x.map(&:#{id}) : nil
  @#{name}      = x
end
EVAL
  end
pluralize(str ;) click to toggle source
# File lib/aduki/attr_finder.rb, line 5
def self.pluralize   str ; str.to_s.pluralize   ; end
singularize(str ;) click to toggle source
# File lib/aduki/attr_finder.rb, line 4
def self.singularize str ; str.to_s.singularize ; end