Class: Capnotify::Component
- Inherits:
-
Object
- Object
- Capnotify::Component
- Defined in:
- lib/capnotify/component.rb
Defined Under Namespace
Classes: TemplateUndefined
Instance Attribute Summary collapse
-
#builder ⇒ Object
readonly
a block that will configure this instance lazily.
-
#config ⇒ Object
Returns the value of attribute config.
-
#css_class ⇒ Object
the class(s) for this component (as a string).
-
#custom_css ⇒ Object
the class(s) for this component (as a string).
-
#header ⇒ Object
Returns the value of attribute header.
-
#name ⇒ Object
Returns the value of attribute name.
-
#renderers ⇒ Object
Returns the value of attribute renderers.
-
#template_path ⇒ Object
Returns the value of attribute template_path.
Instance Method Summary collapse
-
#build!(config) ⇒ Object
call @builder with self as a param if @builder is present ensure builder is nil then return self.
- #content ⇒ Object
-
#content=(new_content) ⇒ Object
assign the content as new_content.
-
#get_binding ⇒ Object
return the binding for this object this is needed when embedding ERB templates in each other.
-
#initialize(name, options = {}, &block) ⇒ Component
constructor
A new instance of Component.
-
#render_content(format) ⇒ Object
FIXME: this should probably leverage Procs for rendering of different types, maybe? that would give a lot of power to a developer who wants a custom format for a plugin (eg XML or JSON) Render the content in the given format using the right built-in template.
-
#render_for(renderers = {}) ⇒ Object
create renderers given a key for the format, provide the name of the ERB template to use to render relative to the template path.
-
#template_path_for(format) ⇒ Object
set the template path for this particular instance the template path is the path to the parent directory of a renderer ERB template.
Constructor Details
#initialize(name, options = {}, &block) ⇒ Component
Returns a new instance of Component.
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/capnotify/component.rb', line 19 def initialize(name, ={}, &block) @name = name.to_sym # default stuff @template_path = File.join( File.dirname(__FILE__), 'templates' ) @renderers = { :html => '_component.html.erb', :txt => '_component.txt.erb' } @header = [:header] @css_class = [:css_class] || 'section' @custom_css = [:custom_css] if block_given? @builder = block end end |
Instance Attribute Details
#builder ⇒ Object (readonly)
a block that will configure this instance lazily
12 13 14 |
# File 'lib/capnotify/component.rb', line 12 def builder @builder end |
#config ⇒ Object
Returns the value of attribute config.
16 17 18 |
# File 'lib/capnotify/component.rb', line 16 def config @config end |
#css_class ⇒ Object
the class(s) for this component (as a string)
9 10 11 |
# File 'lib/capnotify/component.rb', line 9 def css_class @css_class end |
#custom_css ⇒ Object
the class(s) for this component (as a string)
9 10 11 |
# File 'lib/capnotify/component.rb', line 9 def custom_css @custom_css end |
#header ⇒ Object
Returns the value of attribute header.
6 7 8 |
# File 'lib/capnotify/component.rb', line 6 def header @header end |
#name ⇒ Object
Returns the value of attribute name.
6 7 8 |
# File 'lib/capnotify/component.rb', line 6 def name @name end |
#renderers ⇒ Object
Returns the value of attribute renderers.
14 15 16 |
# File 'lib/capnotify/component.rb', line 14 def renderers @renderers end |
#template_path ⇒ Object
Returns the value of attribute template_path.
14 15 16 |
# File 'lib/capnotify/component.rb', line 14 def template_path @template_path end |
Instance Method Details
#build!(config) ⇒ Object
call @builder with self as a param if @builder is present ensure builder is nil then return self
83 84 85 86 87 88 89 90 |
# File 'lib/capnotify/component.rb', line 83 def build!(config) @builder.call(self) unless @builder.nil? @builder = nil @config = config return self end |
#content ⇒ Object
44 45 46 |
# File 'lib/capnotify/component.rb', line 44 def content @content end |
#content=(new_content) ⇒ Object
assign the content as new_content
40 41 42 |
# File 'lib/capnotify/component.rb', line 40 def content=(new_content) @content = new_content end |
#get_binding ⇒ Object
return the binding for this object this is needed when embedding ERB templates in each other
62 63 64 |
# File 'lib/capnotify/component.rb', line 62 def get_binding binding end |
#render_content(format) ⇒ Object
FIXME: this should probably leverage Procs for rendering of different types, maybe?
that would give a lot of power to a developer who wants a custom format for a plugin (eg XML or JSON)
Render the content in the given format using the right built-in template. Returns the content as a string. In the event that there is not a valid template, return an empty string.
52 53 54 55 56 57 58 |
# File 'lib/capnotify/component.rb', line 52 def render_content(format) begin ERB.new( File.open( template_path_for(format) ).read, nil, '%<>' ).result(self.get_binding) rescue TemplateUndefined '' end end |
#render_for(renderers = {}) ⇒ Object
create renderers given a key for the format, provide the name of the ERB template to use to render relative to the template path
76 77 78 |
# File 'lib/capnotify/component.rb', line 76 def render_for(renderers={}) @renderers = @renderers.merge(renderers) end |
#template_path_for(format) ⇒ Object
set the template path for this particular instance the template path is the path to the parent directory of a renderer ERB template
68 69 70 71 72 |
# File 'lib/capnotify/component.rb', line 68 def template_path_for(format) raise TemplateUndefined, "Template for #{ format } is missing!" if @renderers[format].nil? File.join( @template_path, @renderers[format] ) end |