Module: Contextify::ClassMethods

Defined in:
lib/contextify/class_methods.rb

Instance Method Summary collapse

Instance Method Details

#contextify(name) ⇒ Object

Contextifies the class.

Parameters:

  • name (Symbol, String)

    The context name to assign to the class.



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
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/contextify/class_methods.rb', line 11

def contextify(name)
  name = name.to_s

  Contextify.contexts[name] = self

  meta_def(:context_name) { name }

  meta_def(:load_context_block) do |path|
    Contextify.load_block(name,path)
  end

  meta_def(:load_context) do |path,*args|
    pending = Contextify.load_blocks(path)

    context, block = pending.find do |name,block|
      Contextify.contexts[name].ancestors.include?(self)
    end

    if (context && block)
      obj = Contextify.contexts[name].new(*args)
      obj.instance_eval(&block)
      obj
    end
  end

  # define the top-level context wrappers
  Kernel.module_eval %{
    def #{name}(*args,&block)
      if (args.empty? && ::Contextify.is_pending?)
        ::Contextify.pending.blocks[#{name.dump}] = block
        return nil
      else
        new_context = #{self}.new(*args)
        new_context.instance_eval(&block) if block
        return new_context
      end
    end
  }
end