Module: Chas

Defined in:
lib/morph.rb

Class Method Summary collapse

Class Method Details

.add_method(klass, symbol) ⇒ Object



36
37
38
39
40
41
42
43
44
45
# 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) ⇒ Object



86
87
88
89
90
# 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) ⇒ Boolean



105
106
107
# File 'lib/morph.rb', line 105

def self.argument_provided? args
  args.size > 0
end

.convert_to_morph_class_name(label) ⇒ Object



109
110
111
112
113
114
115
116
117
118
# 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) ⇒ Object



120
121
122
# 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) ⇒ Object



82
83
84
# File 'lib/morph.rb', line 82

def self.finish_adding_morph_method klass
  @adding_morph_method[klass] = false
end

.morph_attributes(klass) ⇒ Object



55
56
57
58
59
60
61
# 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_classesObject



32
33
34
# File 'lib/morph.rb', line 32

def self.morph_classes
  @morph_attributes.keys
end

.morph_method_missing(object, symbol, *args) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
# 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) ⇒ Object



63
64
65
66
67
68
69
70
# 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) ⇒ Object



24
25
26
# File 'lib/morph.rb', line 24

def self.register_listener listener
  @listeners[listener.object_id] = listener
end

.remove_method(klass, symbol) ⇒ Object



47
48
49
50
51
52
53
# 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) ⇒ Object



78
79
80
# File 'lib/morph.rb', line 78

def self.start_adding_morph_method klass
  @adding_morph_method[klass] = true
end

.unregister_listener(listener) ⇒ Object



28
29
30
# File 'lib/morph.rb', line 28

def self.unregister_listener listener
  @listeners.delete(listener.object_id) if @listeners.has_key?(listener.object_id)
end