Class: Massimo::Site

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = nil, &block) ⇒ Site

Creates a new Site, passing the given options to a new configuration. If a block is given, it is evaluated in the scope of the new Site.



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/massimo/site.rb', line 12

def initialize(options = nil, &block)
  @original_options        ||= options
  @config                    = Config.new(options)
  @resources                 = [ Massimo::Page, Massimo::Javascript, Massimo::Stylesheet, Massimo::View ]
  @template_scope_blocks     = []
  @template_scope_extensions = []
  Massimo.site               = self
  
  Massimo::Reloader.reload(:config) do
    instance_eval File.read(config.config_path) if File.exist?(config.config_path)
    instance_eval(&block) if block_given?
  end
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



7
8
9
# File 'lib/massimo/site.rb', line 7

def config
  @config
end

#resourcesObject (readonly)

Returns the value of attribute resources.



8
9
10
# File 'lib/massimo/site.rb', line 8

def resources
  @resources
end

Instance Method Details

#helpers(*extensions, &block) ⇒ Object

Adds custom helpers to the ‘#template_scope`. Takes either an array of Modules that will extend the scope, or a block of method definitions that will be added to the scope.



58
59
60
61
# File 'lib/massimo/site.rb', line 58

def helpers(*extensions, &block)
  @template_scope_blocks     << block if block_given?
  @template_scope_extensions += extensions
end

#processObject

Processes all the current resources.



64
65
66
67
68
69
70
# File 'lib/massimo/site.rb', line 64

def process
  @template_scope = nil
  reload_libs
  resources.select(&:processable?).each do |resource|
    resource.all.each(&:process)
  end
end

#reload(&block) ⇒ Object

Sets up the Site from scratch again. Also Reloads the config file again.



27
28
29
# File 'lib/massimo/site.rb', line 27

def reload(&block)
  initialize @original_options, &block
end

#resource(name_or_class, &block) ⇒ Object

Adds a new, custom resource to the Site. If a Class constant is given, it is added to directly to the ‘#resources`. If a Symbol or String is given, a new Class (inheriting from Massimo::Page) is created using that name with the given block used as the Class body.



35
36
37
38
39
40
41
42
43
# File 'lib/massimo/site.rb', line 35

def resource(name_or_class, &block)
  resource = case name_or_class
    when Class
      name_or_class
    else
      Object.const_set name_or_class.to_s.classify, Class.new(Massimo::Page, &block)
    end
  resources << resource
end

#template_scopeObject

The scope used for templating. It includes helpers from Massimo::Helpers along with any custom helpers.



47
48
49
50
51
52
53
# File 'lib/massimo/site.rb', line 47

def template_scope
  Object.new.extend(Massimo::Helpers).tap do |scope|
    add_template_scope_blocks(scope)
    add_template_scope_extensions(scope)
    add_template_scope_helpers(scope)
  end
end