Module: Card::Set::Basket

Included in:
Card::Set, Format::AbstractFormat
Defined in:
card/lib/card/set/basket.rb

Overview

The purpose of a basket it that you can throw something in from the same set in another mod. A basket can be defined on a format or directly on a set

@example: # mod/core/set/self/head.rb: basket :basket_on_set

format :html do basket :js_tags # only available in HtmlFormat view :core { output basket(:js_tags) } end

# mod/shell/set/self/head.rb: add_to_basket :basket_on_set, ‘hello world’

format :html do add_to_basket :js_tags “” add_to_basket :js_tags do |format_obj| format_obj.render_special_view end end

Instance Method Summary collapse

Instance Method Details

#abstract_basket(name) ⇒ Object

Define a basket in an abstract set



38
39
40
41
42
43
44
45
46
# File 'card/lib/card/set/basket.rb', line 38

def abstract_basket name
  # the basket has to be defined on the including set
  # (instead on the set itself)

  define_singleton_method :included do |host|
    super(host)
    host.basket name
  end
end

#add_to_basket(name, content = nil, &block) ⇒ Object



48
49
50
51
# File 'card/lib/card/set/basket.rb', line 48

def add_to_basket name, content=nil, &block
  content ||= block
  send("#{name}_content").send "<<", content
end

#basket(name) ⇒ Object

Define a basket in a set or format



27
28
29
30
31
32
33
34
35
# File 'card/lib/card/set/basket.rb', line 27

def basket name
  mattr_accessor "#{name}_content"
  send("#{name}_content=", [])
  define_method name do
    send("#{name}_content").map do |item|
      item.respond_to?(:call) ? item.call(self) : item
    end
  end
end

#unshift_basket(name, content = nil, &block) ⇒ Object



53
54
55
56
# File 'card/lib/card/set/basket.rb', line 53

def unshift_basket name, content=nil, &block
  content ||= block
  send("#{name}_content").unshift content
end