Module: Theme

Defined in:
lib/theme.rb,
lib/theme/file.rb,
lib/theme/event.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, File Classes: Component, Event, 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.1.0"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#configObject

Returns the value of attribute config.



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

def config
  @config
end

#reset_configObject

Returns the value of attribute reset_config.



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

def reset_config
  @reset_config
end

Class Method Details

.cacheObject



61
62
63
64
65
66
# File 'lib/theme.rb', line 61

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

.configObject



36
37
38
# File 'lib/theme.rb', line 36

def config
  @config || reset_config!
end

.load_component_filesObject



68
69
70
71
72
# File 'lib/theme.rb', line 68

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



74
75
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
# File 'lib/theme.rb', line 74

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 defined? cache.render
    cache.render instance, c.to_h
  else
    cache.to_s
  end
end

.reset_config!Object



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

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



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

def setup app = false
  if app
    load_component_files
    app.plugin Assets
    app.use Middleware
  else
    yield config
  end
end

Instance Method Details

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



117
118
119
# File 'lib/theme.rb', line 117

def component name, options = {}, &block
  theme_components[name].set_locals options
end

#theme_componentsObject



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/theme.rb', line 122

def theme_components
    # Dir.glob("#{Theme.config.component_path}/**/*.rb").each do |c|
    #   load c
    # end
  req.env[:_theme_components] ||= begin
    components = {}

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

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

    components
  end
end