dancer 0.1.0 → 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e269b43ddeb6eefae3fb07baf0d920e6d6420192
4
- data.tar.gz: acc613349321fe054ab4d40d8c920958512bcc8b
3
+ metadata.gz: 5cf70523bc4b8d7dccaf7597e7abc8fc39e1106a
4
+ data.tar.gz: 401eb016fb3f5a36cf4ff19beecaa84c91161644
5
5
  SHA512:
6
- metadata.gz: 78a6b1ddda62b4ff57d0cc2e51bef5c8a4d4b06fd19951adb0db89e76bb540a6245468cce8a3d873eeb399b8849cc76acfb857ed008c64f77e1ee856e2a85c86
7
- data.tar.gz: 0dbf6789d4adde89e0b552e553ddc6a83f34f7e7c8ae501cd1278888615f27ae6bd19656976688cba1a0dcf81ab8a8310042980fa0a34c3ee96929c292da6a3f
6
+ metadata.gz: 484d078cfaed881a026ad2652a755a8362e2aa5c9dfe6296bc2acb230e214240e30ef912af75c90f8495009090d0331bfb3cbbcc8545db7c8aed48421dff9e26
7
+ data.tar.gz: 136d0d1c88c0d1bc728ec5c8e4693209145aadd11e1ec68ca35710def91e4bb9580925320d59bb8b4db26b6a185f85263a1da08a44b109f2ba1c15a0944403f7
data/Gemfile CHANGED
@@ -1,4 +1,3 @@
1
1
  source "https://rubygems.org"
2
2
 
3
- # Specify your gem's dependencies in dancer.gemspec
4
3
  gemspec
