Class: Gumdrop::Context

Inherits:
Object
  • Object
show all
Includes:
ViewHelpers
Defined in:
lib/gumdrop/context.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ViewHelpers

#gumdrop_version, #hidden, #markdown, #textile

Constructor Details

#initialize(site) ⇒ Context

Returns a new instance of Context.



9
10
11
12
# File 'lib/gumdrop/context.rb', line 9

def initialize(site)
  @site= site
  # puts "NEW CONTEXT!!!"
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, value = nil) ⇒ Object



82
83
84
85
86
87
88
89
# File 'lib/gumdrop/context.rb', line 82

def method_missing(name, value=nil)
  @state=  Hash.new {|h,k| h[k]= nil } if @state.nil? 
  unless value.nil?
    @state[name.to_s]= value
  else
    @state[name.to_s]
  end
end

Instance Attribute Details

#stateObject

Returns the value of attribute state.



7
8
9
# File 'lib/gumdrop/context.rb', line 7

def state
  @state
end

Instance Method Details

#configObject

Access to settings as defined in the configure block



74
75
76
# File 'lib/gumdrop/context.rb', line 74

def config
  @site.config
end

#content_for(key, &block) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/gumdrop/context.rb', line 100

def content_for(key, &block)
  keyname= "_content_#{key}"
  if block_given?
    @state[keyname]= block
    nil
  else
    if @state.has_key?(keyname)
      @state[keyname].call
    else
      nil
    end
  end
end

#content_for?(key) ⇒ Boolean

Returns:

  • (Boolean)


114
115
116
117
# File 'lib/gumdrop/context.rb', line 114

def content_for?(key)
  keyname= "_content_#{key}"
  @state.has_key?(keyname)
end

#dataObject



65
66
67
# File 'lib/gumdrop/context.rb', line 65

def data
  @site.data
end

#get_templateObject



37
38
39
40
41
42
43
44
45
# File 'lib/gumdrop/context.rb', line 37

def get_template
  layout= @state['layout']
  @state['layout']= nil
  unless layout.nil?
    @site.layouts["#{layout}.template"]
  else
    nil
  end
end

#paramsObject



91
92
93
# File 'lib/gumdrop/context.rb', line 91

def params
  @content.params
end

#render(path, opts = {}) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/gumdrop/context.rb', line 52

def render(path, opts={})
  page= get_page path
  unless page.nil?
    #TODO: nested state for an inline rendered page?
    old_layout= @state['layout']
    content= page.render(nil, true, false, opts)
    old_layout= @state['layout']
    content
  else
    ""
  end
end

#reset_data(preset = {}) ⇒ Object



78
79
80
# File 'lib/gumdrop/context.rb', line 78

def reset_data(preset={})
  @state = preset
end

#set_content(content, locals) ⇒ Object



95
96
97
98
# File 'lib/gumdrop/context.rb', line 95

def set_content(content, locals)
  @content= content
  @state= @state.reverse_merge(content.params).merge(locals)
end

#siteObject



69
70
71
# File 'lib/gumdrop/context.rb', line 69

def site
  @site
end

#slugObject



33
34
35
# File 'lib/gumdrop/context.rb', line 33

def slug
  @state['current_slug']
end

#uri(path, opts = {}) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/gumdrop/context.rb', line 14

def uri(path, opts={})
  path= path[1..-1] if path.starts_with?('/') # and path != "/"
  uri_string= if !@site.config.relative_paths or force_absolute
    "/#{path}"
  else
    "#{'../'*@state['current_depth']}#{path}"
  end
  if opts[:fresh] and @site.node_tree.has_key?(path)
    uri_string += "?v=#{ @site.node_tree[path].mtime.to_i }"
  end
  uri_string = "/" if uri_string == ""
  uri_string
end

#url(path) ⇒ Object



28
29
30
31
# File 'lib/gumdrop/context.rb', line 28

def url(path)
  path= path[1..-1] if path.starts_with?('/')
  "#{@site.config.site_url}/#{path}"
end

#use_template(name) ⇒ Object Also known as: use_layout



47
48
49
# File 'lib/gumdrop/context.rb', line 47

def use_template(name)
  @state['layout']= name
end