Class: Props::Deferment

Inherits:
Object
  • Object
show all
Defined in:
lib/props_template/extensions/deferment.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base, deferred = []) ⇒ Deferment

Returns a new instance of Deferment.



5
6
7
8
# File 'lib/props_template/extensions/deferment.rb', line 5

def initialize(base, deferred = [])
  @deferred = deferred
  @base = base
end

Instance Attribute Details

#deferredObject (readonly)

Returns the value of attribute deferred.



3
4
5
# File 'lib/props_template/extensions/deferment.rb', line 3

def deferred
  @deferred
end

Instance Method Details

#handle(options) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/props_template/extensions/deferment.rb', line 32

def handle(options)
  return if !options[:defer]

  type, rest = options[:defer]
  placeholder = rest[:placeholder]

  request_path = @base.context.controller.request.fullpath
  path = @base.traveled_path.join('.')
  uri = ::URI.parse(request_path)
  qry = ::URI.decode_www_form(uri.query || '')
    .reject{|x| x[0] == 'bzq' }
    .push(["bzq", path])

  uri.query = ::URI.encode_www_form(qry)

  @deferred.push(
    url: uri.to_s,
    path: path,
    type: type.to_s
  )

  placeholder
end

#refine_options(options, item = nil) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/props_template/extensions/deferment.rb', line 10

def refine_options(options, item = nil)
  return options if !options[:defer]
  pass_opts = options.clone

  type, rest = [*options[:defer]]
  rest ||= {
    placeholder: {}
  }

  if item
    type = Proc === type ? type.call(item) : type
  end

  if type
    pass_opts[:defer] = [type, rest]
  else
    pass_opts.delete(:defer)
  end

  pass_opts
end