Class: Card::Format

Inherits:
Object
  • Object
show all
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

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

Instance Method Summary collapse

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

#fetch_nested_card

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

#cardObject (readonly)

Returns the value of attribute card.



45
46
47
# File 'lib/card/format.rb', line 45

def card
  @card
end

#error_statusObject

Returns the value of attribute error_status.



46
47
48
# File 'lib/card/format.rb', line 46

def error_status
  @error_status
end

#formObject

Returns the value of attribute form.



46
47
48
# File 'lib/card/format.rb', line 46

def form
  @form
end

#main_optsObject (readonly)

Returns the value of attribute main_opts.



45
46
47
# File 'lib/card/format.rb', line 45

def main_opts
  @main_opts
end

#parentObject (readonly)

Returns the value of attribute parent.



45
46
47
# File 'lib/card/format.rb', line 45

def parent
  @parent
end

Instance Method Details

#controllerObject



80
81
82
# File 'lib/card/format.rb', line 80

def controller
  Env[:controller] ||= CardController.new
end

#include_set_format_modulesObject



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_typeObject



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

#paramsObject



76
77
78
# File 'lib/card/format.rb', line 76

def params
  Env.params
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

Raises:



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

Returns:

  • (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

#sessionObject



84
85
86
# File 'lib/card/format.rb', line 84

def session
  Env.session
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

#templateObject



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_symObject



125
126
127
# File 'lib/card/format.rb', line 125

def to_sym
  Card::Format.format_sym self
end