Class: Object

Inherits:
BasicObject
Defined in:
lib/clean-annotations/class_callbacks.rb,
lib/clean-annotations/method_attr.rb

Overview

Rails style callbacks

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.class_attribute(name, default = nil, &block) ⇒ Object

Defines class variable

Raises:

  • (ArgumentError)


3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/clean-annotations/class_attribute.rb', line 3

def Object.class_attribute name, default=nil, &block
  raise ArgumentError.new('name must be symbol') unless name.is_a?(Symbol)

  ivar = "@class_attribute_#{name}"

  instance_variable_set ivar, block || default

  define_singleton_method('%s=' % name) { |arg| send(name, arg) }
  define_singleton_method(name) do |arg=:_undefined, &block|
    # define and set if argument given
    if block || arg != :_undefined
      return instance_variable_set ivar, block || arg
    end

    # find value and return
    ancestors.each do |klass|
      if klass.instance_variable_defined?(ivar)
        value = klass.instance_variable_get ivar
        return value.is_a?(Proc) ? instance_exec(&value) : value
      end
    end
  end

  def class_attribute name
    self.class.send(name)
  end
end

.class_callback(name, context = nil, arg = nil) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/clean-annotations/class_callbacks.rb', line 8

def self.class_callback name, context=nil, arg=nil
  ivar = "@class_callbacks_#{name}"

  unless context
    define_singleton_method(name) do |method_name=nil, &block|
      ref = caller[0].split(':in ').first

      self.instance_variable_set(ivar, {}) unless instance_variable_defined?(ivar)
      self.instance_variable_get(ivar)[ref] = method_name || block
    end

  else
    list = context.respond_to?(:const_missing) && context.respond_to?(:ancestors) ? context.ancestors : context.class.ancestors
    list = list.slice 0, list.index(Object) if list.index(Object)

    list.reverse.each do |klass|
      if klass.instance_variable_defined?(ivar)
        mlist = klass.instance_variable_get(ivar).values
        mlist.each do |m|
          if m.is_a?(Symbol)
            context.send m
          else
            context.instance_exec arg, &m
          end
        end
      end
    end
  end
end

Instance Method Details

#class_callback(name, arg = nil) ⇒ Object



4
5
6
# File 'lib/clean-annotations/class_callbacks.rb', line 4

def class_callback name, arg=nil
  Object.class_callback name, self, arg
end

#method_attr(name = nil, &block) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/clean-annotations/method_attr.rb', line 35

def method_attr name=nil, &block
  if respond_to?(:const_missing) && respond_to?(:ancestors)
    if name.nil?
      return MethodAttributes.get(self) || {}
    end

    MethodAttributes.define self, name, &block
  else
    # instance
    base = MethodAttributes.get(self.class)
    name ? base[name] : base
  end
end