Permalink
Browse files

various updates

  • Loading branch information...
kristianmandrup committed Nov 19, 2012
1 parent 1953161 commit de58a8556566714e482af63ccc7351d88327cca4
View
@@ -1,15 +1,15 @@
-source :rubygems
+source 'http://rubygems.org'
gem 'chronic'
gem 'chronic_duration'
gem 'activesupport', '>= 3.0.0'
gem 'spanner'
-gem 'sugar-high', '~> 0.7.2' # for range intersect
-gem 'xduration', '~> 2.2'
+gem 'sugar-high', '~> 0.7.3' # for range intersect
+gem 'xduration', '~> 2.2'
group :test, :development do
gem "rspec", ">= 2.8.0"
- gem 'rails', '~> 3.2.7'
+ gem 'rails', '>= 3.2'
# gem 'mongoid', '>= 2.4'
# gem 'bson', '>= 1.6'
View
@@ -104,7 +104,7 @@ GEM
hike (~> 1.2)
rack (~> 1.0)
tilt (~> 1.1, != 1.3.0)
- sugar-high (0.7.2)
+ sugar-high (0.7.3)
thor (0.15.4)
tilt (1.3.3)
treetop (1.4.10)
@@ -126,10 +126,10 @@ DEPENDENCIES
jeweler (>= 1.8.3)
mongoid (~> 3.0)
origin-selectable_ext (~> 0.1.1)
- rails (~> 3.2.7)
+ rails (>= 3.2)
rdoc (>= 3.12)
rspec (>= 2.8.0)
simplecov (>= 0.5)
spanner
- sugar-high (~> 0.7.2)
+ sugar-high (~> 0.7.3)
xduration (~> 2.2)
View
@@ -1 +1 @@
-0.5.1
+0.5.2
View
@@ -43,9 +43,9 @@ class TimeParseError < StandardError; end
alias_method :start_date, :start_time
alias_method :end_date, :end_time
- START_KEYS = [:start, :from, :start_date]
- END_KEYS = [:to, :end, :end_date]
- DURATION_KEYS = [:duration, :lasting]
+ START_KEYS = [:start, :from, :start_date, :start_time, :start_from, :starting]
+ END_KEYS = [:to, :end, :end_date, :end_at, :ending]
+ DURATION_KEYS = [:duration, :lasting, :"for"]
ALL_KEYS = START_KEYS + END_KEYS + DURATION_KEYS
@@ -66,6 +66,14 @@ def initialize options = {}
@is_new = false
end
+ def asap!
+ @asap = true
+ end
+
+ def asap= value
+ @asap = !!value
+ end
+
class << self
def max_date
@max_date ||= Time.now + 10.years
@@ -88,8 +96,10 @@ def asap options = {}
end
def from start, duration, options = {}
+ asap = false
start = case start.to_sym
when :now, :asap
+ asap = true
Time.now
when :today
Date.today
@@ -105,7 +115,7 @@ def from start, duration, options = {}
start
end
- self.new start_date: start, duration: duration
+ self.new start_date: start, duration: duration, asap: asap
end
def untill ending
@@ -158,11 +168,11 @@ def end_time= time
alias_method :end_date=, :end_time=
def asap?
- @asap
+ !!@asap
end
def min
- asap ? Time.now : start_time
+ asap? ? Time.now : start_time
end
def max
@@ -2,11 +2,12 @@ class Hash
def __evolve_to_timespan__
serializer = Mongoid::Fields::Timespan
object = self
+ # puts "evolve from: #{self}"
::Timespan.new :from => serializer.from(object), :to => serializer.to(object), asap: serializer.asap(object)
end
def __evolve_to_duration_range__
- range = Range.new self['from'], self['to']
+ range = Range.new (self['from'] || self[:from]), (self['to'] || self[:to])
::DurationRange.new range, :seconds
end
end
@@ -1,22 +1,40 @@
class TimespanRange < DelegateDecorator
attr_accessor :unit, :range
- def initialize range, unit
- super(range)
+ def initialize range, unit = :minutes
+ range = (0..60) if range.min == nil || range.max == nil
+ super(range, except: %w{to_s to_str})
@range = Timespan.new between: range
- @unit = unit.to_s.singularize.to_sym
+ @unit = unit.to_s.pluralize.to_sym
+ end
+
+ def to_str
+ to_s
+ end
+
+ def to_s
+ range.min.nil? ? 'no timespan range' : "#{range.min} to #{range.max} #{unit}"
end
end
class DurationRange < DelegateDecorator
attr_accessor :unit, :range
- def initialize range, unit
- super(range)
- @unit = unit.to_s.singularize.to_sym
+ def initialize range, unit = :minutes
+ range = (0..60) if range.min == nil || range.max == nil
+ super(range, except: %w{to_s to_str})
+ @unit = unit.to_s.pluralize.to_sym
@range = range
end
+ def to_str
+ to_s
+ end
+
+ def to_s
+ range.min.nil? ? 'no duration range' : "#{range.min} to #{range.max} #{unit}"
+ end
+
def __evolve_to_duration_range__
self
end
@@ -70,7 +88,7 @@ def demongoize(object)
else
raise "Unable to demongoize DurationRange from: #{object}"
end
- # puts "demongoized: #{demongoized} - DurationRange"
+ # puts "demongoized: #{demongoized} - #{demongoized.class}"
demongoized
end
@@ -108,7 +126,6 @@ def parse duration
end
end
-
class Range
[:seconds, :minutes, :hours, :days, :weeks, :months, :years].each do |unit|
define_method unit do |type = :duration|
@@ -17,7 +17,14 @@ def __evolve_to_timespan__
# @param [Timespan, Hash, Integer, String] value
# @return [Hash] Timespan in seconds
def mongoize
- {:from => Serializer.serialize_time(start_time), :to => Serializer.serialize_time(end_time), :duration => duration.total, :asap => asap? }
+ hash = {
+ :from => Serializer.serialize_time(start_time),
+ :to => Serializer.serialize_time(end_time),
+ :duration => duration.total,
+ :asap => asap?
+ }
+ # puts "serialize: #{hash}"
+ hash
end
class << self
@@ -2,16 +2,26 @@ module Mongoid
module Timespanned
extend ActiveSupport::Concern
+ def asap_for tspan_field = :period
+ ts = ::Timespan.asap duration: send(tspan_field).duration
+ self.send("#{tspan_field}=", ts)
+ self.save!
+ end
+
class << self
attr_accessor :log
def log msg
- puts msg # if log?
+ puts msg if log?
end
def log?
@log
end
+
+ def log!
+ @log = true
+ end
end
module ClassMethods
View
@@ -16,13 +16,17 @@ module Printer
# %td => total days
# %th => total hours
# %tm => total minutes
- # %ts => total seconds
+ # %ts => total seconds
def to_s mode = :full
meth = "print_#{mode}"
raise ArgumentError, "Print mode not supported, was: #{mode}" unless respond_to?(meth)
send(meth)
end
+ def to_str
+ to_s
+ end
+
def print_dates
"#{i18n_t 'from'} #{print :start_time} #{i18n_t 'to'} #{print :end_time}"
end
@@ -44,6 +48,9 @@ def i18n_t label
def print type
return duration.format(duration_format) if type == :duration
raise ArgumentError, "Not a valid print type, was: #{type}" unless valid_print_type? type
+
+ return "ASAP" if type == :start_time && asap?
+
send(type).strftime(time_format)
end
View
@@ -0,0 +1,32 @@
+require 'spec_helper'
+
+describe Timespan::Span do
+ subject { timespan }
+
+ let(:from) { Chronic.parse("1 day ago") }
+ let(:to) { Time.now }
+
+ describe '.asap factory method' do
+ let(:timespan) { Timespan.asap :to => to }
+
+ its(:asap?) { should be_true }
+ end
+
+ describe '.asap=' do
+ before do
+ @timespan = Timespan.new :from => from, :to => to
+ @timespan.asap = true
+ end
+
+ specify { @timespan.asap?.should be_true }
+ end
+
+ describe '.asap!' do
+ before do
+ @timespan = Timespan.new :from => from, :to => to
+ @timespan.asap!
+ end
+
+ specify { @timespan.asap?.should be_true }
+ end
+end
@@ -28,7 +28,7 @@
its(:range) { should be_a Range }
its(:min) { should == 1.day }
its(:max) { should == 5.days }
- its(:unit) { should == :day }
+ its(:unit) { should == :days }
specify { subject.between?(4.days).should be_true }
end
@@ -39,22 +39,22 @@
its(:range) { should be_a Range }
its(:min) { should == 1.week }
its(:max) { should == 5.weeks }
- its(:unit) { should == :week }
+ its(:unit) { should == :weeks }
end
context 'month range' do
let (:timerange) { range.months }
its(:min) { should == 1.month }
its(:max) { should == 5.months }
- its(:unit) { should == :month }
+ its(:unit) { should == :months }
end
context 'year range' do
let (:timerange) { range.years }
its(:min) { should == 1.year }
its(:max) { should == 5.years }
- its(:unit) { should == :year }
+ its(:unit) { should == :years }
end
end
@@ -25,7 +25,7 @@
its(:range) { should be_a Timespan }
its(:min) { should == 1.day }
its(:max) { should == 5.days }
- its(:unit) { should == :day }
+ its(:unit) { should == :days }
end
context 'week range' do
@@ -34,7 +34,7 @@
its(:range) { should be_a Timespan }
its(:min) { should == 1.week }
its(:max) { should == 5.weeks }
- its(:unit) { should == :week }
+ its(:unit) { should == :weeks }
end
context 'month range' do
@@ -43,7 +43,7 @@
its(:range) { should be_a Timespan }
its(:min) { should == 1.month }
its(:max) { should == 5.months }
- its(:unit) { should == :month }
+ its(:unit) { should == :months }
end
context 'year range' do
@@ -52,6 +52,6 @@
its(:range) { should be_a Timespan }
its(:min) { should == 1.year }
its(:max) { should == 5.years }
- its(:unit) { should == :year }
+ its(:unit) { should == :years }
end
end
@@ -68,11 +68,11 @@ def max_asap
# end
it 'should have custom max_asap' do
- TimePeriod.max_asap.should == 14.days.from_now.to_i
+ TimePeriod.max_asap.should be_within(2).of(14.days.from_now.to_i)
end
it 'should have custom min_asap' do
- TimePeriod.min_asap.should == 2.days.ago.to_i
+ TimePeriod.min_asap.should == be_within(2).of(2.days.ago.to_i)
end
it 'should find #1, #2, #3, #4, #5' do
@@ -13,9 +13,10 @@ class Account
timespan_container_delegates :time_period, :dates, :all
- def self.create_it! duration
+ def self.create_it! duration, flex = nil
acc = self.new period: ::Timespan.new(duration: duration), time_period: ::TimePeriod.new
acc.time_period.dates_duration = 1.day
+ acc.time_period.flex = flex if flex
acc
end
Oops, something went wrong.

0 comments on commit de58a85

Please sign in to comment.