Class: Activr::Utils

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

Overview

Utilities methods

Class Method Summary collapse

Class Method Details

.class_for_kind(kind, suffix = nil) ⇒ Class

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns class for given kind

Parameters:

  • kind (String)

    Kind

  • suffix (String) (defaults to: nil)

    Suffix

Returns:

  • (Class)

    Class



31
32
33
34
# File 'lib/activr/utils.rb', line 31

def class_for_kind(kind, suffix = nil)
  str = suffix ? "#{kind}_#{suffix}" : kind
  str.camelize.constantize
end

.compiled_mustache_template(tpl) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Get a compiled mustache template

Parameters:

  • tpl (String)

    Template

Returns:

  • (String)

    Compiled template



47
48
49
50
51
52
53
54
55
# File 'lib/activr/utils.rb', line 47

def compiled_mustache_template(tpl)
  @compiled_mustache_templates ||= {}
  @compiled_mustache_templates[tpl] ||= begin
    view = Mustache.new
    view.raise_on_context_miss = true
    view.template = tpl # will compile and store template once for all
    view
  end
end

.kind_for_class(klass, suffix = nil) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns kind for given class

Parameters:

  • klass (Class)

    Class

  • suffix (String) (defaults to: nil)

    Expected suffix

Returns:

  • (String)

    Kind



15
16
17
18
19
20
21
22
# File 'lib/activr/utils.rb', line 15

def kind_for_class(klass, suffix = nil)
  class_name = klass.to_s.split('::').last.underscore
  if suffix && (match_data = class_name.match(/(.+)_#{suffix}$/))
    match_data[1]
  else
    class_name
  end
end

.render_mustache(text, bindings) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Render a mustache template

Parameters:

  • text (String)

    Template to render

  • bindings (Hash)

    Template bindings

Returns:

  • (String)

    Rendered template



64
65
66
67
68
# File 'lib/activr/utils.rb', line 64

def render_mustache(text, bindings)
  tpl = self.compiled_mustache_template(text)
  tpl.raise_on_context_miss = true
  tpl.render(bindings)
end