Class: Dao::Messages

Inherits:
Array
  • Object
show all
Defined in:
lib/dao/messages.rb

Defined Under Namespace

Classes: Message

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Array

#as_dao, #to_dao

Constructor Details

#initialize(*args) ⇒ Messages

Returns a new instance of Messages.



22
23
24
# File 'lib/dao/messages.rb', line 22

def initialize(*args)
  @object = args.shift
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



97
98
99
100
101
102
103
104
105
# File 'lib/dao/messages.rb', line 97

def method_missing(method, *args, &block)
  if block.nil?
    type = method.to_s
    message = args.join
    add(message, type)
  else
    super
  end
end

Instance Attribute Details

#objectObject

Returns the value of attribute object.



20
21
22
# File 'lib/dao/messages.rb', line 20

def object
  @object
end

Class Method Details

.each(*args, &block) ⇒ Object



76
77
78
79
80
81
# File 'lib/dao/messages.rb', line 76

def Messages.each(*args, &block)
  args.flatten.compact.each do |arg|
    message = arg.is_a?(Message) ? arg : Message.new(arg)
    block.call(message)
  end
end

.for(object) ⇒ Object



26
27
28
# File 'lib/dao/messages.rb', line 26

def Messages.for(object)
  new(object)
end

.to_html(*args, &block) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/dao/messages.rb', line 30

def Messages.to_html(*args, &block)
  if block
    define_method(:to_html, &block)
  else
    at_least_one = false

    html =
      div_(:class => "dao dao-messages"){
        Messages.each(*args) do |message|
          at_least_one = true
          slug = Slug.for(message.type)

          div_(:class => "dao dao-message alert alert-block alert-#{ slug }"){
            tagz << Tagz.html_safe(message) << ' '

            a_(:href => "#", :class => "close", :data_dismiss => "alert", :onClick => "javascript:$(this).closest('div').remove();false;"){
              Tagz.html_safe('&times;')
            }
          }
        end
      }

    at_least_one ? html : ''
  end
end

.to_text(*args) ⇒ Object



64
65
66
67
68
69
70
# File 'lib/dao/messages.rb', line 64

def Messages.to_text(*args)
  to_text = []
  Messages.each(*args) do |message|
    to_text.push([message.type, message].compact.join(': '))
  end
  to_text.join("\n")
end

Instance Method Details

#add(message, type = nil) ⇒ Object



92
93
94
95
# File 'lib/dao/messages.rb', line 92

def add(message, type = nil)
  push(Message.new(message, type))
  self
end

#to_html(*args) ⇒ Object



56
57
58
# File 'lib/dao/messages.rb', line 56

def to_html(*args)
  Messages.to_html(self)
end

#to_s(format = :html, *args, &block) ⇒ Object



60
61
62
# File 'lib/dao/messages.rb', line 60

def to_s(*args)
  to_html(*args)
end

#to_textObject



72
73
74
# File 'lib/dao/messages.rb', line 72

def to_text
  Messages.to_text(self)
end