Class: Cakery::CakeERBContextTwoClauseHelper

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

Overview

Allow things like r.my_secret << ‘./secret, this handles the ’<<‘ part and is given ’my_secret’ as a name

Instance Method Summary collapse

Constructor Details

#initialize(erb_context, name) ⇒ CakeERBContextTwoClauseHelper

Returns a new instance of CakeERBContextTwoClauseHelper.



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

def initialize erb_context, name
  @erb_context = erb_context
  @name = name
  @macros = []
end

Instance Method Details

#<(e) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/cakery.rb', line 53

def <(e)
  vname = ('@'+@name.to_s.gsub(/=/, "")).to_sym
  out = ""

  #Append all files or it's a macro class
  if e.class == String
    out = e

    #Run through each macro and run it in the reverse
    #order that we put it in so that
    #r.var << MyMacroA << MyMacroB << "./directory" will execute first MyMacroB and then MyMacroA
    while macro = @macros.pop
      out = macro.new.process(out)
    end

    @erb_context.instance_variable_set(vname, "") unless @erb_context.instance_variable_defined?(vname)
    v = @erb_context.instance_variable_get(vname)
    out = "\n" + out unless v == ""
    v += out
    @erb_context.instance_variable_set(vname, v)
  else
    raise "< operator needs a string, you passed #{e.inspect}"
  end
end

#<<(e) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/cakery.rb', line 78

def <<(e)
  vname = ('@'+@name.to_s.gsub(/=/, "")).to_sym
  out = ""

  #Append all files or it's a macro class
  if e.class == String
    fpaths = Dir[e].select{|e| File.file?(e)}
    fpaths.each do |fpath|
      out << File.read(fpath)
    end

    #Run through each macro and run it in the reverse
    #order that we put it in so that
    #r.var << MyMacroA << MyMacroB << "./directory" will execute first MyMacroB and then MyMacroA
    while macro = @macros.pop
      out = macro.new.process(out)
    end

    @erb_context.instance_variable_set(vname, "") unless @erb_context.instance_variable_defined?(vname)
    v = @erb_context.instance_variable_get(vname)
    v += out
    @erb_context.instance_variable_set(vname, v)
  else
    #Assume it's a macro, save it onto the stack
    @macros << e

    #Return self, recurse because there should be a string (or another macro)
    #to the right of this one
    return self
  end
end