Class: Card::Format

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

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 Nest

Nest::NEST_MODES

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_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 Names

#add_name_context, #context_names_from_params, #context_names_minus_irrelevants, #context_names_to_params, #initial_context_names, #showname

Methods included from Render

#api_render, #current_view, #expand_stubs, #final_render, #optional_render_args, #prepare_stub_nest, #render, #show_view?, #stub_render, #view_cache_setting, #view_method, #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_render, #nest_subformat, #reuse_format?

Methods included from Nest::View

#hide_view_in_edit_mode?, #modal_nest_view, #view_in_closed_mode, #view_in_edit_mode, #with_nest_mode

Methods included from Nest::Subformat

#field_subformat, #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.



50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/card/format.rb', line 50

def initialize card, opts={}
  @card = card
  require_card_to_initialize!

  opts.each { |key, value| instance_variable_set "@#{key}", value }

  @mode ||= :normal
  @root ||= self
  @depth ||= 0

  @context_names = initial_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



114
115
116
117
118
119
120
# File 'lib/card/format.rb', line 114

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.



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

def card
  @card
end

#error_statusObject

Returns the value of attribute error_status.



48
49
50
# File 'lib/card/format.rb', line 48

def error_status
  @error_status
end

#formObject

Returns the value of attribute form.



48
49
50
# File 'lib/card/format.rb', line 48

def form
  @form
end

#main_optsObject (readonly)

Returns the value of attribute main_opts.



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

def main_opts
  @main_opts
end

#modeObject (readonly)

Returns the value of attribute mode.



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

def mode
  @mode
end

#parentObject (readonly)

Returns the value of attribute parent.



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

def parent
  @parent
end

#rootObject (readonly)

Returns the value of attribute root.



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

def root
  @root
end

Instance Method Details

#controllerObject



89
90
91
# File 'lib/card/format.rb', line 89

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

#focal?Boolean

meaning the current card is the requested card

Returns:

  • (Boolean)


101
102
103
# File 'lib/card/format.rb', line 101

def focal? # meaning the current card is the requested card
  @depth.zero?
end

#include_set_format_modulesObject



71
72
73
74
75
76
77
# File 'lib/card/format.rb', line 71

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

Returns:

  • (Boolean)


97
98
99
# File 'lib/card/format.rb', line 97

def main?
  @depth.zero?
end

#page(view, slot_opts) ⇒ Object



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

def page view, slot_opts
  @card.run_callbacks :show_page do
    show view, slot_opts
  end
end

#paramsObject



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

def params
  Env.params
end

#pass_method_to_template_object(method, opts, proc) ⇒ Object



127
128
129
130
131
# File 'lib/card/format.rb', line 127

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:



65
66
67
68
69
# File 'lib/card/format.rb', line 65

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)


122
123
124
125
# File 'lib/card/format.rb', line 122

def respond_to_missing? method_name, _include_private=false
  (method_name =~ /(_)?(optional_)?render(_(\w+))?/) ||
    template.respond_to?(method_name)
end

#sessionObject



93
94
95
# File 'lib/card/format.rb', line 93

def session
  Env.session
end

#tagged(view, tag) ⇒ Object



133
134
135
# File 'lib/card/format.rb', line 133

def tagged view, tag
  self.class.tagged view, tag
end

#templateObject



105
106
107
108
109
110
111
112
# File 'lib/card/format.rb', line 105

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