Module: Chas

Defined in:
lib/morph.rb

Class Method Summary collapse

Class Method Details

.add_method(klass, symbol) ⇒ Object



38
39
40
41
42
43
44
45
46
47
# File 'lib/morph.rb', line 38

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



92
93
94
95
96
# File 'lib/morph.rb', line 92

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

Returns:

  • (Boolean)


113
114
115
# File 'lib/morph.rb', line 113

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

.convert_to_morph_class_name(label) ⇒ Object



117
118
119
120
121
122
123
124
125
126
# File 'lib/morph.rb', line 117

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



128
129
130
# File 'lib/morph.rb', line 128

def self.convert_to_morph_method_name label
  convert_to_morph_class_name label.to_s.downcase
end

.finish_adding_morph_method(klass) ⇒ Object



88
89
90
# File 'lib/morph.rb', line 88

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

.morph_attributes(klass) ⇒ Object



57
58
59
60
61
62
63
# File 'lib/morph.rb', line 57

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



34
35
36
# File 'lib/morph.rb', line 34

def self.morph_classes
  @morph_attributes.keys
end

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



98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/morph.rb', line 98

def self.morph_method_missing object, symbol, *args
  attribute = symbol.to_s.chomp '='
  if RUBY_VERSION >= "1.9"
    attribute = attribute.to_sym
  end

  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



65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/morph.rb', line 65

def self.morph_methods klass
  methods = if RUBY_VERSION >= "1.9"
    @morph_methods[klass].keys.sort
  else
    @morph_methods[klass].keys.map(&:to_s).sort
  end

  if klass.superclass.respond_to?(:morph_attributes)
    methods += klass.superclass.morph_methods
  end
  methods
end

.register_listener(listener) ⇒ Object



26
27
28
# File 'lib/morph.rb', line 26

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

.remove_method(klass, symbol) ⇒ Object



49
50
51
52
53
54
55
# File 'lib/morph.rb', line 49

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



84
85
86
# File 'lib/morph.rb', line 84

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

.unregister_listener(listener) ⇒ Object



30
31
32
# File 'lib/morph.rb', line 30

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