Module: Card::Set::All::Json::JsonFormat

Extended by:
Card::Set::AbstractFormat
Defined in:
card/tmpsets/set/mod008-card-mod-format/all/json.rb,
card/tmpsets/set/mod007-card-mod-format/all/json.rb

Instance Method Summary collapse

Instance Method Details

#atomObject



155
156
157
158
159
# File 'card/tmpsets/set/mod008-card-mod-format/all/json.rb', line 155

def atom
  nucleus.tap do |h|
    h[:content] = render_content if card.known? && !card.structure
  end
end

#default_item_viewObject



18
19
20
# File 'card/tmpsets/set/mod008-card-mod-format/all/json.rb', line 18

def default_item_view
  params[:item] || :name
end

#default_nest_viewObject



14
15
16
# File 'card/tmpsets/set/mod008-card-mod-format/all/json.rb', line 14

def default_nest_view
  :atom
end

#essentialsObject



137
138
139
140
# File 'card/tmpsets/set/mod008-card-mod-format/all/json.rb', line 137

def essentials
  return {} if card.structure
  { content: card.db_content }
end

#item_cardsObject

because card.item_cards returns “[[#self]]”



10
11
12
# File 'card/tmpsets/set/mod008-card-mod-format/all/json.rb', line 10

def item_cards
  nested_cards
end

#marksObject

DEPRECATED



120
121
122
123
124
125
126
127
# File 'card/tmpsets/set/mod008-card-mod-format/all/json.rb', line 120

view :marks do
  {
    id: card.id,
    name: card.name,
    key: card.key,
    url: path
  }
end

#max_depthObject



22
23
24
# File 'card/tmpsets/set/mod008-card-mod-format/all/json.rb', line 22

def max_depth
  params[:max_depth].present? ? params[:max_depth].to_i : 1
end

#moleculeObject



161
162
163
164
165
166
167
# File 'card/tmpsets/set/mod008-card-mod-format/all/json.rb', line 161

def molecule
  atom.merge items: _render_items,
             links: _render_links,
             ancestors: _render_ancestors,
             html_url: path,
             type: nest(card.type_card, view: :nucleus)
end

#nucleusObject

NOTE: moving these to methods prevents potential caching problems, because other views manipulate their hashes.



145
146
147
148
149
150
151
152
153
# File 'card/tmpsets/set/mod008-card-mod-format/all/json.rb', line 145

def nucleus
  { id: card.id,
    name: card.name,
    type: card.type_name,
    url: path(format: :json) }.tap do |h|

    h[:codename] = card.codename if card.codename
  end
end

#page_details(obj) ⇒ Object



48
49
50
51
52
# File 'card/tmpsets/set/mod008-card-mod-format/all/json.rb', line 48

def page_details obj
  return obj unless obj.is_a? Hash

  obj.merge url: request_url, timestamp: Time.now.to_s
end

#request_urlObject



63
64
65
# File 'card/tmpsets/set/mod008-card-mod-format/all/json.rb', line 63

def request_url
  controller.request&.original_url || path
end

#show(view, args) ⇒ Object

TODO: support layouts in json eg layout=stamp gives you the metadata currently in “page” view and layout=none gives you ONLY the requested view (default atom)



29
30
31
32
33
34
# File 'card/tmpsets/set/mod008-card-mod-format/all/json.rb', line 29

def show view, args
  view ||= :molecule
  string_with_page_details do
    render! view, args
  end
end

#string_with_page_detailsObject



36
37
38
39
40
41
# File 'card/tmpsets/set/mod008-card-mod-format/all/json.rb', line 36

def string_with_page_details
  raw = yield
  return raw if raw.is_a? String

  stringify page_details(raw)
end

#stringify(raw) ⇒ Object



43
44
45
46
# File 'card/tmpsets/set/mod008-card-mod-format/all/json.rb', line 43

def stringify raw
  method = params[:compress] ? :generate : :pretty_generate
  JSON.send method, raw
end

#view: ancestorsObject



108
109
110
111
112
# File 'card/tmpsets/set/mod008-card-mod-format/all/json.rb', line 108

view :ancestors do
  card.name.ancestors.map do |name|
    nest name, view: :nucleus
  end
end

#view: atomObject

TODO: add simple values for fields



80
81
82
# File 'card/tmpsets/set/mod008-card-mod-format/all/json.rb', line 80

view :atom, unknown: true do
  atom
end

#view: castObject

minimum needed to re-fetch card



115
116
117
# File 'card/tmpsets/set/mod008-card-mod-format/all/json.rb', line 115

view :cast do
  card.cast
end

#view: contentObject



71
72
73
# File 'card/tmpsets/set/mod008-card-mod-format/all/json.rb', line 71

view :content do
  card.content
end

#view: coreObject



67
68
69
# File 'card/tmpsets/set/mod008-card-mod-format/all/json.rb', line 67

view :core, unknown: true do
  card.known? ? render_content : nil
end

#view: essentialsObject



129
130
131
132
133
134
135
# File 'card/tmpsets/set/mod008-card-mod-format/all/json.rb', line 129

view :essentials do
  if voo.show? :marks
    render_marks.merge(essentials)
  else
    essentials
  end
end

#view: itemsObject

NOCACHE because sometimes item_cards is dynamic. could be safely cached for non-dynamic lists



94
95
96
# File 'card/tmpsets/set/mod008-card-mod-format/all/json.rb', line 94

view :items, cache: :never do
  listing item_cards, view: :atom
end


98
99
100
101
102
103
104
105
106
# File 'card/tmpsets/set/mod008-card-mod-format/all/json.rb', line 98

view :links do
  card.link_chunks.map do |chunk|
    if chunk.referee_name
      path mark: chunk.referee_name, format: :json
    else
      link_to_resource chunk.link_target
    end
  end
end

#view: marksObject

DEPRECATED



120
121
122
123
124
125
126
127
# File 'card/tmpsets/set/mod008-card-mod-format/all/json.rb', line 120

view :marks do
  {
    id: card.id,
    name: card.name,
    key: card.key,
    url: path
  }
end

#view: moleculeObject



84
85
86
# File 'card/tmpsets/set/mod008-card-mod-format/all/json.rb', line 84

view :molecule do
  molecule
end

#view: nucleusObject



75
76
77
# File 'card/tmpsets/set/mod008-card-mod-format/all/json.rb', line 75

view :nucleus do
  nucleus
end

#view: pageObject



88
89
90
# File 'card/tmpsets/set/mod008-card-mod-format/all/json.rb', line 88

view :page, cache: :never do
  page_details card: render_atom
end

#view: statusObject



54
55
56
57
58
59
60
61
# File 'card/tmpsets/set/mod008-card-mod-format/all/json.rb', line 54

view :status, unknown: true, perms: :none do
  { key: card.key,
    url_key: card.name.url_key,
    status: card.state }.tap do |h|

    h[:id] = card.id if h[:status] == :real
  end
end