Module: Card::Set::Abstract::Pointer::ItemApi

Extended by:
Card::Set
Defined in:
tmpsets/set/mod008-pointer/abstract/02_pointer/item_api.rb

Overview

Set: Abstract (Pointer, ItemApi)

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

Class Method Summary collapse

Instance Method Summary collapse

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

#abstract_set?, #all_set?, #num_set_parts, #shortname, #underscore

Methods included from Card::Set::AdvancedApi

#attachment, #ensure_set, #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 'tmpsets/set/mod008-pointer/abstract/02_pointer/item_api.rb', line 9

def self.source_location; "/Users/ethan/dev/decko/gem/card/mod/pointer/set/abstract/02_pointer/item_api.rb"; end

Instance Method Details

#<<(item) ⇒ Object

append item to list (does not save)

Parameters:

  • item (Cardish)


64
65
66
# File 'tmpsets/set/mod008-pointer/abstract/02_pointer/item_api.rb', line 64

def << item
  add_item Card::Name[item]
end

#add_item(name, allow_duplicates = false) ⇒ Object

append item to list (does not save)

Parameters:

  • name (String, Card::Name)

    item name

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

    permit duplicate items (default is False)



71
72
73
74
# File 'tmpsets/set/mod008-pointer/abstract/02_pointer/item_api.rb', line 71

def add_item name, allow_duplicates=false
  return if !allow_duplicates && include_item?(name)
  self.content = "[[#{(item_names << name).reject(&:blank?) * "]]\n[["}]]"
end

#add_item!(name) ⇒ Object

append item to list and save card

Parameters:



78
79
80
# File 'tmpsets/set/mod008-pointer/abstract/02_pointer/item_api.rb', line 78

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

#all_item_cards(args = {}) ⇒ Object



130
131
132
133
134
# File 'tmpsets/set/mod008-pointer/abstract/02_pointer/item_api.rb', line 130

def all_item_cards args={}
  item_names(args).map do |name|
    Card.fetch name, new: new_unknown_item_args(args)
  end
end

#all_raw_item_strings(content = nil) ⇒ Object



160
161
162
# File 'tmpsets/set/mod008-pointer/abstract/02_pointer/item_api.rb', line 160

def all_raw_item_strings content=nil
  (content || self.content).to_s.split(/\n+/)
end

#drop_item(name) ⇒ Object

remove item from list

Parameters:



84
85
86
87
88
89
# File 'tmpsets/set/mod008-pointer/abstract/02_pointer/item_api.rb', line 84

def drop_item name
  return unless include_item? name
  key = name.to_name.key
  new_names = item_names.reject { |n| n.to_name.key == key }
  self.content = new_names.empty? ? "" : "[[#{new_names * "]]\n[["}]]"
end

#drop_item!(name) ⇒ Object

remove item from list and save card

Parameters:



93
94
95
96
# File 'tmpsets/set/mod008-pointer/abstract/02_pointer/item_api.rb', line 93

def drop_item! name
  drop_item name
  save!
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



101
102
103
104
105
106
# File 'tmpsets/set/mod008-pointer/abstract/02_pointer/item_api.rb', line 101

def insert_item index, name
  new_names = item_names
  new_names.delete name
  new_names.insert index, name
  self.content = new_names.map { |new_name| "[[#{new_name}]]" }.join "\n"
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



111
112
113
114
# File 'tmpsets/set/mod008-pointer/abstract/02_pointer/item_api.rb', line 111

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



39
40
41
42
43
# File 'tmpsets/set/mod008-pointer/abstract/02_pointer/item_api.rb', line 39

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



122
123
124
# File 'tmpsets/set/mod008-pointer/abstract/02_pointer/item_api.rb', line 122

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)



30
31
32
# File 'tmpsets/set/mod008-pointer/abstract/02_pointer/item_api.rb', line 30

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
# File 'tmpsets/set/mod008-pointer/abstract/02_pointer/item_api.rb', line 22

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

#item_typeObject

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



143
144
145
146
147
148
# File 'tmpsets/set/mod008-pointer/abstract/02_pointer/item_api.rb', line 143

def item_type
  opt = options_rule_card
  # FIXME: need better recursion prevention
  return if !opt || opt == self
  opt.item_type
end

#items=(array) ⇒ Object

set card content based on array and save card

Parameters:

  • array (Array)

    list of strings/names (Cardish)



56
57
58
59
60
# File 'tmpsets/set/mod008-pointer/abstract/02_pointer/item_api.rb', line 56

def items= array
  self.content = ""
  array.each { |i| self << i }
  save!
end

#known_item_cards(args = {}) ⇒ Object



126
127
128
# File 'tmpsets/set/mod008-pointer/abstract/02_pointer/item_api.rb', line 126

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

#new_unknown_item_args(args) ⇒ Object



136
137
138
139
# File 'tmpsets/set/mod008-pointer/abstract/02_pointer/item_api.rb', line 136

def new_unknown_item_args args
  itype = args[:type] || item_type
  itype ? { type: itype } : {}
end

#raw_item_strings(content, limit, offset) ⇒ Object

item_names helpers



152
153
154
155
156
157
158
# File 'tmpsets/set/mod008-pointer/abstract/02_pointer/item_api.rb', line 152

def raw_item_strings content, limit, offset
  items = all_raw_item_strings content
  limit = limit.to_i
  offset = offset.to_i
  return items unless limit.positive? || offset.positive?
  items[offset, (limit.zero? ? items.size : limit)] || []
end