Module: Card::Set::All::Collection

Extended by:
Card::Set
Defined in:
tmpsets/set/mod001-01_core/all/collection.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Methods included from Card::Set

abstract_set?, all_set?, card_accessor, card_reader, card_writer, clean_empty_module_from_hash, clean_empty_modules, define_on_format, ensure_set, extended, format, process_base_module_list, process_base_modules, register_set, register_set_format, shortname, stage_method, view, write_tmp_file

Methods included from Event

#define_event, #event

Instance Method Details

#add_item(item) ⇒ Object



63
64
65
66
# File 'tmpsets/set/mod001-01_core/all/collection.rb', line 63

def add_item item
  return if include_item? item
  self.content = "#{content}\n#{name}"
end

#contextual_content(context_card, format_args = {}, view_args = {}) ⇒ Object



121
122
123
124
125
# File 'tmpsets/set/mod001-01_core/all/collection.rb', line 121

def contextual_content context_card, format_args={}, view_args={}
  context_card.format(format_args).process_content(
    format(format_args)._render_raw(view_args), view_args
  )
end

#drop_item(item) ⇒ Object



68
69
70
71
72
# File 'tmpsets/set/mod001-01_core/all/collection.rb', line 68

def drop_item item
  return unless include_item? item
  new_names = item_names.reject { |i| i == item }
  self.content = new_names.empty? ? '' : new_names.join("\n")
end

#extended_item_cards(context = nil) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'tmpsets/set/mod001-01_core/all/collection.rb', line 81

def extended_item_cards context = nil
  context = (context ? context.cardname : cardname)
  args = { limit: '' }
  items = item_cards(args.merge(context: context))
  extended_list = []
  already_extended = ::Set.new # avoid loops

  while items.size > 0
    item = items.shift
    if already_extended.include? item
      next
    elsif item.item_cards == [item]  # no further level of items
      extended_list << item
      already_extended << item
    else
      items.unshift(*item.item_cards) # keep items in order
      already_extended << item
    end
  end
  extended_list
end

#extended_item_contents(context = nil) ⇒ Object



103
104
105
# File 'tmpsets/set/mod001-01_core/all/collection.rb', line 103

def extended_item_contents context=nil
  extended_item_cards(context).map(&:item_names).flatten
end

#extended_list(context = nil) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'tmpsets/set/mod001-01_core/all/collection.rb', line 107

def extended_list context=nil
  context = (context ? context.cardname : cardname)
  args = { limit: '' }
  item_cards(args.merge(context: context)).map do |x|
    x.item_cards(args)
  end.flatten.map do |x|
    x.item_cards(args)
  end.flatten.map do |y|
    y.item_names(args)
  end.flatten
  # this could go on and on.  more elegant to recurse until you don't have
  # a collection
end

#include_item?(item) ⇒ Boolean

Returns:

  • (Boolean)


58
59
60
61
# File 'tmpsets/set/mod001-01_core/all/collection.rb', line 58

def include_item? item
  key = item.is_a?(Card) ? item.cardname.key : item.to_name.key
  item_names.map { |name| name.to_name.key }.member? key
end

#insert_item(index, name) ⇒ Object



74
75
76
77
78
79
# File 'tmpsets/set/mod001-01_core/all/collection.rb', line 74

def insert_item index, name
  new_names = item_names
  new_names.delete name
  new_names.insert index, name
  self.content = new_names.join "\n"
end

#item_cards(_args = {}) ⇒ Object

FIXME: this is inconsistent with item_names



44
45
46
# File 'tmpsets/set/mod001-01_core/all/collection.rb', line 44

def item_cards _args={} # FIXME: this is inconsistent with item_names
  [self]
end

#item_keys(args = {}) ⇒ Object



52
53
54
55
56
# File 'tmpsets/set/mod001-01_core/all/collection.rb', line 52

def item_keys args={}
  item_names(args).map do |item|
    item.to_name.key
  end
end

#item_names(_args = {}) ⇒ Object



40
41
42
# File 'tmpsets/set/mod001-01_core/all/collection.rb', line 40

def item_names _args={}
  format._render_raw.split /[,\n]/
end

#item_typeObject



48
49
50
# File 'tmpsets/set/mod001-01_core/all/collection.rb', line 48

def item_type
  nil
end