Module: Fold::FoldFactory

Included in:
Precompiler
Defined in:
lib/fold/fold_factory.rb

Defined Under Namespace

Modules: ClassMethods Classes: IndentationError

Constant Summary collapse

Indent =
2

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



6
7
8
# File 'lib/fold/fold_factory.rb', line 6

def self.included klass
  klass.extend ClassMethods
end

Instance Method Details

#count_soft_tabs(line) ⇒ Object

Raises:



47
48
49
50
51
# File 'lib/fold/fold_factory.rb', line 47

def count_soft_tabs line
  spaces= line.index(/([^ ]|$)/)
  raise IndentationError if spaces.odd?
  spaces / Indent
end

#detect_class(line) ⇒ Object



42
43
44
45
# File 'lib/fold/fold_factory.rb', line 42

def detect_class line
  return nil if line.blank?
  self.class.defined_folds.reverse.detect {|fold| fold::Regex and fold::Regex.match(line)}
end

#produce(line = '') ⇒ Object



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
37
38
39
40
# File 'lib/fold/fold_factory.rb', line 10

def produce line=''
  tabs = count_soft_tabs(line)
  line = line.sub(/^[ ]+/, '')
  attrs= {
    :text => line, 
    :tabs => tabs
  }

# This all happens because I need to access Precompiler attributes
# from within AbstractFold render methods
# Pretty much needs a nice refactor, but don't have the time.
# PS: thx ruby   
  fold = if klass= detect_class(line)
    klass.new attrs
  else
    AbstractFold.new attrs.merge(:tabs => -1)
  end
  
  instance_variables.each do |var|
    fold.instance_variable_set var, instance_variable_get(var)
  
    fold.metaclass.send :attr_accessor, var.gsub('@', '')
  end
  that = self
  fold.meta_def :method_missing do |meth, *args|
    return that.send(meth, *args) if that.respond_to?(meth)
    super
  end

  fold
end