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_active_job, define_event_method, define_event_perform_later_method, define_on_format, ensure_set, event, extended, format, process_base_module_list, process_base_modules, register_set, register_set_format, shortname, view, write_tmp_file

Instance Method Details

#add_item(item) ⇒ Object



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

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

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



128
129
130
131
132
# File 'tmpsets/set/mod001-01_core/all/collection.rb', line 128

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

#drop_item(item) ⇒ Object



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

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

#extended_item_cards(context = nil) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'tmpsets/set/mod001-01_core/all/collection.rb', line 89

def extended_item_cards context = nil
  context = (context ? context.cardname : self.cardname)
  args={ :limit=>'' }
  items = self.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



111
112
113
# File 'tmpsets/set/mod001-01_core/all/collection.rb', line 111

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

#extended_list(context = nil) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
# File 'tmpsets/set/mod001-01_core/all/collection.rb', line 115

def extended_list context = nil
  context = (context ? context.cardname : self.cardname)
  args={ :limit=>'' }
  self.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)


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

def include_item? item
  key = if Card === item
    item.cardname.key
  else
    item.to_name.key
  end
  item_names.map{|name| name.to_name.key}.member? key
end

#insert_item(index, name) ⇒ Object



81
82
83
84
85
86
# File 'tmpsets/set/mod001-01_core/all/collection.rb', line 81

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



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

def item_cards(args={})  ## FIXME this is inconsistent with item_names
  [self]
end

#item_keys(args = {}) ⇒ Object



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

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

#item_names(args = {}) ⇒ Object



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

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

#item_typeObject



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

def item_type
  nil
end