Class: Twilio::Rails::Phone::Tree::MessageSet

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/twilio/rails/phone/tree.rb

Instance Method Summary collapse

Constructor Details

#initialize(set) ⇒ MessageSet

Returns a new instance of MessageSet.



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/twilio/rails/phone/tree.rb', line 177

def initialize(set)
  @messages = []

  # This whole chunk here feels like an incorrect level of abstraction. That it should be the caller's responsbiility
  # to pass in the contents of `message:` and not a hash with `message:` as a key. But maybe it's ok to do it once
  # here so the callsites can be cleaner passthroughs without doing the checks over and over.
  if set.is_a?(Hash)
    set = set.symbolize_keys
    if set.key?(:message)
      raise Twilio::Rails::Phone::InvalidTreeError, "MessageSet should never receive a hash with any key other than :message but received #{ set }" if set.keys != [:message]
      set = set[:message]
    end
  end

  set = [set] unless set.is_a?(Array)
  set.each do |message|
    next nil if message.blank?

    if message.is_a?(Twilio::Rails::Phone::Tree::Message)
      @messages << message
    elsif message.is_a?(Proc)
      @messages << message
    elsif message.is_a?(String)
      @messages << Twilio::Rails::Phone::Tree::Message.new(say: message)
    elsif message.is_a?(Hash)
      @messages << Twilio::Rails::Phone::Tree::Message.new(**message.symbolize_keys)
    else
      raise Twilio::Rails::Phone::InvalidTreeError, "message value #{ message } is not valid"
    end
  end
end

Instance Method Details

#each(&block) ⇒ Object



209
210
211
# File 'lib/twilio/rails/phone/tree.rb', line 209

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

#firstObject



217
218
219
# File 'lib/twilio/rails/phone/tree.rb', line 217

def first
  @messages.first
end

#lastObject



221
222
223
# File 'lib/twilio/rails/phone/tree.rb', line 221

def last
  @messages.last
end

#lengthObject



213
214
215
# File 'lib/twilio/rails/phone/tree.rb', line 213

def length
  @messages.count
end