Class: RepoManager::BaseView

Inherits:
Object
  • Object
show all
Defined in:
lib/repo_manager/views/base_view.rb

Overview

An abstract superclass for basic view/reporting functionality using templates

Direct Known Subclasses

AppView

Instance Method Summary collapse

Constructor Details

#initialize(items, configuration = {}) ⇒ BaseView

Returns a new instance of BaseView.



20
21
22
23
24
# File 'lib/repo_manager/views/base_view.rb', line 20

def initialize(items, configuration={})
  @configuration = configuration.deep_clone
  @items = items
  @template = File.expand_path('../templates/default.slim', __FILE__)
end

Instance Method Details

#configurationObject



26
27
28
# File 'lib/repo_manager/views/base_view.rb', line 26

def configuration
  @configuration
end

#dateObject



57
58
59
60
61
62
63
64
65
# File 'lib/repo_manager/views/base_view.rb', line 57

def date
  return @date if @date

  if configuration[:date]
    @date = Chronic.parse(configuration[:date])
    return @date if @date
  end
  @date = Date.today
end

#date=(value) ⇒ Object



67
68
69
# File 'lib/repo_manager/views/base_view.rb', line 67

def date=(value)
  @date = value
end

#get_bindingObject

ERB binding



72
73
74
# File 'lib/repo_manager/views/base_view.rb', line 72

def get_binding
  binding
end

#itemsObject



30
31
32
# File 'lib/repo_manager/views/base_view.rb', line 30

def items
  @items
end

#partial(filename) ⇒ String

render a partial

filename: unless absolute, it will be relative to the main template

Examples:

slim escapes HTML, use ‘==’


head
== render 'mystyle.css'

Returns:

  • (String)

    of non-escaped textual content



86
87
88
89
90
91
92
93
94
# File 'lib/repo_manager/views/base_view.rb', line 86

def partial(filename)
  filename = partial_path(filename)
  raise "unable to find partial file: #{filename}" unless File.exists?(filename)
  contents = File.open(filename, "rb") {|f| f.read}
  # TODO: detect template EOL and match it to the partial's EOL
  # force unix eol
  contents.gsub!(/\r\n/, "\n") if contents.match("\r\n")
  contents
end

#renderObject

TODO: render based on file ext



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/repo_manager/views/base_view.rb', line 97

def render
  raise "unable to find template file: #{template}" unless File.exists?(template)

  extension = File.extname(template)
  extension = extension.downcase if extension

  case extension
    when '.erb'
      contents = File.open(template, "r") {|f| f.read}
      ERB.new(contents, nil, '-').result(self.get_binding)
    when '.slim'
      Slim::Template.new(template, {:pretty => true}).render(self)
    else
      raise "unsupported template type based on file extension #{extension}"
  end
end

#templateObject



34
35
36
37
38
39
40
41
42
43
# File 'lib/repo_manager/views/base_view.rb', line 34

def template
  return @template if @template.nil? || Pathname.new(@template).absolute?

  # try relative to PWD
  fullpath = File.expand_path(File.join(FileUtils.pwd, @template))
  return fullpath if File.exists?(fullpath)

  # try built in template folder
  fullpath = File.expand_path(File.join('../templates', @template), __FILE__)
end

#template=(value) ⇒ Object



45
46
47
# File 'lib/repo_manager/views/base_view.rb', line 45

def template=(value)
  @template = value
end

#titleObject



49
50
51
# File 'lib/repo_manager/views/base_view.rb', line 49

def title
  @title || configuration[:title] || "Default Title"
end

#title=(value) ⇒ Object



53
54
55
# File 'lib/repo_manager/views/base_view.rb', line 53

def title=(value)
  @title = value
end