Module: Ferb::ClassMethods

Defined in:
lib/ferb.rb

Overview

Inner module holding the actual methods to be added to the class

Instance Method Summary collapse

Instance Method Details

#add_timestamp(path, time) ⇒ Object

Record file timestamps



174
175
176
# File 'lib/ferb.rb', line 174

def add_timestamp(path, time)
  self.file_timestamps[path.to_s] = time
end

#def_template(signature, args) ⇒ Object

Defines a template function as an instance method. Creates the method directly on the class.

Arguments:

<tt>signature</tt> - (string) The complete signature including parameters
<tt>body</tt> - (string) The body of the functionusing ERB syntax

Example:

def_template(“hello_world(message)”,“<%= message %>”)



215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/ferb.rb', line 215

def def_template(signature,args)
  if args.is_a?(String)
    internal_func = Ferb.construct_internal_function_def(signature,args)
    external_func = Ferb.construct_function_def(signature,args)
    module_eval(internal_func)
    module_eval(external_func)
  elsif args.is_a?(Hash)
    args = { :template_root => template_root}.merge(args)
    template = Ferb.load_template(args)
    add_timestamp(args[:full_path], args[:timestamp])
    internal_func = Ferb.construct_internal_function_def(signature,template)
    external_func = Ferb.construct_function_def(signature,template,args)
    module_eval(internal_func)
    module_eval(external_func)
  end
end

#file_timestampsObject

Hash of file timestamps



166
167
168
169
170
171
# File 'lib/ferb.rb', line 166

def file_timestamps
  unless defined?(@file_timestamps)
    @file_timestamps = {}
  end
  @file_timestamps
end

#should_reload?(path) ⇒ Boolean

Test file timestamps

Returns:

  • (Boolean)


179
180
181
182
183
184
185
186
187
# File 'lib/ferb.rb', line 179

def should_reload?(path)
  time = Pathname.new(path.to_s).mtime
  ts = self.file_timestamps[path]
  if ts and (ts >= time)
    return false;
  else
    return true
  end
end

#template_rootObject

Returns the root in the file system where ERB templates can be found



190
191
192
193
194
195
# File 'lib/ferb.rb', line 190

def template_root
  if !defined?(@template_root)
    @template_root = Pathname.new(File.expand_path(File.dirname(__FILE__)))
  end
  @template_root
end

#template_root=(location) ⇒ Object

Sets the root in the file system where ERB templates can be found



198
199
200
# File 'lib/ferb.rb', line 198

def template_root=(location)
  @template_root = Pathname.new(File.expand_path(location.to_s))
end