Module: Card::Set::Abstract::Items

Extended by:
Card::Set
Defined in:
card/tmpsets/set/mod006-card-mod-collection/abstract/items.rb,
card/tmpsets/set/mod005-card-mod-collection/abstract/items.rb

Overview

~~~~~~~~~~~~ READING ITEMS ~~~~~~~~~~~~

Constant Summary

Constants included from Helpers

Helpers::SET_PATTERN_TEST_REGEXP

Constants included from Event::Api

Event::Api::OPTIONS

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Card::Set

reset_modules

Methods included from I18nScope

#mod_name, #scope

Methods included from Loader

#clean_empty_module_from_hash, #clean_empty_modules, #extended, #process_base_modules, #register_set

Methods included from Helpers

#method_missing, #num_set_parts, #pattern_code, #respond_to_missing?, #set_name_parts, #shortname, #underscore

Methods included from Card::Set::AdvancedApi

#attachment, #define_set_from_error, #ensure_set, #setting_opts, #stage_method

Methods included from Format

#before, #format, layout_method_name, #view, view_method_name, view_setting_method_name, wrapper_method_name

Methods included from Inheritance

#include_set, #include_set_formats

Methods included from Basket

#abstract_basket, #add_to_basket, #basket, #unshift_basket

Methods included from Trait

#card_accessor, #card_reader, #card_writer, #require_field

Methods included from Event::Api

#event

Class Method Details

.source_locationObject



9
# File 'card/tmpsets/set/mod006-card-mod-collection/abstract/items.rb', line 9

def self.source_location; "/Users/ethan/dev/decko/gem/card-mod-collection/set/abstract/items.rb"; end

Instance Method Details

#<<(cardish) ⇒ Object

append item to list (does not save)

Parameters:

  • cardish (Cardish)


86
87
88
# File 'card/tmpsets/set/mod006-card-mod-collection/abstract/items.rb', line 86

def << cardish
  add_item cardish
end

#add_item(cardish, allow_duplicates = false) ⇒ Object

append item to list (does not save)

Parameters:

  • cardish (String, Card::Name)

    item name

  • allow_duplicates (True/False) (defaults to: false)

    permit duplicate items (default is False)



93
94
95
96
97
98
# File 'card/tmpsets/set/mod006-card-mod-collection/abstract/items.rb', line 93

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) ⇒ Object

append item to list and save card

Parameters:



102
103
104
# File 'card/tmpsets/set/mod006-card-mod-collection/abstract/items.rb', line 102

def add_item! name
  add_item(name) && save!
end

#all_item_cards(args = {}) ⇒ Object



152
153
154
155
# File 'card/tmpsets/set/mod006-card-mod-collection/abstract/items.rb', line 152

def all_item_cards args={}
  names = args[:item_names] || item_names(args)
  names.map { |name| fetch_item_card name, args }
end

#drop_item(cardish) ⇒ Object

remove item from list

Parameters:



108
109
110
111
# File 'card/tmpsets/set/mod006-card-mod-collection/abstract/items.rb', line 108

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) ⇒ Object

remove item from list and save card

Parameters:



115
116
117
118
# File 'card/tmpsets/set/mod006-card-mod-collection/abstract/items.rb', line 115

def drop_item! cardish
  drop_item cardish
  save!
end

#first_card(args = {}) ⇒ Object



33
34
35
36
# File 'card/tmpsets/set/mod006-card-mod-collection/abstract/items.rb', line 33

def first_card args={}
  return unless (name = first_name)
  fetch_item_card name, args
end

#first_codeObject



38
39
40
# File 'card/tmpsets/set/mod006-card-mod-collection/abstract/items.rb', line 38

def first_code
  first_card&.codename
end

#first_idObject



42
43
44
# File 'card/tmpsets/set/mod006-card-mod-collection/abstract/items.rb', line 42

def first_id
  first_card&.id
end

#first_name(args = {}) ⇒ Object



29
30
31
# File 'card/tmpsets/set/mod006-card-mod-collection/abstract/items.rb', line 29

def first_name args={}
  item_names(args).first
end

