Class: Hobby::Pages

Inherits:
Object
  • Object
show all
Includes:
Hobby
Defined in:
lib/hobby/pages.rb

Defined Under Namespace

Classes: Directory

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(directory) ⇒ Pages

Returns a new instance of Pages.



15
16
17
# File 'lib/hobby/pages.rb', line 15

def initialize directory
  @directory = Directory.new directory
end

Instance Attribute Details

#directoryObject (readonly)

Returns the value of attribute directory.



14
15
16
# File 'lib/hobby/pages.rb', line 14

def directory
  @directory
end

#nameObject (readonly)

Returns the value of attribute name.



14
15
16
# File 'lib/hobby/pages.rb', line 14

def name
  @name
end

Instance Method Details

#cssObject



64
65
66
67
68
69
70
71
# File 'lib/hobby/pages.rb', line 64

def css
  style_file = "#{directory}/css/pages/#{name}.sass"
  if File.exist? style_file
    sass_string = IO.read style_file
    load_path = "#{directory}/css/pages/#{name}"
    Sass::Engine.new(sass_string, load_paths: [load_path]).render
  end
end

#css_tagObject



56
57
58
59
60
61
62
# File 'lib/hobby/pages.rb', line 56

def css_tag
  @css_tag ||= if css
                 "<style>#{css}</style>"
               else
                 ''
               end
end

#js_at_name(name) ⇒ Object



81
82
83
# File 'lib/hobby/pages.rb', line 81

def js_at_name name
  directory.sprockets["pages/#{name}.js"]
end

#js_tagObject



73
74
75
76
77
78
79
# File 'lib/hobby/pages.rb', line 73

def js_tag
  @js_tag ||= if js_at_name(name)
                "<script src='#{env['SCRIPT_NAME']}/#{name}.js'></script>"
              else
                ''
              end
end

#not_foundObject



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/hobby/pages.rb', line 44

def not_found
  response.status = 404

  name, @name = @name, '404'

  if page = @directory[@name]
    page.render self
  else
    "404. The page named '#{name}' was not found."
  end
end