Module: BreezyTemplate::DefermentExtension

Included in:
BreezyTemplate
Defined in:
lib/breezy_template/deferment_extension.rb

Constant Summary collapse

ACTIVE_MODES =
[:auto, :manual].freeze

Instance Method Summary collapse

Instance Method Details

#_breezy_visit_current(path) ⇒ Object



19
20
21
22
23
24
# File 'lib/breezy_template/deferment_extension.rb', line 19

def _breezy_visit_current(path)
  uri = ::URI.parse(@request_path)
  qry = ::URI.decode_www_form(uri.query || '') << ["bzq", path.join('.')]
  uri.query = ::URI.encode_www_form(qry)
  "defers.push({url:'#{uri}'});"
end

#_deferment_auto?(options) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/breezy_template/deferment_extension.rb', line 34

def _deferment_auto?(options)
  _deferment_options(options) == :auto
end

#_deferment_options(options) ⇒ Object



30
31
32
# File 'lib/breezy_template/deferment_extension.rb', line 30

def _deferment_options(options)
  options[:defer]
end

#_deferment_options?(options) ⇒ Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/breezy_template/deferment_extension.rb', line 26

def _deferment_options?(options)
  options && !!options[:defer] && (@search_path.nil? || @search_path.size == 0)
end

#_extended_options?(value) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/breezy_template/deferment_extension.rb', line 42

def _extended_options?(value)
  _deferment_options?(value) || super
end

#_mapping_element(element, options) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/breezy_template/deferment_extension.rb', line 46

def _mapping_element(element, options)
  if _deferment_options?(options)
    if ::Proc === _deferment_options(options)
       value = _deferment_options(options).call(element)
       options = options.dup.merge({defer: value})
    end
  end

  if _deferment_options?(options)
    if _deferment_auto?(options)
      @js.push(_breezy_visit_current(@path))
    end

    if options.has_key? :key
      id_name = options[:key]
      id_val = element[id_name]
      ::Hash[id_name, id_val]
    else
      ::BreezyTemplate::Var.new('undefined')
    end
  else
    super
  end
end

#_set_request_url_once(request_path) ⇒ Object



38
39
40
# File 'lib/breezy_template/deferment_extension.rb', line 38

def _set_request_url_once(request_path)
  @request_path ||= request_path
end

#set!(key, value = BLANK, *args) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/breezy_template/deferment_extension.rb', line 7

def set!(key, value = BLANK, *args)
  options = args[0]
  if ::Kernel.block_given? && _deferment_options?(options)
    if _deferment_auto?(options)
      @js.push(_breezy_visit_current(@path))
    end
    return _set_value key, ::BreezyTemplate::Var.new('undefined')
  else
    super
  end
end