bithour 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 6d5341e368672b91a575569892899f91f13366b4
4
+ data.tar.gz: dde5081f952011848aa2a85f05241594d3a98bc2
5
+ SHA512:
6
+ metadata.gz: 955da61af164da5fba08b98a5094e2e467321c05035620d1db56cdb50f401a1109a2da98b1cfce05d8d7dfa03ab350321fd848228193a82e3dbf351b8b88ab63
7
+ data.tar.gz: 8f24b62a7543639a6e4039adbdc8ce24d81ef331a3404329da82af10a12cbcb8fd808627d03004504ae88286415148126282a071b9c99a06681b11f5a1a0dfd1
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.0.0
4
+ - 2.1
5
+ - 2.2
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in bithour.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Masafumi Yokoyama <myokoym@gmail.com>
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,41 @@
1
+ # Bithour [![Gem Version](https://badge.fury.io/rb/bithour.svg)](http://badge.fury.io/rb/bithour) [![Build Status](https://secure.travis-ci.org/myokoym/bithour.png?branch=master)](http://travis-ci.org/myokoym/bithour)
2
+
3
+ A library for range of hours in bits.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'bithour'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install bithour
20
+
21
+ ## Usage
22
+
23
+ See [test/test-range.rb](https://github.com/myokoym/bithour/blob/master/test/test-range.rb).
24
+
25
+ ## Authors
26
+
27
+ * Masafumi Yokoyama `<myokoym@gmail.com>`
28
+
29
+ ## License
30
+
31
+ MIT License.
32
+
33
+ See LICENSE.txt for details.
34
+
35
+ ## Contributing
36
+
37
+ 1. Fork it ( https://github.com/myokoym/bithour/fork )
38
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
39
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
40
+ 4. Push to the branch (`git push origin my-new-feature`)
41
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ desc "Run test"
4
+ task :test do
5
+ Bundler::GemHelper.install_tasks
6
+ ruby("test/run-test.rb")
7
+ end
8
+
9
+ task :default => :test
data/bithour.gemspec ADDED
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'bithour/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "bithour"
8
+ spec.version = Bithour::VERSION
9
+ spec.authors = ["Masafumi Yokoyama"]
10
+ spec.email = ["myokoym@gmail.com"]
11
+
12
+ spec.summary = "A library for range of hours in bits."
13
+ spec.description = spec.summary
14
+ spec.homepage = "https://github.com/myokoym/bithour"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0")
18
+ spec.bindir = "bin"
19
+ spec.executables = spec.files.grep(%r{^bin/}) {|f| File.basename(f)}
20
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
21
+ spec.files -= spec.test_files
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_development_dependency("test-unit")
25
+ spec.add_development_dependency("test-unit-notify")
26
+ spec.add_development_dependency("bundler")
27
+ spec.add_development_dependency("rake")
28
+ end
data/lib/bithour.rb ADDED
@@ -0,0 +1,5 @@
1
+ require "bithour/range"
2
+ require "bithour/version"
3
+
4
+ module Bithour
5
+ end
@@ -0,0 +1,42 @@
1
+ module Bithour
2
+ class Range
3
+ def initialize(max=24)
4
+ @hour_bits = 0
5
+ @max = max
6
+ end
7
+
8
+ def add(hours)
9
+ update(hours, "1")
10
+ end
11
+
12
+ def remove(hours)
13
+ update(hours, "0")
14
+ end
15
+
16
+ def include?(hour)
17
+ @hour_bits[hour] == 1
18
+ end
19
+
20
+ def to_a
21
+ bits = ("%0#{@max}d" % @hour_bits.to_s(2)).reverse.split(//)
22
+ hours = bits.collect.with_index {|hour, i| [i, hour]}
23
+ hours.select! {|hour| hour[1] == "1"}
24
+ hours.collect {|hour, bit| hour}
25
+ end
26
+
27
+ private
28
+ def update(_hours, bit)
29
+ if _hours.respond_to?(:each)
30
+ hours = _hours
31
+ else
32
+ hours = [_hours]
33
+ end
34
+ hour_str = "%0#{@max}d" % @hour_bits.to_s(2)
35
+ hour_str.reverse!
36
+ hours.each do |i|
37
+ hour_str[i % @max] = bit
38
+ end
39
+ @hour_bits = hour_str.reverse.to_i(2)
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,3 @@
1
+ module Bithour
2
+ VERSION = "0.0.1"
3
+ end
data/test/run-test.rb ADDED
@@ -0,0 +1,11 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "test-unit"
4
+ require "test/unit/notify"
5
+
6
+ base_dir = File.expand_path("..", File.dirname(__FILE__))
7
+ lib_dir = File.join(base_dir, "lib")
8
+ test_dir = File.join(base_dir, "test")
9
+ $LOAD_PATH.unshift(lib_dir)
10
+
11
+ exit Test::Unit::AutoRunner.run(true, test_dir)
@@ -0,0 +1,50 @@
1
+ require "bithour/range"
2
+
3
+ class RangeTest < Test::Unit::TestCase
4
+ def setup
5
+ @range = Bithour::Range.new
6
+ end
7
+
8
+ def test_new
9
+ assert_not_nil(@range)
10
+ end
11
+
12
+ def test_add_single
13
+ @range.add(0)
14
+ assert_equal([0], @range.to_a)
15
+ end
16
+
17
+ def test_add_multiple
18
+ @range.add(2..4)
19
+ assert_equal([2, 3, 4], @range.to_a)
20
+ end
21
+
22
+ def test_add_two_times
23
+ @range.add(0)
24
+ @range.add(2)
25
+ assert_equal([0, 2], @range.to_a)
26
+ end
27
+
28
+ def test_remove
29
+ @range.add(0)
30
+ @range.add(2..4)
31
+ @range.remove([2, 3])
32
+ assert_equal([0, 4], @range.to_a)
33
+ end
34
+
35
+ def test_include
36
+ @range.add(0)
37
+ assert_true(@range.include?(0))
38
+ assert_false(@range.include?(1))
39
+ end
40
+
41
+ def test_max
42
+ @range.add(24)
43
+ assert_true(@range.include?(0))
44
+ end
45
+
46
+ def test_to_a
47
+ @range.add(0..1)
48
+ assert_equal([0, 1], @range.to_a)
49
+ end
50
+ end
metadata ADDED
@@ -0,0 +1,115 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bithour
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Masafumi Yokoyama
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-03-16 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: test-unit
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: test-unit-notify
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: A library for range of hours in bits.
70
+ email:
71
+ - myokoym@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".travis.yml"
78
+ - Gemfile
79
+ - LICENSE.txt
80
+ - README.md
81
+ - Rakefile
82
+ - bithour.gemspec
83
+ - lib/bithour.rb
84
+ - lib/bithour/range.rb
85
+ - lib/bithour/version.rb
86
+ - test/run-test.rb
87
+ - test/test-range.rb
88
+ homepage: https://github.com/myokoym/bithour
89
+ licenses:
90
+ - MIT
91
+ metadata: {}
92
+ post_install_message:
93
+ rdoc_options: []
94
+ require_paths:
95
+ - lib
96
+ required_ruby_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ required_rubygems_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ requirements: []
107
+ rubyforge_project:
108
+ rubygems_version: 2.2.2
109
+ signing_key:
110
+ specification_version: 4
111
+ summary: A library for range of hours in bits.
112
+ test_files:
113
+ - test/run-test.rb
114
+ - test/test-range.rb
115
+ has_rdoc: