Class: Characterizable::Base

Inherits:
Object
  • Object
show all
Includes:
Blockenspiel::DSL
Defined in:
lib/characterizable/base.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass) ⇒ Base

Returns a new instance of Base.



4
5
6
# File 'lib/characterizable/base.rb', line 4

def initialize(klass)
  @klass = klass
end

Instance Attribute Details

#klassObject (readonly)

Returns the value of attribute klass.



3
4
5
# File 'lib/characterizable/base.rb', line 3

def klass
  @klass
end

Instance Method Details

#characteristicsObject



7
8
9
# File 'lib/characterizable/base.rb', line 7

def characteristics
  @_characteristics ||= BetterHash.new
end

#has(name, options = {}, &block) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/characterizable/base.rb', line 11

def has(name, options = {}, &block)
  raise CharacteristicAlreadyDefined, "The characteristic #{name} has already been defined on #{klass}!" if characteristics.has_key?(name)
  characteristics[name] = Characteristic.new(self, name, options, &block)
  begin
    # quacks like an activemodel
    klass.define_attribute_methods unless klass.respond_to?(:attribute_methods_generated?) and klass.attribute_methods_generated?
  rescue
    # for example, if a table doesn't exist... just ignore it
  end
  begin
    klass.module_eval(%{
      def #{name}_with_expire_snapshot=(new_#{name})
        expire_snapshot!
        self.#{name}_without_expire_snapshot = new_#{name}
      end
      alias_method_chain :#{name}=, :expire_snapshot
    }, __FILE__, __LINE__) #if klass.instance_methods.include?("#{name}=")
  rescue
  end
end