Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/archan937/slot_machine
Ruby gem for matching available slots (time slots are also supported)
https://github.com/archan937/slot_machine
Last synced: about 2 months ago
JSON representation
Ruby gem for matching available slots (time slots are also supported)
- Host: GitHub
- URL: https://github.com/archan937/slot_machine
- Owner: archan937
- License: mit
- Created: 2012-09-15T12:22:03.000Z (over 12 years ago)
- Default Branch: master
- Last Pushed: 2020-03-29T23:22:25.000Z (almost 5 years ago)
- Last Synced: 2024-04-26T14:05:32.778Z (8 months ago)
- Language: Ruby
- Homepage:
- Size: 27.3 KB
- Stars: 24
- Watchers: 4
- Forks: 9
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.rdoc
- License: MIT-LICENSE
Awesome Lists containing this project
README
# SlotMachine [![Build Status](https://secure.travis-ci.org/archan937/slot_machine.png)](http://travis-ci.org/archan937/slot_machine)
Ruby gem for matching available slots (time slots are also supported)
## Introduction
One of the classic programming problems is the determination of time slot availability. Very often this is used within scheduling / calendar programs. SlotMachine is a very small Ruby gem which can do the job for you. It does not only focuses on time slots, but also slots in general.
## Installation
### Add `SlotMachine` to your Gemfile
gem "slot_machine"
### Install the gem dependencies
$ bundle
## Usage
### SlotMachine::Slot module
The core implementation of SlotMachine is written in `SlotMachine::Slot`. A module which has to be included within a Ruby class.
SlotMachine provides the classes `Slot` and `TimeSlot` of which `Slot` is defined as follows:class Slot
include SlotMachine::Slot
endClean and simple. Cool, huh? `TimeSlot` also includes the `SlotMachine::Slot` module and overrides a few methods to provide time slot specific behaviour.
### Slot
`SlotMachine` is pretty straightforward. A slot consists of a start and an end (integer) value. You can define a slot as follows:
[1] pry(main)> s = Slot.new 0..20
=> #
[2] pry(main)> s.start
=> 0
[3] pry(main)> s.end
=> 20After having defined a slot, you can match available slots within that slot. Let's say that you want to calculate available slots with a length of `10`:
[4] pry(main)> s.match 10
=> [#, #]The `match` method returns an array with the available slots. At default, the `Slot` searches for slots with an interval size of `10` and thus `0..10` and `10..20` are matched.
You can change the interval by passing it as a second argument. Using an interval size of `2`:
[5] pry(main)> s.match 10, 2
=> [#,
#,
#,
#,
#,
#]You can also match a slot with another slot instance. This returns the slot in which both slots overlap each other:
[5] pry(main)> s.match 15..30 #=> you can also use: s.match Slot.new(15..30)
=> [#]
[6] s.match Slot.new(21..30) #=> no overlap
=> []### TimeSlot
The `TimeSlot` class is (of course) similar to the `Slot` class, but accepts military times (e.g. 1300 for 1:00 pm). In other words, it only counts from `0` untill `59` within a `0` to `99` range. Also, the default interval size is `15`.
An example:
[1] pry(main)> ts = TimeSlot.new 1015..1045
=> #
[2] pry(main)> ts.match 10
=> [#, #]
[3] pry(main)> ts.match 10, 5
=> [#,
#,
#,
#,
#]
[4] pry(main)> ts.match 1038..1100 #=> you can also use: ts.match TimeSlot.new(1038..1100)
=> [#]## Using the console
The SlotMachine repo is provided with `script/console` which you can use for development / testing purposes.
Run the following command in your console:
$ script/console
Loading development environment (SlotMachine 0.1.0)
[1] pry(main)> s = Slot.new 0..25
=> #
[2] pry(main)> s.match 15
=> [#, #]
[3] pry(main)> s.match 10, 4
=> [#,
#,
#,
#]
[4] pry(main)> ts = TimeSlot.new 1015..1045
=> #
[5] pry(main)> ts.match 10
=> [#, #]
[6] pry(main)> ts.match 10, 5
=> [#,
#,
#,
#,
#]## Testing
Run the following command for testing:
$ rake
You can also run a single test file:
$ ruby test/unit/test_time_slot.rb
## Closing words
Well that's about it! Pretty straightforward, right? Have fun playing with the `SlotMachine`! ^^
## TODO
* Update documentation regarding Slots, TimeSlots and Time objects
## Contact me
For support, remarks and requests, please mail me at [[email protected]](mailto:[email protected]).
## License
Copyright (c) 2018 Paul Engel, released under the MIT license
[http://holder.nl](http://holder.nl) - [http://codehero.es](http://codehero.es) - [http://gettopup.com](http://gettopup.com) - [http://github.com/archan937](http://github.com/archan937) - [http://twitter.com/archan937](http://twitter.com/archan937) - [[email protected]](mailto:[email protected])
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.