class Object

Constants

MAX_ANONYMOUS_SEARCH_PARAM
PAGE_LI_CLASS

Public Instance Methods

<<(cardish) click to toggle source

append item to list (does not save) @param cardish [Cardish]

# File set/abstract/items.rb, line 81
def << cardish
  add_item cardish
end
add_id(id) click to toggle source

I think the following should work as add_item…

# File set/all/item.rb, line 66
def add_id id
  add_item "~#{id}"
end
add_item(cardish, allow_duplicates=false) click to toggle source

append item to list (does not save) @param cardish [String, Card::Name] item name @param allow_duplicates [True/False] permit duplicate items (default is False)

# File set/abstract/items.rb, line 88
def add_item cardish, allow_duplicates=false
  return if !allow_duplicates && include_item?(cardish)

  items = item_strings << cardish
  items_to_content items
end
add_item!(name) click to toggle source

append item to list and save card @param name [String, Card::Name] item name

# File set/abstract/items.rb, line 97
def add_item! name
  add_item(name) && save!
end
add_paging_url(hash, page, status) click to toggle source
# File set/abstract/paging/paging_views.rb, line 107
def add_paging_url hash, page, status
  return unless page && status.in?(%i[next previous])

  hash[status] = path page_link_path_args(page)
end
all_item_cards(args={}) click to toggle source
# File set/abstract/items.rb, line 147
def all_item_cards args={}
  names = args[:item_names] || item_names(args)
  names.map { |name| fetch_item_card name, args }
end
already_extended?(item, book) click to toggle source
# File set/all/extended.rb, line 31
def already_extended? item, book
  return true if book.include? item

  book << item
  false
end
clean_item_name(item, context) click to toggle source
# File set/abstract/items.rb, line 192
def clean_item_name item, context
  item = item.to_name
  return item if context == :raw

  context ||= context_card.name
  item.absolute_name context
rescue Card::Error::NotFound
  # eg for invalid ids or codenames
  # "Invalid Item: #{item}".to_name
  nil
end
contextual_param(param) click to toggle source
# File set/abstract/paging.rb, line 37
def contextual_param param
  env_search_param(param) || voo_search_param(param)
end
count_with_params() click to toggle source
# File set/abstract/paging.rb, line 16
def count_with_params
  @count_with_params ||= card.item_names.count
end
current_page() click to toggle source
# File set/abstract/paging.rb, line 26
def current_page
  (offset / limit).to_i
end
default_item_view() click to toggle source
# File set/all/item.rb, line 100
def default_item_view
  :name
end
determine_item_view_options_type(options) click to toggle source
# File set/all/item.rb, line 112
def determine_item_view_options_type options
  return if options[:type]

  type_from_rule = card.item_type
  options[:type] = type_from_rule if type_from_rule
end
drop_id(id) click to toggle source
# File set/all/item.rb, line 70
def drop_id id
  drop_item "~#{id}"
end
drop_item(cardish) click to toggle source

remove item from list @param cardish [String, Card::Name] item to drop

# File set/abstract/items.rb, line 103
def drop_item cardish
  drop_item_name = Card::Name[cardish]
  items_to_content(item_names.reject { |item_name| item_name == drop_item_name })
end
drop_item!(cardish) click to toggle source

remove item from list and save card @param cardish [String, Card::Name] item to drop

# File set/abstract/items.rb, line 110
def drop_item! cardish
  drop_item cardish
  save!
end
env_search_param(param) click to toggle source
# File set/abstract/paging.rb, line 41
def env_search_param param
  enforcing_legal_limit param do
    val = Env.params[param]
    val.to_i if focal? && val.present?
  end
end
extend_item_list(items, list, book) click to toggle source
# File set/all/extended.rb, line 19
def extend_item_list items, list, book
  item = items.shift
  return if already_extended? item, book

  if item.collection?
    # keep items in order
    items.unshift(*item.item_cards)
  else  # no further level of items
    list << item
  end
end
extended_item_cards(context=nil) click to toggle source
# File set/all/extended.rb, line 1
def extended_item_cards context=nil
  items = item_cards limit: "", context: (context || self).name
  list = []
  book = ::Set.new # avoid loops
  extend_item_list items, list, book until items.empty?
  list
end
extended_item_contents(context=nil) click to toggle source
# File set/all/extended.rb, line 9
def extended_item_contents context=nil
  extended_item_cards(context).map(&:item_names).flatten
end
extra_paging_path_args() click to toggle source

for override

# File set/abstract/paging.rb, line 31
def extra_paging_path_args
  {}
