Class: Card::Format
- Inherits:
-
Object
- Object
- Card::Format
- Extended by:
- Registration
- Includes:
- Env::Location, Content, ContextNames, Error, Nest, Permission, Render
- Defined in:
- lib/card/format.rb,
lib/card/format/nest.rb,
lib/card/format/error.rb,
lib/card/format/render.rb,
lib/card/format/content.rb,
lib/card/format/nest/main.rb,
lib/card/format/nest/mode.rb,
lib/card/format/nest/fetch.rb,
lib/card/format/permission.rb,
lib/card/format/registration.rb,
lib/card/format/context_names.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/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,
mod/notifications/format/email_html_format.rb,
mod/notifications/format/email_text_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, ContextNames, Error, Nest, Permission, Registration, Render Classes: CssFormat, CsvFormat, DataFormat, EmailHtmlFormat, EmailTextFormat, FileFormat, HtmlFormat, JsFormat, JsonFormat, RssFormat, TextFormat, XmlFormat
Constant Summary collapse
- VIEW_VARS =
[ :perms, :denial, :closed, :error_code ]
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.
-
#parent ⇒ Object
readonly
Returns the value of attribute parent.
Instance Method Summary collapse
- #controller ⇒ Object
- #include_set_format_modules ⇒ Object
-
#initialize(card, opts = {}) ⇒ Format
constructor
A new instance of Format.
- #method_missing(method, *opts, &proc) ⇒ Object
- #mime_type ⇒ Object
- #page(view, slot_opts) ⇒ Object
- #params ⇒ Object
- #pass_method_to_template_object(method, opts, proc) ⇒ Object
- #require_card_to_initialize! ⇒ Object
- #respond_to_missing?(method_name, _include_private = false) ⇒ Boolean
- #session ⇒ Object
- #tagged(view, tag) ⇒ Object
- #template ⇒ Object
- #to_sym ⇒ Object
Methods included from Registration
class_from_name, extract_class_vars, extract_view_tags, format_ancestry, format_class, format_class_name, format_sym, interpret_view_opts, new, register, view_cache_setting_method
Methods included from Error
#debug_error, #error_cardname, #rendering_error, #rescue_view
Methods included from Content
#add_class, #css_classes, #format_date, #get_content_object, #id_counter, #output, #prepend_class, #process_content, #unique_id
Methods included from ContextNames
#add_name_context, #context_names, #context_names_from_params, #context_names_from_parent, #context_names_to_params, #initial_context_names, #title_in_context
Methods included from Render
#add_debug_info, #api_render, #current_view, #expand_stubs, #final_render, #optional_render_args, #prepare_stub_nest, #pretty_path, #render, #show_debug_info?, #show_view?, #stub_render, #supports_view?, #view_cache_setting, #view_caching?, #view_method, #view_method_name, #view_options_with_defaults, #voo, #with_voo
Methods included from Permission
#assign_view_error_status, #check_view, #handle_view_denial, #ok?, #ok_view, #permitted_view, #subformats_nested_too_deeply?, #task_denied_for_view, #unknown_disqualifies_view?, #view_always_permitted?, #view_for_unknown
Methods included from Nest
#content_nest, #content_view?, #default_nest_view, #field_nest, #implicit_nest_view, #interpret_nest_options, #nest, #nest_recursion_risk?, #nest_render, #nest_subformat, #reuse_format?
Methods included from Nest::Mode
#configured_view_in_closed_mode, #hide_view_in_edit_mode?, #modal_nest_view, #nest_mode, #view_in_closed_mode, #view_in_edit_mode, #with_nest_mode
Methods included from Nest::Subformat
#depth, #field_subformat, #focal?, #main?, #root, #subformat
Methods included from Nest::Main
#already_mained?, #main!, #main_nest, #main_nest?, #main_nest_options, #wrap_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.
48 49 50 51 52 53 54 |
# File 'lib/card/format.rb', line 48 def initialize card, opts={} @card = card require_card_to_initialize! opts.each { |key, value| instance_variable_set "@#{key}", value } 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
98 99 100 101 102 103 104 |
# File 'lib/card/format.rb', line 98 def method_missing method, *opts, &proc if method =~ /(_)?(optional_)?render(_(\w+))?/ api_render 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.
45 46 47 |
# File 'lib/card/format.rb', line 45 def card @card end |
#error_status ⇒ Object
Returns the value of attribute error_status.
46 47 48 |
# File 'lib/card/format.rb', line 46 def error_status @error_status end |
#form ⇒ Object
Returns the value of attribute form.
46 47 48 |
# File 'lib/card/format.rb', line 46 def form @form end |
#main_opts ⇒ Object (readonly)
Returns the value of attribute main_opts.
45 46 47 |
# File 'lib/card/format.rb', line 45 def main_opts @main_opts end |
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
45 46 47 |
# File 'lib/card/format.rb', line 45 def parent @parent end |
Instance Method Details
#controller ⇒ Object
80 81 82 |
# File 'lib/card/format.rb', line 80 def controller Env[:controller] ||= CardController.new end |
#include_set_format_modules ⇒ Object
62 63 64 65 66 67 68 |
# File 'lib/card/format.rb', line 62 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 |
#mime_type ⇒ Object
121 122 123 |
# File 'lib/card/format.rb', line 121 def mime_type "text/text" end |
#page(view, slot_opts) ⇒ Object
70 71 72 73 74 |
# File 'lib/card/format.rb', line 70 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
111 112 113 114 115 |
# File 'lib/card/format.rb', line 111 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 |
#require_card_to_initialize! ⇒ Object
56 57 58 59 60 |
# File 'lib/card/format.rb', line 56 def require_card_to_initialize! return if @card msg = I18n.t :exception_init_without_card, scope: "lib.card.format" raise Card::Error, msg end |
#respond_to_missing?(method_name, _include_private = false) ⇒ Boolean
106 107 108 109 |
# File 'lib/card/format.rb', line 106 def respond_to_missing? method_name, _include_private=false (method_name =~ /(_)?(optional_)?render(_(\w+))?/) || template.respond_to?(method_name) end |
#tagged(view, tag) ⇒ Object
117 118 119 |
# File 'lib/card/format.rb', line 117 def tagged view, tag self.class.tagged view, tag end |
#template ⇒ Object
89 90 91 92 93 94 95 96 |
# File 'lib/card/format.rb', line 89 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 |
#to_sym ⇒ Object
125 126 127 |
# File 'lib/card/format.rb', line 125 def to_sym Card::Format.format_sym self end |