Module: Rapid::Spec::Template

Defined in:
lib/rapid/spec/template.rb

Defined Under Namespace

Modules: ClassMethods

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.skeleton_classObject

Returns the value of attribute skeleton_class.



70
71
72
# File 'lib/rapid/spec/template.rb', line 70

def skeleton_class
  @skeleton_class
end

Class Method Details

.find(filename) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/rapid/spec/template.rb', line 72

def find filename
  if filename =~ /\/spec\//
    templates_path = File.join $`, 'templates'
  end
  
  raise "Templates path could not found be: #{filename.inspect}" unless templates_path
  
  if filename =~ /\/templates\/(.+)_spec.rb$/
    template_name = $1
    template_path = File.join templates_path, template_name
    
    unless File.exists?(template_path) && !File.directory?(template_path)
      template_name += ".rb"
      template_path = File.join templates_path, template_name
      
      unless File.exists?(template_path)
        raise "Couldn't figure out the template file location for: #{filename.inspect}"
      end
    end
    
  else
    raise "Not sure where the template file is for #{filename.inspect}"
  end
  
  return templates_path, template_path, template_name
end

.included(base) ⇒ Object



6
7
8
# File 'lib/rapid/spec/template.rb', line 6

def self.included base
  base.extend ClassMethods
end

Instance Method Details

#pull(content = {}) ⇒ Object



43
44
45
46
47
48
49
50
51
52
# File 'lib/rapid/spec/template.rb', line 43

def pull content = {}
  # render with the settings
  if content.is_a? Hash
    content = push content
  end
  
  @content = content
  @template = Rapid::Template::Base.new template_content
  @result = @template.pull @content
end

#push(settings_hash = nil) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/rapid/spec/template.rb', line 26

def push settings_hash = nil
  if respond_to? :build_skeleton
    @skeleton = build_skeleton
    
  elsif Rapid::Spec::Template.skeleton_class
    skeleton_class = Rapid::Spec::Template.skeleton_class
    @skeleton = skeleton_class.new
    
  else
    raise "please set define build_skeleton or set Rapid::Spec::Template.skeleton_class before running specs"
  end
  
  @skeleton.settings = settings_hash if settings_hash
  @template = Rapid::Template::Base.new template_content
  @result = @template.push @skeleton
end

#template_contentObject



22
23
24
# File 'lib/rapid/spec/template.rb', line 22

def template_content
  File.open(template_path) {|f| f.read } if template_path
end

#template_nameObject



18
19
20
# File 'lib/rapid/spec/template.rb', line 18

def template_name
  @template_name
end

#template_pathObject



14
15
16
# File 'lib/rapid/spec/template.rb', line 14

def template_path
  @template_path
end

#templates_pathObject



10
11
12
# File 'lib/rapid/spec/template.rb', line 10

def templates_path
  @templates_path
end