Module: Card::Format::ContextNames

Included in:
Card::Format
Defined in:
lib/card/format/context_names.rb

Overview

Contextual names make titles less noisy by not rendering redundant name parts

For example, in the context of “Ball”, “Ball+size” is rendered as just “+size”

Instance Method Summary collapse

Instance Method Details

#add_name_context(name = nil) ⇒ Object



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

def add_name_context name=nil
  name ||= card.name
  @context_names = (context_names + name.to_name.part_names).uniq
end

#context_namesObject

TODO: stop this lazy loading the combo of lazy loading + format ancestry navigation + caching is dangerous Long term, it would probably be smarter to handle this in the voo.



16
17
18
# File 'lib/card/format/context_names.rb', line 16

def context_names
  @context_names ||= initial_context_names
end

#context_names_from_paramsObject

slot” param is a string; @context_names is an array



32
33
34
35
36
# File 'lib/card/format/context_names.rb', line 32

def context_names_from_params
  return [] unless (name_list = Card::Env.slot_opts.delete(:name_context))

  name_list.to_s.split(",").map(&:to_name)
end

#context_names_to_paramsObject



38
39
40
41
42
# File 'lib/card/format/context_names.rb', line 38

def context_names_to_params
  return if context_names.empty?

  context_names.join(",")
end

#initial_context_namesObject



20
21
22
23
24
# File 'lib/card/format/context_names.rb', line 20

def initial_context_names
  @initial_context_names ||= relevant_context_names do
    parent ? parent.context_names : context_names_from_params
  end
end

#naming(name = nil) ⇒ Object



7
8
9
10
11
# File 'lib/card/format/context_names.rb', line 7

def naming name=nil
  result = yield
  add_name_context name
  result
end

#relevant_context_namesObject



26
27
28
29
# File 'lib/card/format/context_names.rb', line 26

def relevant_context_names
  part_keys = @card.name.part_names.map(&:key)
  yield.select { |n| part_keys.include? n.key }
end

#title_in_context(title = nil) ⇒ Object



49
50
51
52
53
54
# File 'lib/card/format/context_names.rb', line 49

def title_in_context title=nil
  keep_safe = title&.html_safe?
  title = title ? title.to_name.absolute_name(card.name) : card.name
  newtitle = title.from(*context_names)
  keep_safe ? newtitle.html_safe : newtitle
end