class Card::Path

Generate standard card paths.

Attributes

opts[R]

Public Class Methods

new(card, opts) click to toggle source
# File lib/card/path.rb, line 6
def initialize card, opts
  @card = card
  @opts = opts
end

Public Instance Methods

render() click to toggle source
# File lib/card/path.rb, line 11
def render
  new_cardtype || standard
end

Private Instance Methods

action() click to toggle source
# File lib/card/path.rb, line 19
def action
  @action ||= opts.delete(:action)&.to_sym
end
action_base() click to toggle source
# File lib/card/path.rb, line 45
def action_base
  mark.present? ? "#{action}/#{mark}" : "card/#{action}"
  # the card/ prefix prevents interpreting action as cardname
end
base() click to toggle source
# File lib/card/path.rb, line 41
def base
  explicit_action? ? action_base : mark
end
explicit_action?() click to toggle source
# File lib/card/path.rb, line 50
def explicit_action?
  action.in? %i[create update delete]
end
extension() click to toggle source
# File lib/card/path.rb, line 73
def extension
  extension = opts.delete :format
  extension ? ".#{extension}" : ""
end
handle_unknown() { || ... } click to toggle source
# File lib/card/path.rb, line 83
def handle_unknown
  yield.tap do |name|
    return name if name.nil? || known_name?(name)

    opts[:card] ||= {}
    opts[:card][:name] = name
  end
interpret_mark() click to toggle source
# File lib/card/path.rb, line 58
def interpret_mark
  name = handle_unknown do
    opts[:mark] ? Card::Name[opts.delete(:mark)] : @card.name
  end
  name&.url_key.to_s
end
mark() click to toggle source
# File lib/card/path.rb, line 54
def mark
  @mark ||= (markless? ? "" : interpret_mark)
end
markless?() click to toggle source
# File lib/card/path.rb, line 65
def markless?
  action == :create || no_mark?
end
new_cardtype() click to toggle source
# File lib/card/path.rb, line 23
def new_cardtype
  return unless new_cardtype?

  "#{action}/#{mark}#{query}"
end
new_cardtype?() click to toggle source
# File lib/card/path.rb, line 29
def new_cardtype?
  return false unless action.in? %i[new type]

  # "new" and "type" are not really an action and are only
  # a valid value here for this path
  opts[:mark].present?
end
no_mark?() click to toggle source
# File lib/card/path.rb, line 69
def no_mark?
  @no_mark ||= opts.delete :no_mark
end
query() click to toggle source
# File lib/card/path.rb, line 78
def query
  query_opts = cast_path_hash opts
  query_opts.empty? ? "" : "?#{query_opts.to_param}"
end
standard() click to toggle source
# File lib/card/path.rb, line 37
def standard
  base + extension + query
end