Module: Theme

Defined in:
lib/theme.rb,
lib/theme/file.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, 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.3"

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



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

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

.configObject



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

def config
  @config || reset_config!
end

.load_component_filesObject



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

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



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

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



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

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

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



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

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

#theme_componentsObject



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

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