Class: Inkcite::View::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/inkcite/view/context.rb

Overview

Private class used to convey view attributes to the Erubis rendering engine without exposing all of the view’s attributes.

Instance Method Summary collapse

Constructor Details

#initialize(view) ⇒ Context

Returns a new instance of Context.



10
11
12
# File 'lib/inkcite/view/context.rb', line 10

def initialize view
  @view = view
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args, &block) ⇒ Object



44
45
46
47
48
49
50
51
52
53
# File 'lib/inkcite/view/context.rb', line 44

def method_missing(m, *args, &block)
  if m[-1] == QUESTION_MARK
    start_at = m[0] == UNDERSCORE ? 1 : 0
    symbol = m[start_at, m.length - (start_at + 1)].to_sym

    @view.version == symbol
  else
    super
  end
end

Instance Method Details

#helper(tag, open, close = nil) ⇒ Object

Defines a new helper via ERB, which allows designers to keep helper markup alongside the usage of it inside of partial. Helps keep code clean and prevents helper.tsv pollution for one-offs



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/inkcite/view/context.rb', line 17

def helper tag, open, close=nil

  tag = tag.to_sym

  # The config object holds the defined helpers
  config = @view.config

  # Warn the user if the helper is already defined.
  view.error("Helper '#{tag}' already defined", :open => open, :close => close) unless config[tag].nil?

  config[tag] = open.to_s
  config[:"/#{tag}"] = close.to_s

end

#once?(key) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
35
36
37
38
39
40
41
42
# File 'lib/inkcite/view/context.rb', line 32

def once? key

  # Initialize the 'once' data hash which maps
  @view.data[:once] ||= {}

  # True if this is the first time we've encountered this key.
  first_time = @view.data[:once][key].nil?
  @view.data[:once][key] = true if first_time

  first_time
end