Class: CobblesHelper::Cobbler

Inherits:
Object
  • Object
show all
Defined in:
app/helpers/cobbles_helper.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(template, name, options = {}) ⇒ Cobbler

Returns a new instance of Cobbler.


27
28
29
30
31
32
# File 'app/helpers/cobbles_helper.rb', line 27

def initialize(template, name, options = {})
  @template = template
  @name = name
  @items = []
  @order = options[:order] || []
end

Instance Attribute Details

#itemsObject (readonly)

Returns the value of attribute items


25
26
27
# File 'app/helpers/cobbles_helper.rb', line 25

def items
  @items
end

#nameObject (readonly)

Returns the value of attribute name


25
26
27
# File 'app/helpers/cobbles_helper.rb', line 25

def name
  @name
end

#templateObject (readonly)

Returns the value of attribute template


25
26
27
# File 'app/helpers/cobbles_helper.rb', line 25

def template
  @template
end

Instance Method Details

#any?Boolean

Returns:

  • (Boolean)

42
43
44
# File 'app/helpers/cobbles_helper.rb', line 42

def any?
  @items.any?
end

#cobble(name, options = {}, &block) ⇒ Object


34
35
36
37
38
39
40
# File 'app/helpers/cobbles_helper.rb', line 34

def cobble(name, options = {}, &block)
  if @items.detect { |i| i.name.to_s == name.to_s }
    raise "Already taken. You already use #{name.inspect}"
  end
  raise "Need a block for #{name} in cobbler #{@name}" unless block_given?
  @items << Cobble.new(self, name, options, &block)
end

#each(&block) ⇒ Object


59
60
61
# File 'app/helpers/cobbles_helper.rb', line 59

def each(&block)
  @items.each(&block)
end

#sort!Object


46
47
48
49
50
51
52
53
54
55
56
57
# File 'app/helpers/cobbles_helper.rb', line 46

def sort!
  @order.each_with_index do |name, index|
    @items.select do |cobble|
      cobble.id.to_s == name.to_s
    end.each do |cobble|
      cobble.position = index + 1
    end
  end
  @items.sort! do |a, b|
    a.position <=> b.position
  end
end