end
fetch_item_card(name, args={}) click to toggle source
# File set/abstract/items.rb, line 183
def fetch_item_card name, args={}
  Card.fetch name, new: new_unknown_item_args(args)
end
fewer_results_than_limit?() click to toggle source

clear we don't need paging even before running count query

# File set/abstract/paging/paging_views.rb, line 76
def fewer_results_than_limit?
  return false unless offset.zero?

  limit > offset + search_with_params.length
end
filtered_items(items, limit: 0, offset: 0) click to toggle source
# File set/abstract/items.rb, line 175
def filtered_items items, limit: 0, offset: 0
  limit = limit.to_i
  offset = offset.to_i
  return items unless limit.positive? || offset.positive?

  items[offset, (limit.zero? ? items.size : limit)] || []
end
first_card(args={}) click to toggle source
# File set/abstract/items.rb, line 26
def first_card args={}
  return unless (name = first_name)

  fetch_item_card name, args
end
first_code() click to toggle source
# File set/abstract/items.rb, line 32
def first_code
  first_card&.codename
end
first_id() click to toggle source
# File set/abstract/items.rb, line 36
def first_id
  first_card&.id
end
first_name(args={}) click to toggle source
# File set/abstract/items.rb, line 22
def first_name args={}
  item_names(args).first
end
implicit_item_view() click to toggle source
# File set/all/item.rb, line 89
def implicit_item_view
  view = voo_items_view || default_item_view
  Card::View.normalize view
end
include_item?(item) click to toggle source
# File set/all/item.rb, line 33
def include_item? item
  item_names.include? Card::Name[item]
end
insert_id(index, id) click to toggle source
# File set/all/item.rb, line 74
def insert_id index, id
  insert_item index, "~#{id}"
end
insert_item(index, name) click to toggle source

insert item into list at specified location @param index [Integer] Array index in which to insert item (0 is first) @param name [String, Card::Name] item name

# File set/abstract/items.rb, line 118
def insert_item index, name
  new_names = item_names
  new_names.delete name
  new_names.insert index, name
  items_to_content new_names
end
insert_item!(index, name) click to toggle source

insert item into list at specified location and save @param index [Integer] Array index in which to insert item (0 is first) @param name [String, Card::Name] item name

# File set/abstract/items.rb, line 128
def insert_item! index, name
  insert_item index, name
  save!
end
item_cards(args={}) click to toggle source

@return [Array] list of Card objects @param args [Hash] see item_names for additional options @option args [String] :complete keyword to use in searching among items @option args [True/False] :known_only if true, return only known cards @option args [String] :type name of type to be used for unknown cards

# File set/abstract/items.rb, line 51
def item_cards args={}
  return item_cards_search(args) if args[:complete]
  return known_item_cards(args) if args[:known_only]

  all_item_cards args
end
item_count(args={}) click to toggle source
# File set/all/item.rb, line 20
def item_count args={}
  item_names(args).size
end
item_ids(args={}) click to toggle source

@return [Array] list of integers (card ids of items) @param args [Hash] see item_names

# File set/abstract/items.rb, line 42
def item_ids args={}
  item_names(args).map(&:card_id).compact
end
item_keys(args={}) click to toggle source
# File set/all/item.rb, line 14
def item_keys args={}
  item_names(args).map do |item|
    item.to_name.key
  end
end
item_names(args={}) click to toggle source

@return [Array] list of Card::Name objects @param args [Hash] @option args [String] :content override card content @option args [String, Card::Name, Symbol] :context name in whose context relative items

will be interpreted. For example. +A in context of B is interpreted as B+A
context defaults to pointer card's name. If value is `:raw`, then name is not
contextualized

@option args [String, Integer] :limit max number of cards to return @option args [String, Integer] :offset begin after the offset-th item

# File set/abstract/items.rb, line 15
def item_names args={}
  context = args[:context]
  item_strings(args).map do |item|
    clean_item_name item, context
  end.compact
end
item_strings(args={}) click to toggle source
# File set/abstract/items.rb, line 162
def item_strings args={}
  items = raw_item_strings(args[:content] || content)
  return items unless args.present?

  filtered_items items, limit: args[:limit], offset: args[:offset]
end
item_type() click to toggle source

TODO: support type_code and type_id. (currently type) uses name, because its most common use is from CQL

# File set/abstract/items.rb, line 154
def item_type
  opt = options_rule_card
  # FIXME: need better recursion prevention
  return if !opt || opt == self

  opt.item_type
end
item_value(item_name) click to toggle source

for override, eg by json

# File set/abstract/items.rb, line 66
def item_value item_name
  item_name
