Module: Theme

Defined in:
lib/theme.rb,
lib/theme/mab.rb,
lib/theme/assets.rb,
lib/theme/events.rb,
lib/theme/version.rb,
lib/theme/component.rb,
lib/theme/middleware.rb,
lib/theme/assets/render.rb,
lib/theme/assets/middleware.rb

Defined Under Namespace

Modules: Assets, Events Classes: Component, MabTemplate, Middleware, NoFileFound

Constant Summary collapse

IMAGE_TYPES =
%w(png gif jpg jpeg)
FONT_TYPES =
%w(eot woff ttf svg)
STATIC_TYPES =
%w(html js css map)
VIEW_TYPES =
%w(html slim haml erb md markdown mkd mab nokogiri)
PARTIAL_REGEX =
Regexp.new '([a-zA-Z_]+)$'
JS_ESCAPE =
{ '\\' => '\\\\', '</' => '<\/', "\r\n" => '\n', "\n" => '\n', "\r" => '\n', '"' => '\\"', "'" => "\\'" }
VERSION =
"0.2.3"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#configObject

Returns the value of attribute config.



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

def config
  @config
end

#reset_configObject

Returns the value of attribute reset_config.



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

def reset_config
  @reset_config
end

Class Method Details

.cacheObject



63
64
65
66
67
68
# File 'lib/theme.rb', line 63

def cache
  Thread.current[:_theme_cache] ||= OpenStruct.new({
    file: {},
    dom:  {}
  })
end

.configObject



38
39
40
# File 'lib/theme.rb', line 38

def config
  @config || reset_config!
end

.load_component_filesObject



70
71
72
73
74
# File 'lib/theme.rb', line 70

def load_component_files
  Dir.glob("#{Theme.config.component_path}/**/*.rb").each do |c|
    require c
  end
end

.load_file(path, c = {}, instance = self) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/theme.rb', line 76

def load_file path, c = {}, instance = self
  cache = Theme.cache.file.fetch(path) {
    template = false

    ext = path[/\.[^.]*$/][1..-1]

    if ext && File.file?(path)
      if STATIC_TYPES.include? ext
        template = Tilt::PlainTemplate.new nil, 1, outvar: '@_output', default_encoding: 'UTF-8' do |t|
          File.read(path)
        end
      elsif FONT_TYPES.include?(ext) || IMAGE_TYPES.include?(ext)
        template = File.read path
      else
        template = Tilt.new path, 1, outvar: '@_output'
      end
    else
      VIEW_TYPES.each do |type|
        f = "#{path}.#{type}"

        if File.file? f
          template = Tilt.new f, 1, outvar: '@_output'
          break
        end
      end
    end

    unless template
      raise Theme::NoFileFound,
        "Could't find file: #{path} with any of these extensions: #{VIEW_TYPES.join(', ')}."
    end

    template
  }

  if cache.respond_to?(:render, true)
    cache.render instance, c.to_h
  else
    cache.to_s
  end
end

.mab(&blk) ⇒ Object



4
5
6
# File 'lib/theme/mab.rb', line 4

def self.mab(&blk)
  Mab::Builder.new({}, self, &blk).to_s
end

.reset_config!Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/theme.rb', line 42

def reset_config!
  @config = OpenStruct.new({
    path:             './',
    component_path:   './theme/components',
    component_url:    '/components',
    components:       {},
    view_path:        './views',
    layout:           'app',
    layout_path:      './views/layouts',
    assets: OpenStruct.new({
      js: {},
      css: {}
    }),
    asset_url:        '/assets',
    asset_path:       './assets',
    asset_js_folder:  'js',
    asset_css_folder: 'css',
    assets_compiled:  false
  })
end

.setup(app = false) ⇒ Object



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

def setup app = false
  if app
    load_component_files
    app.settings[:render] ||= {}
    app.plugin Assets
    app.use Middleware
  else
    yield config
  end
end

Instance Method Details

#component(name, options = {}, &block) ⇒ Object Also known as: comp



119
120
121
122
123
124
125
# File 'lib/theme.rb', line 119

def component name, options = {}, &block
  if c = theme_components[name]
    c.set_locals options
  else
    raise "No component called '#{name}' loaded."
  end
end

#theme_componentsObject



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/theme.rb', line 128

def theme_components
  @_theme_components ||= begin
    components = {}

    Theme.config.components.each do |name, klass|
      component        = Object.const_get(klass).new self
      components[name] = component
      component.instance_variable_set :@id, name
    end

    components.each do |name, component|
      if listeners = component.class._for_listeners
        listeners.each do |id|
          if c = components[id.to_sym]
            c.add_listener component
          end
        end
      end
    end

    components
  end
end