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, ThreadUtility
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.3.1"
Instance Attribute Summary collapse
-
#config ⇒ Object
Returns the value of attribute config.
-
#reset_config ⇒ Object
Returns the value of attribute reset_config.
Class Method Summary collapse
- .cache ⇒ Object
- .config ⇒ Object
- .load_component_files ⇒ Object
- .load_file(path, c = {}, instance = self) ⇒ Object
- .mab(&blk) ⇒ Object
- .reset_config! ⇒ Object
- .setup(app = false) ⇒ Object
Instance Method Summary collapse
Instance Attribute Details
#config ⇒ Object
Returns the value of attribute config.
22 23 24 |
# File 'lib/theme.rb', line 22 def config @config end |
#reset_config ⇒ Object
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
.cache ⇒ Object
66 67 68 69 70 71 |
# File 'lib/theme.rb', line 66 def cache Thread.current[:_theme_cache] ||= OpenStruct.new({ file: {}, dom: {} }) end |
.config ⇒ Object
40 41 42 |
# File 'lib/theme.rb', line 40 def config @config || reset_config! end |
.load_component_files ⇒ Object
73 74 75 76 77 |
# File 'lib/theme.rb', line 73 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
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 117 118 119 |
# File 'lib/theme.rb', line 79 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
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/theme.rb', line 44 def reset_config! @config = OpenStruct.new({ path: './', component_path: './theme/components', component_url: '/components', components: {}, view_path: './views', layout: 'app', layout_path: './views/layouts', use_component_middleware: true, 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 37 38 |
# File 'lib/theme.rb', line 27 def setup app = false if app load_component_files app.settings[:render] ||= {} app.plugin Assets if Theme.config.use_component_middleware app.use Middleware end else yield config end end |
Instance Method Details
#component(name, options = {}, &block) ⇒ Object Also known as: comp
122 123 124 125 126 127 128 |
# File 'lib/theme.rb', line 122 def component name, = {}, &block if c = theme_components[name] c.set_locals else raise "No component called '#{name}' loaded." end end |
#theme_components ⇒ Object
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/theme.rb', line 131 def theme_components req.env[:_theme_components] ||= begin components = {} Theme.config.components.each do |name, klass| comp_klass = Object.const_get(klass) component = comp_klass.new self components[name] = component # component.instance_variable_set :@id, name comp_klass.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 |