Class: CompEx::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/compex/config.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfig

Returns a new instance of Config.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/compex/config.rb', line 25

def initialize
  @on_multiple_root = :wrap
  @multiple_root_wrap_element = "div"
  @on_non_element = :wrap
  self.cache_backend = Cache::MemoryBackend.new
  self.cache_prefix = "compex.cache"

  return unless defined?(::Rails)

  root_path = File.join(::Rails.root, "app", "views", "components")
  @template_search_path = root_path
  @style_search_path = root_path
  @js_search_path = root_path
end

Instance Attribute Details

#cache_backendObject

Returns the value of attribute cache_backend.



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

def cache_backend
  @cache_backend
end

#cache_prefixObject

Returns the value of attribute cache_prefix.



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

def cache_prefix
  @cache_prefix
end

#js_search_pathObject

Returns the value of attribute js_search_path.



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

def js_search_path
  @js_search_path
end

#multiple_root_wrap_elementObject

Returns the value of attribute multiple_root_wrap_element.



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

def multiple_root_wrap_element
  @multiple_root_wrap_element
end

#on_multiple_rootObject

Returns the value of attribute on_multiple_root.



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

def on_multiple_root
  @on_multiple_root
end

#on_non_elementObject

Returns the value of attribute on_non_element.



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

def on_non_element
  @on_non_element
end

#style_search_pathObject

Returns the value of attribute style_search_path.



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

def style_search_path
  @style_search_path
end

#template_search_pathObject

Returns the value of attribute template_search_path.



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

def template_search_path
  @template_search_path
end

Instance Method Details

#lookup_template(component, type) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/compex/config.rb', line 40

def lookup_template(component, type)
  @lookup_cache ||= {}
  lookup_key = [component, type]
  return @lookup_cache[lookup_key] if @lookup_cache.key? lookup_key

  dynamically_defined = component.descriptor.dynamically_defined?
  raise MissingTemplateError, "No template defined by anonymous class #{self.class} (through ::#{type})" if dynamically_defined && type == :html

  # Anonymous classes can't be looked up since we can't define its path
  if dynamically_defined
    @lookup_cache[lookup_key] = nil
    return nil
  end

  cname = component.name

  ret = case type
  when :html
    File.join(@template_search_path || "", "#{cname}.html.crb")
  when :style
    File.join(@style_search_path || "", "#{cname}.css")
  when :js
    File.join(@js_search_path || "", "#{cname}.js")
  else
    raise "Unexpected lookup type #{type.inspect}"
  end

  @lookup_cache[lookup_key] = ret
end