Module: Plezi

Defined in:
lib/plezi.rb,
lib/plezi/api.rb,
lib/plezi/helpers.rb,
lib/plezi/version.rb,
lib/plezi/activation.rb,
lib/plezi/render/erb.rb,
lib/plezi/render/sass.rb,
lib/plezi/render/slim.rb,
lib/plezi/router/route.rb,
lib/plezi/router/router.rb,
lib/plezi/router/assets.rb,
lib/plezi/router/errors.rb,
lib/plezi/render/render.rb,
lib/plezi/router/adclient.rb,
lib/plezi/render/markdown.rb,
lib/plezi/websockets/redis.rb,
lib/plezi/render/has_cache.rb,
lib/plezi/controller/cookies.rb,
lib/plezi/controller/controller.rb,
lib/plezi/websockets/message_dispatch.rb,
lib/plezi/controller/controller_class.rb

Overview

Plezi is amazing. Read the README

Defined Under Namespace

Modules: AssetBaker, Base, Controller, Renderer

Constant Summary collapse

VERSION =
'0.14.2'.freeze

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.app_nameObject

Get / set the application name, which is used to create and identify the application's global pub/sub channel when using Redis.


12
13
14
# File 'lib/plezi/api.rb', line 12

def app_name
  @app_name
end

.assetsObject

Get / set the assets folder for the `:assets` route (the root for `Plezi.route '/assets/path'`, :assets).


10
11
12
# File 'lib/plezi/api.rb', line 10

def assets
  @assets
end

.templatesObject

Get / set the template folder for the Plezi::Controller#render function.


8
9
10
# File 'lib/plezi/api.rb', line 8

def templates
  @templates
end

Class Method Details

.appObject

Returns the Plezi Rack application


33
34
35
36
37
# File 'lib/plezi/api.rb', line 33

def app
  no_autostart
  puts "Running Plezi version: #{::Plezi::VERSION}"
  Plezi::Base::Router.call_method
end

.hash_proc_4symstrObject

Catches String/Symbol mixups. Add this to any Hash using Hash#default_proc=


48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/plezi/helpers.rb', line 48

def hash_proc_4symstr
  @hash_proc_4symstr ||= proc do |hash, key|
    case key
    when String
      tmp = key.to_sym
      hash.key?(tmp) ? hash[tmp] : nil
    when Symbol
      tmp = key.to_s
      hash.key?(tmp) ? hash[tmp] : nil
    end
  end
end

.new(app, *_args) ⇒ Object

Allows Plezi to be used as middleware.


28
29
30
# File 'lib/plezi/api.rb', line 28

def new(app, *_args)
  Plezi::Base::Router.new(app)
end

.no_autostartObject

Disables the autostart feature


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

def no_autostart
  @plezi_autostart = false
end

.route(path, controller) ⇒ Object

Will add a route to the Plezi application.

The order of route creation is the order of precedence.

path

the HTTP path for the route. Inline parameters and optional parameters are supported. i.e.

Plezi.route '/fixed/path', controller
Plezi.route '/fixed/path/:required_param/(:optional_param)', controller
Plezi.route '/(:format)', /^(html|json|xml)$/ # a rewrite route, usally on top.
Plezi.route '*', controller # catch all
controller

A Controller class or one of the included controllers: `false` for rewrite routes; `:client` for the Javascript Auto Dispatch client; `:assets` for a missing asset baker controller (bakes any unbaked assets into the public folder file).


52
53
54
55
# File 'lib/plezi/api.rb', line 52

def route(path, controller)
  plezi_initialize
  Plezi::Base::Router.route path, controller
end

.rubyfy(hash) ⇒ Object

Sanitizes hash data, attempting to “rubyfy” any Hash Strings to reversible Ruby objects. i.e:

{"go" => "<tag>home</tag>", "now" => "false", "count" => "8", "float" => "0.7", "empty" => ""}

Will become:

{go: "&lt;tag>home&lt;/tag&gt;", now: false, count: 8, float: "0.7", empty: nil}

As you may notice, float Strings aren't “rubyfied”. Any “rubyfied” object will revert back to the same String form using `to_s`.


23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/plezi/helpers.rb', line 23

def rubyfy(hash)
  case hash
  when Hash
    cpy = Hash.new(&hash_proc_4symstr)
    hash.each { |k, v| cpy[k.is_a?(String) ? k.to_sym : k] = rubyfy(v) }
    hash = cpy
  when String
    hash = if hash.empty?
             nil
           elsif hash.to_i.to_s == hash
             hash.to_i
           elsif hash == 'true'.freeze
             true
           elsif hash == 'false'.freeze
             false
           else
             ERB::Util.h try_utf8!(hash)
           end
  when Array
    hash = hash.map { |i| rubyfy(i) }
  end
  hash
end

.try_utf8!(string, encoding = ::Encoding::UTF_8) ⇒ Object

attempts to convert a string's encoding to UTF-8, but only when the string is a valid UTF-8 encoded string.


8
9
10
11
12
# File 'lib/plezi/helpers.rb', line 8

def try_utf8!(string, encoding = ::Encoding::UTF_8)
  return nil unless string
  string.force_encoding(::Encoding::ASCII_8BIT) unless string.force_encoding(encoding).valid_encoding?
  string
end

.url_for(controller, method_sym, params = {}) ⇒ Object

Will make a weak attempt to retrive a string representing a valid URL for the requested Controller's function. False positives (invalid URL strings) are possible (i.e., when requesting a URL of a method that doesn't exist).


59
60
61
# File 'lib/plezi/api.rb', line 59

def url_for(controller, method_sym, params = {})
  Plezi::Base::Router.url_for controller, method_sym, params
end