Class: Waylon::RSpec::TestChannel
- Inherits:
-
Object
- Object
- Waylon::RSpec::TestChannel
- Defined in:
- lib/waylon/rspec/test_channel.rb
Overview
The TestChannel
Instance Attribute Summary collapse
-
#id ⇒ Object
readonly
Returns the value of attribute id.
Class Method Summary collapse
-
.all ⇒ Array<TestChannel>
Simple way to list all TestChannels.
-
.find_by_name(name) ⇒ TestChannel?
Looks up an existing TestChannel by name.
-
.find_or_create(name) ⇒ TestChannel
Always provides a TestChannel, either by finding an existing or creating a new one.
Instance Method Summary collapse
-
#created_at ⇒ Time
Easy access to when the TestChannel was created.
-
#details ⇒ Hash
private
Lazily provides the details for a TestUser.
-
#initialize(channel_id, details = {}) ⇒ TestChannel
constructor
A new instance of TestChannel.
-
#name ⇒ String
The name of the TestChannel.
-
#post_message(content, from: TestUser.whoami) ⇒ Message
Send a TestMessage to a TestChannel.
Constructor Details
#initialize(channel_id, details = {}) ⇒ TestChannel
Returns a new instance of TestChannel.
37 38 39 40 |
# File 'lib/waylon/rspec/test_channel.rb', line 37 def initialize(channel_id, details = {}) @id = channel_id.to_i @details = details end |
Instance Attribute Details
#id ⇒ Object (readonly)
Returns the value of attribute id.
7 8 9 |
# File 'lib/waylon/rspec/test_channel.rb', line 7 def id @id end |
Class Method Details
.all ⇒ Array<TestChannel>
Simple way to list all TestChannels
11 12 13 |
# File 'lib/waylon/rspec/test_channel.rb', line 11 def self.all TestSense.channel_list.each_index.map { |id| new(id) } end |
.find_by_name(name) ⇒ TestChannel?
Looks up an existing TestChannel by name
30 31 32 33 |
# File 'lib/waylon/rspec/test_channel.rb', line 30 def self.find_by_name(name) channel_id = TestSense.channel_list.index { |channel| channel[:name] == name } channel_id ? new(channel_id) : nil end |
.find_or_create(name) ⇒ TestChannel
Always provides a TestChannel, either by finding an existing or creating a new one
17 18 19 20 21 22 23 24 25 26 |
# File 'lib/waylon/rspec/test_channel.rb', line 17 def self.find_or_create(name) existing_channel = find_by_name(name) if existing_channel existing_channel else channel_details = { name:, created_at: Time.now } TestSense.channel_list << channel_details new(TestSense.channel_list.size - 1) end end |
Instance Method Details
#created_at ⇒ Time
Easy access to when the TestChannel was created
44 45 46 |
# File 'lib/waylon/rspec/test_channel.rb', line 44 def created_at details[:created_at] end |
#details ⇒ Hash
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Lazily provides the details for a TestUser
77 78 79 80 |
# File 'lib/waylon/rspec/test_channel.rb', line 77 def details @details = TestSense.room_list[id] if @details.empty? @details.dup end |
#name ⇒ String
The name of the TestChannel
50 51 52 |
# File 'lib/waylon/rspec/test_channel.rb', line 50 def name details[:name] end |
#post_message(content, from: TestUser.whoami) ⇒ Message
Send a TestMessage to a TestChannel
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/waylon/rspec/test_channel.rb', line 57 def (content, from: TestUser.whoami) msg = if content.is_a?(Message) content.text else content end msg_details = { user_id: from.id, text: msg, type: :channel, channel_id: id, created_at: Time.now } TestSense. << msg_details TestMessage.new(TestSense..size - 1) end |