Class: Card::Format

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

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

Render::DEPRECATED_VIEWS

Constants included from Nest::View

Nest::View::NEST_MODES

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Nest::ClassMethods

max_depth

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

#nest_render, #with_nest_mode

Methods included from Nest::Subformat

#field_subformat, #subformat

Methods included from Nest::Main

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



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

#cardObject (readonly)

Returns the value of attribute card.



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

def card
  @card
end

#error_statusObject

Returns the value of attribute error_status.



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

def error_status
  @error_status
end

#formObject

Returns the value of attribute form.



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

def form
  @form
end

#main_optsObject (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_optsObject

Returns the value of attribute nest_opts.



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

def nest_opts
  @nest_opts
end

#parentObject (readonly)

Returns the value of attribute parent.



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

def parent
  @parent
end

#rootObject (readonly)

Returns the value of attribute root.



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

def root
  @root
end

Instance Method Details

#controllerObject



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

Returns:

  • (Boolean)


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_modulesObject



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

Returns:

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

#paramsObject



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

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

Returns:

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

#sessionObject



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

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

#templateObject



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