Class: Banacle::Slack::Action

Inherits:
Struct
  • Object
show all
Defined in:
lib/banacle/slack.rb

Defined Under Namespace

Classes: ValidationError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Action

Returns a new instance of Action.



109
110
111
112
113
114
# File 'lib/banacle/slack.rb', line 109

def initialize(*args)
  super
  self.set_default!
  self.validate!
  self
end

Instance Attribute Details

#confirmObject

Returns the value of attribute confirm

Returns:

  • (Object)

    the current value of confirm



86
87
88
# File 'lib/banacle/slack.rb', line 86

def confirm
  @confirm
end

#nameObject

Returns the value of attribute name

Returns:

  • (Object)

    the current value of name



86
87
88
# File 'lib/banacle/slack.rb', line 86

def name
  @name
end

#styleObject

Returns the value of attribute style

Returns:

  • (Object)

    the current value of style



86
87
88
# File 'lib/banacle/slack.rb', line 86

def style
  @style
end

#textObject

Returns the value of attribute text

Returns:

  • (Object)

    the current value of text



86
87
88
# File 'lib/banacle/slack.rb', line 86

def text
  @text
end

#typeObject

Returns the value of attribute type

Returns:

  • (Object)

    the current value of type



86
87
88
# File 'lib/banacle/slack.rb', line 86

def type
  @type
end

#valueObject

Returns the value of attribute value

Returns:

  • (Object)

    the current value of value



86
87
88
# File 'lib/banacle/slack.rb', line 86

def value
  @value
end

Class Method Details

.approve_buttonObject



89
90
91
# File 'lib/banacle/slack.rb', line 89

def self.approve_button
  self.build_button('approve', style: 'primary', confirm: Confirm.approve)
end

.build(value, style, type, confirm) ⇒ Object



105
106
107
# File 'lib/banacle/slack.rb', line 105

def self.build(value, style, type, confirm)
  self.new(name: value, text: value.capitalize, style: style, type: type, value: value, confirm: confirm)
end

.build_button(value, style: 'default', confirm: nil) ⇒ Object



101
102
103
# File 'lib/banacle/slack.rb', line 101

def self.build_button(value, style: 'default', confirm: nil)
  self.build(value, style, 'button', confirm)
end

.cancel_buttonObject



97
98
99
# File 'lib/banacle/slack.rb', line 97

def self.cancel_button
  self.build_button('cancel')
end

.reject_buttonObject



93
94
95
# File 'lib/banacle/slack.rb', line 93

def self.reject_button
  self.build_button('reject', style: 'danger')
end

Instance Method Details

#approved?Boolean

Returns:

  • (Boolean)


136
137
138
# File 'lib/banacle/slack.rb', line 136

def approved?
  self.value == 'approve'
end

#as_jsonObject



148
149
150
151
152
153
154
155
156
# File 'lib/banacle/slack.rb', line 148

def as_json
  self.to_h.tap do |h|
    if h[:confirm]
      h[:confirm] = h[:confirm].as_json
    else
      h.delete(:confirm)
    end
  end
end

#cancelled?Boolean

Returns:

  • (Boolean)


144
145
146
# File 'lib/banacle/slack.rb', line 144

def cancelled?
  self.value == 'cancel'
end

#rejected?Boolean

Returns:

  • (Boolean)


140
141
142
# File 'lib/banacle/slack.rb', line 140

def rejected?
  self.value == 'reject'
end

#set_default!Object



116
117
118
119
120
121
122
# File 'lib/banacle/slack.rb', line 116

def set_default!
  self.name ||= ''
  self.text ||= ''
  self.style ||= ''
  self.type ||= ''
  self.value ||= ''
end

#validate!Object



124
125
126
127
128
129
130
131
132
133
134
# File 'lib/banacle/slack.rb', line 124

def validate!
  %i(name text style type value).each do |label|
    unless self.send(label).is_a?(String)
      raise ValidationError.new("#{attr} must be String")
    end
  end

  if self.confirm && !self.confirm.is_a?(Confirm)
    raise ValidationError.new("confirm must be Slack::Confirm")
  end
end