Module: Curly

Defined in:
lib/curly.rb,
lib/curly/railtie.rb,
lib/curly/presenter.rb,
lib/generators/curly/controller/controller_generator.rb

Overview

Curly is a simple view system. Each view consists of two parts, a template and a presenter. The template is a simple string that can contain references in the format ‘{refname}`, e.g.

Hello {{recipient}},
you owe us ${{amount}}.

The references will be converted into messages that are sent to the presenter, which is any Ruby object. Only public methods can be referenced. To continue the earlier example, here’s the matching presenter:

class BankPresenter
  def initialize(recipient, amount)
    @recipient, @amount = recipient, amount
  end

  def recipient
    @recipient.full_name
  end

  def amount
    "%.2f" % @amount
  end
end

See Curly::Presenter for more information on presenters.

Defined Under Namespace

Modules: Generators Classes: InvalidReference, Presenter, Railtie, TemplateHandler

Constant Summary collapse

VERSION =
"0.3.0"
REFERENCE_REGEX =
%r(\{\{(\w+)\}\})

Class Method Summary collapse

Class Method Details

.compile(template) ⇒ Object

Compiles a Curly template to Ruby code.

template - The template String that should be compiled.

Returns a String containing the Ruby code.



43
44
45
46
47
48
# File 'lib/curly.rb', line 43

def compile(template)
  source = template.inspect
  source.gsub!(REFERENCE_REGEX) { compile_reference($1) }

  source
end

.valid?(template, presenter_class) ⇒ Boolean

Whether the Curly template is valid. This includes whether all references are available on the presenter class.

template - The template String that should be validated. presenter_class - The presenter Class.

Returns true if the template is valid, false otherwise.

Returns:

  • (Boolean)


57
58
59
60
61
# File 'lib/curly.rb', line 57

def valid?(template, presenter_class)
  references = extract_references(template)
  methods = presenter_class.available_methods.map(&:to_s)
  references & methods == references
end