Class: ActiveTools::Misc::UniqContent::Storage

Inherits:
Object
  • Object
show all
Defined in:
lib/active_tools/misc/uniq_content.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeStorage

Returns a new instance of Storage.



9
10
11
12
# File 'lib/active_tools/misc/uniq_content.rb', line 9

def initialize
  @content = ::ActiveSupport::OrderedHash.new {|h,k| h[k] = []}
  @hashes = ::ActiveSupport::OrderedHash.new {|h,k| h[k] = []}
end

Instance Attribute Details

#contentObject (readonly)

Returns the value of attribute content.



5
6
7
# File 'lib/active_tools/misc/uniq_content.rb', line 5

def content
  @content
end

Instance Method Details

#append_content(value, key = nil) ⇒ Object Also known as: append_content!

Called by content_for



25
26
27
# File 'lib/active_tools/misc/uniq_content.rb', line 25

def append_content(value, key = nil)
  @content[key] |= Array(value)
end

#get_content(key = nil) ⇒ Object

Called by _layout_for to read stored values.



15
16
17
# File 'lib/active_tools/misc/uniq_content.rb', line 15

def get_content(key = nil)
  @content[key]
end

#remember(value, key = nil) ⇒ Object



34
35
36
37
38
39
# File 'lib/active_tools/misc/uniq_content.rb', line 34

def remember(value, key = nil)
  unless remembered?(value, key)
    @hashes[key] << value.hash
    value
  end
end

#remembered?(value, key = nil) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/active_tools/misc/uniq_content.rb', line 41

def remembered?(value, key = nil)
  @hashes[key].include?(value.hash)
end

#render_content(key = nil) ⇒ Object



30
31
32
# File 'lib/active_tools/misc/uniq_content.rb', line 30

def render_content(key = nil)
  @content[key].join("\n").html_safe
end

#set_content(value, key = nil) ⇒ Object

Called by each renderer object to set the layout contents.



20
21
22
# File 'lib/active_tools/misc/uniq_content.rb', line 20

def set_content(value, key = nil)
  @content[key] = Array(value)
end