module Chas

Public Class Methods

add_method(klass, symbol) click to toggle source
# File lib/morph.rb, line 36
def self.add_method klass, symbol
  if adding_morph_method?(klass)
    @morph_methods[klass][symbol] = true
    is_writer = symbol.to_s =~ /=$/
    unless is_writer
      @morph_attributes[klass] << symbol
      @listeners.values.each { |l| l.call klass, symbol }
    end
  end
end
add_morph_attribute(klass, attribute) click to toggle source
# File lib/morph.rb, line 86
def self.add_morph_attribute klass, attribute
  start_adding_morph_method(klass)
  klass.send(:attr_accessor, attribute)
  finish_adding_morph_method(klass)
end
argument_provided?(args) click to toggle source
# File lib/morph.rb, line 105
def self.argument_provided? args
  args.size > 0
end
convert_to_morph_class_name(label) click to toggle source
# File lib/morph.rb, line 109
def self.convert_to_morph_class_name label
  name = label.to_s + ''
  name.tr!(',.:"\'/()\-*\\',' ')
  name.gsub!('%','percentage')
  name.strip!
  name.gsub!(/^(\d)/, '_\1')
  name.gsub!(/\s/,'_')
  name.squeeze!('_')
  name
end
convert_to_morph_method_name(label) click to toggle source
# File lib/morph.rb, line 120
def self.convert_to_morph_method_name label
  convert_to_morph_class_name label.to_s.downcase
end
finish_adding_morph_method(klass) click to toggle source
# File lib/morph.rb, line 82
def self.finish_adding_morph_method klass
  @adding_morph_method[klass] = false
end
morph_attributes(klass) click to toggle source
# File lib/morph.rb, line 55
def self.morph_attributes klass
  if klass.superclass.respond_to?(:morph_attributes)
    @morph_attributes[klass] + klass.superclass.morph_attributes
  else
    @morph_attributes[klass] + []
  end
end
morph_classes() click to toggle source
# File lib/morph.rb, line 32
def self.morph_classes
  @morph_attributes.keys
end
morph_method_missing(object, symbol, *args) click to toggle source
# File lib/morph.rb, line 92
def self.morph_method_missing object, symbol, *args
  attribute = symbol.to_s.chomp '='
  attribute = attribute.to_sym

  if Object.instance_methods.include?(attribute)
    raise "'#{attribute}' is an instance_method on Object, cannot create accessor methods for '#{attribute}'"
  elsif argument_provided? args
    base = object.class
    add_morph_attribute base, attribute
    object.send(symbol, *args)
  end
end
morph_methods(klass) click to toggle source
# File lib/morph.rb, line 63
def self.morph_methods klass
  methods = @morph_methods[klass].keys.sort

  if klass.superclass.respond_to?(:morph_attributes)
    methods += klass.superclass.morph_methods
  end
  methods
end
register_listener(listener) click to toggle source
# File lib/morph.rb, line 24
def self.register_listener listener
  @listeners[listener.object_id] = listener
end
remove_method(klass, symbol) click to toggle source
# File lib/morph.rb, line 47
def self.remove_method klass, symbol
  if @morph_methods[klass].has_key? symbol
    @morph_methods[klass].delete symbol
    is_writer = symbol.to_s =~ /=$/
    @morph_attributes[klass].delete(symbol) unless is_writer
  end
end
start_adding_morph_method(klass) click to toggle source
# File lib/morph.rb, line 78
def self.start_adding_morph_method klass
  @adding_morph_method[klass] = true
end
unregister_listener(listener) click to toggle source
# File lib/morph.rb, line 28
def self.unregister_listener listener
  @listeners.delete(listener.object_id) if @listeners.has_key?(listener.object_id)
end

Private Class Methods

adding_morph_method?(klass) click to toggle source
# File lib/morph.rb, line 73
def self.adding_morph_method? klass
  @adding_morph_method[klass]
end