end
item_view_options(new_options={}) click to toggle source
# File set/all/item.rb, line 104
def item_view_options new_options={}
  options = (voo.items || {}).clone
  options = options.merge new_options
  options[:view] ||= implicit_item_view
  determine_item_view_options_type options
  options
end
items=(array) click to toggle source

set card content based on array and save card @param array [Array] list of strings/names (Cardish)

# File set/abstract/items.rb, line 74
def items= array
  items_to_content array
  save!
end
items_to_content(array) click to toggle source
# File set/all/item.rb, line 24
def items_to_content array
  items = array.map { |i| standardize_item i }.reject(&:blank?)
  self.content = items.to_pointer_content
end
known_item_cards(args={}) click to toggle source
# File set/abstract/items.rb, line 143
def known_item_cards args={}
  item_names(args).map { |name| Card.fetch name }.compact
end
limit() click to toggle source
# File set/abstract/paging.rb, line 4
def limit
  @limit ||= contextual_param(:limit) || default_limit
end
listing(listing_cards, item_args={}) click to toggle source
# File set/all/item.rb, line 119
def listing listing_cards, item_args={}
  listing_cards.map do |item_card|
    nest_item item_card, item_args do |rendered, item_view|
      wrap_item rendered, item_view
    end
  end
end
nest_item(cardish, options={}) click to toggle source
# File set/all/item.rb, line 83
def nest_item cardish, options={}, &block
  options = item_view_options options
  options[:nest_name] = Card::Name[cardish].s
  nest cardish, options, &block
end
new_unknown_item_args(args) click to toggle source
# File set/abstract/items.rb, line 187
def new_unknown_item_args args
  itype = args[:type] || item_type
  itype ? { type: itype } : {}
end
offset() click to toggle source
# File set/abstract/paging.rb, line 8
def offset
  @offset ||= contextual_param(:offset) || 0
end
paging_needed?() click to toggle source
# File set/abstract/paging/paging_views.rb, line 67
def paging_needed?
  return false if limit < 1
  return false if fewer_results_than_limit? # avoid extra count search

  # count search result instead
  limit < count_with_params
end
paging_path_args(local_args={}) click to toggle source
# File set/abstract/paging/paging_views.rb, line 56
def paging_path_args local_args={}
  @paging_path_args ||= {}
  @paging_path_args.reverse_merge!(limit: limit, offset: offset)
  @paging_path_args.merge! extra_paging_path_args
  @paging_path_args.merge local_args
end
paging_urls_hash() click to toggle source
# File set/abstract/paging/paging_views.rb, line 99
def paging_urls_hash
  hash = {}
  PagingLinks.new(total_pages, current_page).build do |_text, page, status, _options|
    add_paging_url hash, page, status
  end
  hash
end
raw_item_strings(content) click to toggle source
# File set/abstract/items.rb, line 169
def raw_item_strings content
  content.to_s.split(/\n+/).map { |i| strip_item i }
end
replace_item(old, new) click to toggle source
# File set/all/item.rb, line 57
def replace_item old, new
  return unless include_item? old

  drop_item old
  add_item new
end
search_with_params() click to toggle source
# File set/abstract/paging.rb, line 12
def search_with_params
  @search_with_params ||= card.item_names
end
standardize_item(item) click to toggle source
# File set/all/item.rb, line 29
def standardize_item item
  Card::Name[item]
end
strip_item(item) click to toggle source
# File set/abstract/items.rb, line 204
def strip_item item
  item.gsub(/\[\[|\]\]/, "").strip
end
total_pages() click to toggle source
# File set/abstract/paging.rb, line 20
def total_pages
  return 1 if limit.zero?

  ((count_with_params - 1) / limit).to_i
end
voo_items_view() click to toggle source
# File set/all/item.rb, line 94
def voo_items_view
  return unless voo && (items = voo.items)

  items[:view]
end
voo_search_param(param) click to toggle source
# File set/abstract/paging.rb, line 62
def voo_search_param param
  voo&.cql&.dig(param)&.to_i
end
with_paging(path_args={}) { |paging_path_args| ... } click to toggle source
# File set/abstract/paging/paging_views.rb, line 4
def with_paging path_args={}
  with_paging_path_args path_args do
    output [yield(@paging_path_args), _render_paging]
  end
end
with_paging_path_args(args) { || ... } click to toggle source
# File set/abstract/paging/paging_views.rb, line 48
def with_paging_path_args args
  tmp = @paging_path_args
  @paging_path_args = paging_path_args args
  yield
ensure
  @paging_path_args = tmp
end
wrap_item(item, _args={}) click to toggle source
# File set/all/item.rb, line 127
def wrap_item item, _args={}
  item # no wrap in base
end