mod/pointer/set/abstract/02_pointer/item_api.rb in card-1.99.1 vs mod/pointer/set/abstract/02_pointer/item_api.rb in card-1.99.2
- old
+ new
@@ -11,15 +11,28 @@
# 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
def item_names args={}
- raw_item_strings(args[:content], args[:limit], args[:offset]).map do |item|
- clean_item_name item, args[:context]
+ context = args[:context]
+ item_strings(args).map do |item|
+ clean_item_name item, context
end.compact
end
+def first_name args={}
+ item_names(args).first
+end
+
+def first_card args={}
+ fetch_item_card first_name, args
+end
+
+def first_code
+ first_card&.codename
+end
+
# @return [Array] list of integers (card ids of items)
# @param args [Hash] see #item_names
def item_ids args={}
item_names(args).map { |name| Card.fetch_id name }.compact
end
@@ -45,42 +58,41 @@
# ~~~~~~~~~~~~ ALTERING ITEMS ~~~~~~~~~~~~
# set card content based on array and save card
# @param array [Array] list of strings/names (Cardish)
def items= array
- self.content = ""
- array.each { |i| self << i }
+ items_to_content array
save!
end
# append item to list (does not save)
# @param item [Cardish]
-def << item
- add_item Card::Name[item]
+def << cardish
+ add_item cardish
end
# append item to list (does not save)
-# @param name [String, Card::Name] item name
+# @param cardish [String, Card::Name] item name
# @param allow_duplicates [True/False] permit duplicate items (default is False)
-def add_item name, allow_duplicates=false
- return if !allow_duplicates && include_item?(name)
- self.content = "[[#{(item_names << name).reject(&:blank?) * "]]\n[["}]]"
+def add_item cardish, allow_duplicates=false
+ return if !allow_duplicates && include_item?(cardish)
+
+ items = item_strings << cardish
+ items_to_content items
end
# append item to list and save card
# @param name [String, Card::Name] item name
def add_item! name
add_item(name) && save!
end
# remove item from list
# @param name [String, Card::Name] item name
-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[["}]]"
+def drop_item cardish
+ drop_item_name = Card::Name[cardish]
+ items_to_content(item_names.reject { |item_name| item_name == drop_item_name })
end
# remove item from list and save card
# @param name [String, Card::Name] item name
def drop_item! name
@@ -93,11 +105,11 @@
# @param name [String, Card::Name] item name
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"
+ items_to_content new_names
end
# 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
@@ -119,46 +131,54 @@
def known_item_cards args={}
item_names(args).map { |name| Card.fetch name }.compact
end
def all_item_cards args={}
- item_names(args).map do |name|
- Card.fetch name, new: new_unknown_item_args(args)
- end
+ names = args[:item_names] || item_names(args)
+ names.map { |name| fetch_item_card name, args }
end
-def new_unknown_item_args args
- itype = args[:type] || item_type
- itype ? { type: itype } : {}
-end
-
# TODO: support type_code and type_id. (currently type)
# uses name, because its most common use is from WQL
def item_type
opt = options_rule_card
# FIXME: need better recursion prevention
return if !opt || opt == self
opt.item_type
end
-# #item_names helpers
+def item_strings args={}
+ items = raw_item_strings(args[:content] || content)
+ return items unless args.present?
-def raw_item_strings content, limit, offset
- items = all_raw_item_strings content
+ filtered_items items, args.slice(:limit, :offset)
+end
+
+def raw_item_strings content
+ content.to_s.split(/\n+/).map { |i| strip_item i }
+end
+
+private
+
+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
-def all_raw_item_strings content=nil
- (content || self.content).to_s.split(/\n+/)
+def fetch_item_card name, args={}
+ Card.fetch name, new: new_unknown_item_args(args)
end
-private
+def new_unknown_item_args args
+ itype = args[:type] || item_type
+ itype ? { type: itype } : {}
+end
def clean_item_name item, context
- item = strip_item(item).to_name
+ 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