Class: UmlautConfigurable::ResolveSectionsArray

Inherits:
Array
  • Object
show all
Defined in:
app/controllers/umlaut_configurable.rb

Overview

Used for `resolve_sections` config, like an Array but we add a some custom methods into the resolve_sections array, insert_section, remove_section. With a sub-class.

Instance Method Summary collapse

Instance Method Details

#deep_dupObject

Make deep_dup work


68
69
70
# File 'app/controllers/umlaut_configurable.rb', line 68

def deep_dup
  self.class.new.concat super
end

#ensure_order!(first, second) ⇒ Object

Deprecated. This was a silly confusing way to do this. See #remove and #insert below instead.


25
26
27
28
29
30
31
32
33
34
35
36
# File 'app/controllers/umlaut_configurable.rb', line 25

def ensure_order!(first, second)
  $stderr.puts "resolve_sections.ensure_order! is deprecated, please see resolve_sections.remove and resolve_sections.insert"

  list = self
  
  index1 = list.index {|s| s[:div_id].to_s == first.to_s}
  index2 = list.index {|s| s[:div_id].to_s == second.to_s}
  
  (list[index1], list[index2] = list[index2], list[index1]) if index1 && index2 && (index1 > index2)
  
  list
end

#insert_section(section_config, options = {}) ⇒ Object

Insert a section_config hash before or after an existing section, by the existing section's div_id

resolve_sections.insert_section({:div_id => "new"}, :after => "fulltext")
resolve_sections.insert_section(  resolve_sections.remove_section("document_deliver"), :before => "fulltext")

43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'app/controllers/umlaut_configurable.rb', line 43

def insert_section(section_config, options = {})
  list = self

  if options[:before]
    i = (list.find_index {|s| s[:div_id].to_s == options[:before].to_s}) || 0
    list.insert(i, section_config)
  elsif options[:after]
    i = (list.find_index {|s| s[:div_id].to_s == options[:after].to_s}) || (list.length - 1)
    list.insert(i + 1, section_config)
  else
    # just add at end of list
    list << section_config
  end
end

#map(*args, &block) ⇒ Object Also known as: collect

Make map work returning same class, to avoid breaking Hashie::Mash. Bah, this is a mess, yep.


74
75
76
# File 'app/controllers/umlaut_configurable.rb', line 74

def map(*args, &block)
  self.class.new.concat super
end

#remove_section(div_id) ⇒ Object

Remove a configuration block with a certain div_id from the configuration entirely, returning the removed configuration block (or nil if none exists). You can re-insert it with #insert if you like.


61
62
63
64
65
# File 'app/controllers/umlaut_configurable.rb', line 61

def remove_section(div_id)
  list = self
  i = list.find_index {|s| s[:div_id].to_s == div_id.to_s}
  return list.delete_at(i) if i
end