#insert_item(index, name) ⇒ Object

insert item into list at specified location

Parameters:

  • index (Integer)

    Array index in which to insert item (0 is first)

  • name (String, Card::Name)

    item name



123
124
125
126
127
128
# File 'card/tmpsets/set/mod006-card-mod-collection/abstract/items.rb', line 123

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) ⇒ Object

insert item into list at specified location and save

Parameters:

  • index (Integer)

    Array index in which to insert item (0 is first)

  • name (String, Card::Name)

    item name



133
134
135
136
# File 'card/tmpsets/set/mod006-card-mod-collection/abstract/items.rb', line 133

def insert_item! index, name
  insert_item index, name
  save!
end

#item_cards(args = {}) ⇒ Array

Returns list of Card objects.

Parameters:

  • args (Hash) (defaults to: {})

    see #item_names for additional options

Options Hash (args):

  • :complete (String)

    keyword to use in searching among items

  • :known_only (True/False)

    if true, return only known cards

  • :type (String)

    name of type to be used for unknown cards

Returns:

  • (Array)

    list of Card objects



57
58
59
60
61
# File 'card/tmpsets/set/mod006-card-mod-collection/abstract/items.rb', line 57

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_cards_search(query) ⇒ Object

item_cards helpers



144
145
146
# File 'card/tmpsets/set/mod006-card-mod-collection/abstract/items.rb', line 144

def item_cards_search query
  Card::Query.run query.reverse_merge(referred_to_by: name, limit: 0)
end

#item_ids(args = {}) ⇒ Array

Returns list of integers (card ids of items).

Parameters:

  • args (Hash) (defaults to: {})

    see #item_names

Returns:

  • (Array)

    list of integers (card ids of items)



48
49
50
# File 'card/tmpsets/set/mod006-card-mod-collection/abstract/items.rb', line 48

def item_ids args={}
  item_names(args).map { |name| Card.fetch_id name }.compact
end

#item_names(args = {}) ⇒ Array

Returns list of Card::Name objects.

Parameters:

  • args (Hash) (defaults to: {})

Options Hash (args):

  • :content (String)

    override card content

  • :context (String, Card::Name, Symbol)

    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

  • :limit (String, Integer)

    max number of cards to return

  • :offset (String, Integer)

    begin after the offset-th item

Returns:

  • (Array)

    list of Card::Name objects



22
23
24
25
26
27
# File 'card/tmpsets/set/mod006-card-mod-collection/abstract/items.rb', line 22

def item_names args={}
  context = args[:context]
  item_strings(args).map do |item|
    clean_item_name item, context
  end.compact
end

#item_strings(args = {}) ⇒ Object



166
167
168
169
170
171
# File 'card/tmpsets/set/mod006-card-mod-collection/abstract/items.rb', line 166

def item_strings args={}
  items = raw_item_strings(args[:content] || content)
  return items unless args.present?

  filtered_items items, args.slice(:limit, :offset)
end

#item_typeObject

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



159
160
161
162
163
164
# File 'card/tmpsets/set/mod006-card-mod-collection/abstract/items.rb', line 159

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) ⇒ Object

for override, eg by json



71
72
73
# File 'card/tmpsets/set/mod006-card-mod-collection/abstract/items.rb', line 71

def item_value item_name
  item_name
end

#items=(array) ⇒ Object

set card content based on array and save card

Parameters:

  • array (Array)

    list of strings/names (Cardish)



79
80
81
82
# File 'card/tmpsets/set/mod006-card-mod-collection/abstract/items.rb', line 79

def items= array
  items_to_content array
  save!
end

#known_item_cards(args = {}) ⇒ Object



148
149
150
# File 'card/tmpsets/set/mod006-card-mod-collection/abstract/items.rb', line 148

def known_item_cards args={}
  item_names(args).map { |name| Card.fetch name }.compact
end

#raw_item_strings(content) ⇒ Object



173
174
175
# File 'card/tmpsets/set/mod006-card-mod-collection/abstract/items.rb', line 173

def raw_item_strings content
  content.to_s.split(/\n+/).map { |i| strip_item i }
end