Class: Ducklink::Decorator

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

Constant Summary collapse

@@hosts =
{}

Class Method Summary collapse

Class Method Details

.configure(&block) ⇒ Object



14
15
16
# File 'lib/ducklink.rb', line 14

def configure(&block)
  Params.new.instance_exec &block
end

.decorate(url, context = {}) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/ducklink.rb', line 26

def decorate(url, context = {})
  params, block = hosts[URI.parse(url).host] rescue nil
  return url unless params
  
  params.set :url, url
  params.instance_exec context, &block
  
  params.template.scan(/\{\{([a-z_]+?)\}\}/i).inject(params.template) do |result, matches|
    params[matches.first] ? result.gsub("{{#{matches.first}}}", params[matches.first]) : result
  end
end

.host(domain, params, &block) ⇒ Object



22
23
24
# File 'lib/ducklink.rb', line 22

def host(domain, params, &block)
  @@hosts[domain] = [params, block]
end

.hostsObject



18
19
20
# File 'lib/ducklink.rb', line 18

def hosts
  @@hosts
end