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/render/sassc.rb,
lib/plezi/router/route.rb,
lib/plezi/render/render.rb,
lib/plezi/router/assets.rb,
lib/plezi/router/errors.rb,
lib/plezi/router/router.rb,
lib/plezi/render/markdown.rb,
lib/plezi/router/adclient.rb,
lib/plezi/render/has_cache.rb,
lib/plezi/controller/bridge.rb,
lib/plezi/controller/cookies.rb,
lib/plezi/controller/controller.rb,
lib/plezi/controller/identification.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.16.4'.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.



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

def app_name
  @app_name
end

.assetsObject

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



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

def assets
  @assets
end

.templatesObject

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



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

def templates
  @templates
end

Class Method Details

.appObject

Returns the Plezi Rack application



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

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.



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

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

.no_autostartObject

Disables the autostart feature



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

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).



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

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).



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

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