Class: Izzle::HasEasy::Configurator

Inherits:
Object
  • Object
show all
Defined in:
lib/has_easy/configurator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass, name, options) ⇒ Configurator

Returns a new instance of Configurator.



7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/has_easy/configurator.rb', line 7

def initialize(klass, name, options)
  @klass = klass
  @name = name
  @definitions = {}
  
  @aliases = []
  if options.has_key?(:aliases)
    @aliases = options[:aliases]
  elsif options.has_key?(:alias)
    @aliases = [options[:alias]]
  end
  @aliases = @aliases.collect{ |a| a.to_s }
end

Instance Attribute Details

#aliasesObject

Returns the value of attribute aliases.



5
6
7
# File 'lib/has_easy/configurator.rb', line 5

def aliases
  @aliases
end

#definitionsObject

Returns the value of attribute definitions.



5
6
7
# File 'lib/has_easy/configurator.rb', line 5

def definitions
  @definitions
end

Instance Method Details

#define(name, options = {}) ⇒ Object

Raises:

  • (ArgumentError)


21
22
23
24
25
# File 'lib/has_easy/configurator.rb', line 21

def define(name, options = {})
  name = Helpers.normalize(name)
  raise ArgumentError, "class #{@klass} has_easy('#{@name}') already defines '#{name}'" if @definitions.has_key?(name)
  @definitions[name] = Definition.new(name, options)
end

#do_metaprogramming_magic_aka_define_methodsObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/has_easy/configurator.rb', line 27

def do_metaprogramming_magic_aka_define_methods
  
  easy_accessors, object_accessors = [], []
  @definitions.values.each do |definition|
    
    easy_accessors << <<-end_eval
      def #{@name}_#{definition.name}=(value)
        set_has_easy_thing('#{@name}', '#{definition.name}', value, true)
      end
      def #{@name}_#{definition.name}
        get_has_easy_thing('#{@name}', '#{definition.name}', true)
      end
      def #{@name}_#{definition.name}?
        !!get_has_easy_thing('#{@name}', '#{definition.name}', true)
      end
    end_eval
    
    object_accessors << <<-end_eval
      def #{definition.name}=(value)
        proxy_association.owner.set_has_easy_thing('#{@name}', '#{definition.name}', value)
      end
      def #{definition.name}
        proxy_association.owner.get_has_easy_thing('#{@name}', '#{definition.name}')
      end
      def #{definition.name}?
        !!proxy_association.owner.get_has_easy_thing('#{@name}', '#{definition.name}')
      end
    end_eval
  end
  
  method_aliases = @aliases.inject([]) do |memo, alias_name|
    memo << "alias_method :#{alias_name}, :#{@name}"
    @definitions.values.each do |definition|
      memo << "alias_method :#{alias_name}_#{definition.name}=, :#{@name}_#{definition.name}="
      memo << "alias_method :#{alias_name}_#{definition.name},  :#{@name}_#{definition.name}"
      memo << "alias_method :#{alias_name}_#{definition.name}?, :#{@name}_#{definition.name}?"
    end
    memo
  end
  
  @klass.class_eval <<-end_eval
    # first define the has many relationship
    has_many :#{@name}, :class_name => 'HasEasyThing',
                        :as => :model,
                        :extend => AssocationExtension,
                        :dependent => :destroy do
      #{object_accessors.join("\n")}
    end
    
    # now define the easy accessors
    #{easy_accessors.join("\n")}
    
    # define the aliases
    #{method_aliases.join("\n")}
  end_eval
  
end