data/README.md CHANGED
@@ -1,8 +1,7 @@
1
1
  # Dancer
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/dancer`. To experiment with that code, run `bin/console` for an interactive prompt.
3
+ A small library to move forward or backward through Time in predefined steps.
4
4
 
5
- TODO: Delete this and the text above, and describe your gem
6
5
 
7
6
  ## Installation
8
7
 
@@ -20,9 +19,85 @@ Or install it yourself as:
20
19
 
21
20
  $ gem install dancer
22
21
 
22
+
23
23
  ## Usage
24
24
 
25
- TODO: Write usage instructions here
25
+ ### Basic usage
26
+
27
+ ```ruby
28
+ now = Time.now
29
+
30
+ # takes 4 arguments: `start_at`, `end_at`, `step`, and `exclude_end` (like Range).
31
+ dancer = Dancer.new(now, now+86400, 3600, true)
32
+
33
+ dancer.each_time do |time|
34
+ # do something...
35
+ end
36
+
37
+ dancer.each_range do |range|
38
+ # do something...
39
+ end
40
+ ```
41
+
42
+ ### Usage with a Range
43
+
44
+ ```ruby
45
+ # takes 2 arguments: `range` and `step`.
46
+ dancer = Dancer.range(now..(now+86400), 3600)
47
+
48
+ dancer.each_time do |time|
49
+ # do something...
50
+ end
51
+ ```
52
+
53
+ ### Usage with an Extent
54
+
55
+ ```ruby
56
+ # takes 4 arguments: `start_at`, `size`, `step` and `exclude_end`.
57
+ dancer = Dancer.extent(now, 24, 3600, true)
58
+
59
+ dancer.each_time do |time|
60
+ # do something...
61
+ end
62
+ ```
63
+
64
+ ### Usage with an array of times
65
+
66
+ ```ruby
67
+ # takes 3 arguments: `keys`, `step`, and `exclude_end`.
68
+ dancer = Dancer.extent(now, 24, 3600, true)
69
+
70
+ dancer.each_time do |time|
71
+ # do something...
72
+ end
73
+ ```
74
+
75
+ ### Usage with a predefined step size
76
+
77
+ ```ruby
78
+ TradingPeriod = Dancer::Defined.new(1800)
79
+
80
+ # accepts the same 4 methods but with `step` omitted from each
81
+ dancer = TradingPeriod.extent(now, 24, true)
82
+
83
+ dancer.each_time do |time|
84
+ # do something...
85
+ end
86
+ ```
87
+
88
+ ### Other methods
89
+
90
+ ```ruby
91
+ # takes 4 arguments: `start_at`, `end_at`, `step`, and `exclude_end` (like Range).
92
+ dancer = Dancer.new(now, now+86400, 3600, true)
93
+
94
+ dancer.size
95
+ > 24
96
+
97
+ dancer.duration
98
+ > 86400
99
+ ```
100
+
26
101
 
27
102
  ## Development
28
103
 
@@ -30,6 +105,7 @@ After checking out the repo, run `bin/setup` to install dependencies. Then, run
30
105
 
31
106
  To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
107
 
108
+
33
109
  ## Contributing
34
110
 
35
111
  Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/dancer.
@@ -38,4 +114,3 @@ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERN
38
114
  ## License
39
115
 
40
116
  The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
41
-
@@ -9,8 +9,8 @@ Gem::Specification.new do |spec|
9
9
  spec.authors = ["Steve Hoeksema"]
10
10
  spec.email = ["steve@kotiri.com"]
11
11
 
12
- spec.summary = "Dancer steps through Time"
13
- spec.description = "Dancer steps through Time"
12
+ spec.summary = "Dancer steps in Time"
13
+ spec.description = "A small library to move forward or backward through Time in predefined steps"
14
14
  spec.homepage = "https://github.com/steveh/dancer"
15
15
  spec.license = "MIT"
16
16
 
@@ -23,4 +23,5 @@ Gem::Specification.new do |spec|
23
23
  spec.add_development_dependency "rake"
24
24
  spec.add_development_dependency "guard-rspec"
25
25
  spec.add_development_dependency "simplecov"
26
+ spec.add_development_dependency "pry-byebug"
26
27
  end
@@ -38,7 +38,7 @@ class Dancer
38
38
  def size
39
39
  return unless bounded?
40
40
 
41
- ((end_at.public_send(operator, 1) - start_at) / step).abs
41
+ ((end_at.public_send(operator, offset) - start_at) / step).abs.to_i
42
42
  end
43
43
 
44
44
  alias count size
@@ -48,7 +48,11 @@ class Dancer
48
48
  def range
49
49
  return unless bounded?
50
50
 
51
- start_at..end_at
51
+ if exclude_end?
52
+ start_at...end_at
53
+ else
54
+ start_at..end_at
55
+ end
52
56
  end
53
57
 
54
58
  # Enumerator for each start time
@@ -71,9 +75,16 @@ class Dancer
71
75
 
72
76
  current = start_at
73
77
  while current.public_send(comparator, end_at)
74
- current_end_at = current.public_send(operator, step) - (0.public_send(operator, 1))
75
- current_range = current..current_end_at
78
+ current_end_at = current.public_send(operator, step) - (0.public_send(operator, offset))
79
+
80
+ current_range = if exclude_end?
81
+ current...current_end_at
82
+ else
83
+ current..current_end_at
84
+ end
85
+
76
86
  yield current_range
87
+
77
88
  current = current.public_send(operator, step)
78
89
  end
79
90
  self
@@ -83,20 +94,26 @@ class Dancer
83
94
  def duration
84
95
  return unless bounded?
85
96
 
86
- (end_at - start_at).abs + 1
97
+ (end_at - start_at).abs.to_i + offset
87
98
  end
88
99
 
89
100
  alias to_i duration
90
101
 
91
- def bounded?
92
- start_at && end_at
93
- end
94
-
95
102
  def to_s
96
- "#{start_at.inspect}..#{end_at.inspect} (step: #{step})"
103
+ "#{start_at.inspect}#{exclude_end? ? "..." : ".."}#{end_at.inspect} (step: #{step})"
97
104
  end
98
105
 
99
106
  def inspect
100
107
  "#<Dancer #{to_s}>"
101
108
  end
109
+
110
+ protected
111
+
112
+ def bounded?
113
+ start_at && end_at
114
+ end
115
+
116
+ def offset
117
+ exclude_end? ? 0 : 1
118
+ end
102
119
  end
@@ -11,15 +11,24 @@ class Dancer
11
11
 
12
12
  # Create a timeslice from a start time and a number of a points
13
13
  def self.extent(start_at, size, step, exclude_end = false)
14
- end_at = start_at + (step * size) - 1
14
+ offset = exclude_end ? 0 : 1
15
+
16
+ end_at = if size.negative?
17
+ start_at + (step * size) + offset
18
+ else
19
+ start_at + (step * size) - offset
20
+ end
15
21
 
16
22
  new(start_at, end_at, step, exclude_end)
17
23
  end
18
24
 
19
25
  # Create a timeslice from a list of start times
20
26
  def self.keys(keys, step, exclude_end = false)
27
+ offset = exclude_end ? 0 : 1
28
+
21
29
  start_at = keys.min
22
- end_at = keys.max ? (keys.max + step - 1) : nil
30
+
31
+ end_at = keys.max ? (keys.max + (step * offset) - offset) : nil
23
32
 
24
33
  new(start_at, end_at, step, exclude_end)
25
34
  end
@@ -1,3 +1,3 @@
1
1
  class Dancer
2
- VERSION = "0.1.0"
2
+ VERSION = "0.9.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dancer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.9.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Steve Hoeksema
@@ -66,7 +66,22 @@ dependencies:
66
66
  - - ">="
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
- description: Dancer steps through Time
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry-byebug
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: A small library to move forward or backward through Time in predefined
84
+ steps
70
85
  email:
71
86
  - steve@kotiri.com
72
87
  executables: []
@@ -115,5 +130,5 @@ rubyforge_project:
115
130
  rubygems_version: 2.5.1
116
131
  signing_key:
117
132
  specification_version: 4
118
- summary: Dancer steps through Time
133
+ summary: Dancer steps in Time
119
134
  test_files: []