Module: DataMapper::Sweatshop::ClassAttributes

Defined in:
lib/dm-sweatshop/support/class_attributes.rb

Class Method Summary collapse

Class Method Details

.accessor(klass, *attributes) ⇒ Object


40
41
42
43
# File 'lib/dm-sweatshop/support/class_attributes.rb', line 40

def self.accessor(klass, *attributes)
  self.reader(klass, *attributes)
  self.writer(klass, *attributes)
end

.reader(klass, *attributes) ⇒ Object


4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/dm-sweatshop/support/class_attributes.rb', line 4

def self.reader(klass, *attributes)
  attributes.each do |attribute|
    klass.class_eval(<<-RUBY, __FILE__, __LINE__ + 1)
      unless defined? @@#{attribute}
        @@#{attribute} = nil
      end

      def self.#{attribute}
        @@#{attribute}
      end

      def #{attribute}
        @@#{attribute}
      end
    RUBY
  end
end

.writer(klass, *attributes) ⇒ Object


22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/dm-sweatshop/support/class_attributes.rb', line 22

def self.writer(klass, *attributes)
  attributes.each do |attribute|
    klass.class_eval(<<-RUBY, __FILE__, __LINE__ + 1)
      unless defined? @@#{attribute}
        @@#{attribute} = nil
      end

      def self.#{attribute}=(obj)
        @@#{attribute} = obj
      end

      def #{attribute}=(obj)
        @@#{attribute} = obj
      end
    RUBY
  end
end