Class: Card::Format
- Inherits:
-
Object
- Object
- Card::Format
- Extended by:
- Nest::ClassMethods, Registration
- Includes:
- Env::Location, Content, Error, Names, Nest, Permission, Render
- Defined in:
- lib/card/format.rb,
lib/card/format/nest.rb,
lib/card/format/error.rb,
lib/card/format/names.rb,
lib/card/format/render.rb,
lib/card/format/content.rb,
lib/card/format/nest/main.rb,
lib/card/format/nest/view.rb,
lib/card/format/nest/fetch.rb,
lib/card/format/permission.rb,
lib/card/format/registration.rb,
lib/card/format/nest/subformat.rb,
mod/core/format/data_format.rb,
mod/core/format/html_format.rb,
mod/core/format/text_format.rb,
mod/basic_formats/format/js_format.rb,
mod/email/format/email_html_format.rb,
mod/email/format/email_text_format.rb,
mod/basic_formats/format/css_format.rb,
mod/basic_formats/format/csv_format.rb,
mod/basic_formats/format/rss_format.rb,
mod/basic_formats/format/xml_format.rb,
mod/basic_formats/format/file_format.rb,
mod/basic_formats/format/json_format.rb
Overview
The Format class is a key strut in the MoFoS (Model-Format-Set) architecture.
The primary means of transacting with the card Model (cards across time) is the event. The primary means for displaying card content (cards across space) is the view. Format objects manage card views.
Here is a very simple view that just displays the card's id:
view(:simple_content) { card.raw_content }
But suppose you would like this view to appear differently in different output formats. You might need certain characters escaped in some formats (csv, html, etc) but not others. You might like to make use of the aesthetic or structural benefits certain formats allow.
To this end we have format classes. HtmlFormat, JsonFormat, XmlFormat, etc, each are descendants of Format.
For information on how Formats intersect with Sets, see Set::Format
Direct Known Subclasses
CssFormat, DataFormat, FileFormat, HtmlFormat, JsFormat, TextFormat
Defined Under Namespace
Modules: Content, Error, Names, Nest, Permission, Registration, Render Classes: CssFormat, CsvFormat, DataFormat, EmailHtmlFormat, EmailTextFormat, FileFormat, HtmlFormat, JsFormat, JsonFormat, RssFormat, TextFormat, XmlFormat
Constant Summary
Constants included from Render
Constants included from Nest::View
Instance Attribute Summary collapse
-
#card ⇒ Object
readonly
Returns the value of attribute card.
-
#error_status ⇒ Object
Returns the value of attribute error_status.
-
#form ⇒ Object
Returns the value of attribute form.
-
#main_opts ⇒ Object
readonly
Returns the value of attribute main_opts.
-
#nest_opts ⇒ Object
Returns the value of attribute nest_opts.
-
#parent ⇒ Object
readonly
Returns the value of attribute parent.
-
#root ⇒ Object
readonly
Returns the value of attribute root.
Instance Method Summary collapse
- #controller ⇒ Object
-
#focal? ⇒ Boolean
meaning the current card is the requested card.
- #include_set_format_modules ⇒ Object
-
#initialize(card, opts = {}) ⇒ Format
constructor
A new instance of Format.
- #main? ⇒ Boolean
- #method_missing(method, *opts, &proc) ⇒ Object
- #page(view, slot_opts) ⇒ Object
- #params ⇒ Object
- #pass_method_to_template_object(method, opts, proc) ⇒ Object
- #respond_to_missing?(method_name, _include_private = false) ⇒ Boolean
- #session ⇒ Object
- #tagged(view, tag) ⇒ Object
- #template ⇒ Object
Methods included from Nest::ClassMethods
Methods included from Registration
class_from_name, extract_class_vars, extract_view_tags, format_ancestry, format_class_name, format_sym, new, register
Methods included from Error
#debug_error, #error_cardname, #rendering_error, #rescue_view
Methods included from Content
#add_class, #format_date, #get_content_object, #id_counter, #output, #process_content, #unique_id
Methods included from Names
#add_name_context, #context_names_from_params, #context_names_minus_irrelevants, #initialize_context_names, #showname, #with_name_context
Methods included from Render
#canonicalize_view, #configured_visibility, #current_view, #default_item_view, #default_render_args, #hidden_view?, #nest_arg_visibility, #optional_render_args, #parse_view_visibility, #render, #render_api, #show_view?, #view_method
Methods included from Permission
#approved_view, #ok?, #ok_view, #permitted_view, #view_for_unknown
Methods included from Nest
#field_nest, #get_nest_defaults, #nest, #nest_card, #nest_defaults, #process_nest
Methods included from Nest::View
Methods included from Nest::Subformat
Methods included from Nest::Main
Methods included from Nest::Fetch
Methods included from Env::Location
#card_path, #card_url, #page_path
Constructor Details
#initialize(card, opts = {}) ⇒ Format
Returns a new instance of Format.
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/card/format.rb', line 51 def initialize card, opts={} unless (@card = card) msg = I18n.t :exception_init_without_card, scope: "lib.card.format" raise Card::Error, msg end opts.each do |key, value| instance_variable_set "@#{key}", value end @mode ||= :normal @root ||= self @depth ||= 0 @context_names = initialize_context_names include_set_format_modules self end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *opts, &proc) ⇒ Object
117 118 119 120 121 122 123 |
# File 'lib/card/format.rb', line 117 def method_missing method, *opts, &proc if method =~ /(_)?(optional_)?render(_(\w+))?/ render_api Regexp.last_match, opts else pass_method_to_template_object(method, opts, proc) { yield } end end |
Instance Attribute Details
#card ⇒ Object (readonly)
Returns the value of attribute card.
48 49 50 |
# File 'lib/card/format.rb', line 48 def card @card end |
#error_status ⇒ Object
Returns the value of attribute error_status.
49 50 51 |
# File 'lib/card/format.rb', line 49 def error_status @error_status end |
#form ⇒ Object
Returns the value of attribute form.
49 50 51 |
# File 'lib/card/format.rb', line 49 def form @form end |
#main_opts ⇒ Object (readonly)
Returns the value of attribute main_opts.
48 49 50 |
# File 'lib/card/format.rb', line 48 def main_opts @main_opts end |
#nest_opts ⇒ Object
Returns the value of attribute nest_opts.
49 50 51 |
# File 'lib/card/format.rb', line 49 def nest_opts @nest_opts end |
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
48 49 50 |
# File 'lib/card/format.rb', line 48 def parent @parent end |
#root ⇒ Object (readonly)
Returns the value of attribute root.
48 49 50 |
# File 'lib/card/format.rb', line 48 def root @root end |
Instance Method Details
#controller ⇒ Object
88 89 90 |
# File 'lib/card/format.rb', line 88 def controller Env[:controller] ||= CardController.new end |
#focal? ⇒ Boolean
meaning the current card is the requested card
100 101 102 103 104 105 106 |
# File 'lib/card/format.rb', line 100 def focal? # meaning the current card is the requested card if Env.ajax? @depth.zero? else main? end end |
#include_set_format_modules ⇒ Object
70 71 72 73 74 75 76 |
# File 'lib/card/format.rb', line 70 def include_set_format_modules self.class.format_ancestry.reverse_each do |klass| card.set_format_modules(klass).each do |m| singleton_class.send :include, m end end end |
#main? ⇒ Boolean
96 97 98 |
# File 'lib/card/format.rb', line 96 def main? @depth.zero? end |
#page(view, slot_opts) ⇒ Object
78 79 80 81 82 |
# File 'lib/card/format.rb', line 78 def page view, slot_opts @card.run_callbacks :show_page do show view, slot_opts end end |
#pass_method_to_template_object(method, opts, proc) ⇒ Object
130 131 132 133 134 |
# File 'lib/card/format.rb', line 130 def pass_method_to_template_object method, opts, proc proc = proc { |*a| raw yield(*a) } if proc response = root.template.send method, *opts, &proc response.is_a?(String) ? root.template.raw(response) : response end |
#respond_to_missing?(method_name, _include_private = false) ⇒ Boolean
125 126 127 128 |
# File 'lib/card/format.rb', line 125 def respond_to_missing? method_name, _include_private=false (method_name =~ /(_)?(optional_)?render(_(\w+))?/) || template.respond_to?(method_name) end |
#tagged(view, tag) ⇒ Object
136 137 138 |
# File 'lib/card/format.rb', line 136 def tagged view, tag self.class.tagged view, tag end |
#template ⇒ Object
108 109 110 111 112 113 114 115 |
# File 'lib/card/format.rb', line 108 def template @template ||= begin c = controller t = ActionView::Base.new c.class.view_paths, { _routes: c._routes }, c t.extend c.class._helpers t end end |