timespan 0.4.1 → 0.4.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/VERSION +1 -1
- data/lib/timespan/mongoid.rb +1 -0
- data/lib/timespan/mongoid/mongoid_3x.rb +42 -32
- data/lib/timespan/mongoid/timespanned.rb +21 -0
- data/spec/timespan/mongoid/models/account_3x.rb +5 -0
- data/spec/timespan/mongoid/mongoid_timespan_spec.rb +14 -0
- data/timespan.gemspec +3 -2
- metadata +4 -3
    
        data/VERSION
    CHANGED
    
    | @@ -1 +1 @@ | |
| 1 | 
            -
            0.4. | 
| 1 | 
            +
            0.4.2
         | 
    
        data/lib/timespan/mongoid.rb
    CHANGED
    
    
| @@ -1,6 +1,18 @@ | |
| 1 1 | 
             
            class Timespan
         | 
| 2 | 
            -
               | 
| 3 | 
            -
             | 
| 2 | 
            +
              Serializer = Mongoid::Fields::Timespan
         | 
| 3 | 
            +
             | 
| 4 | 
            +
              # See http://mongoid.org/en/mongoid/docs/upgrading.html        
         | 
| 5 | 
            +
             | 
| 6 | 
            +
              # Serialize a Timespan or a Hash (with Timespan units) or a Duration in some form to
         | 
| 7 | 
            +
              # a BSON serializable type.
         | 
| 8 | 
            +
              #
         | 
| 9 | 
            +
              # @param [Timespan, Hash, Integer, String] value
         | 
| 10 | 
            +
              # @return [Hash] Timespan in seconds
         | 
| 11 | 
            +
              def mongoize
         | 
| 12 | 
            +
                {:from => Serializer.serialize_time(start_time), :to => Serializer.serialize_time(end_time), :duration => duration.total }
         | 
| 13 | 
            +
              end
         | 
| 14 | 
            +
             | 
| 15 | 
            +
              class << self
         | 
| 4 16 | 
             
                # See http://mongoid.org/en/mongoid/docs/upgrading.html        
         | 
| 5 17 |  | 
| 6 18 | 
             
                # Serialize a Timespan or a Hash (with Timespan units) or a Duration in some form to
         | 
| @@ -8,39 +20,37 @@ class Timespan | |
| 8 20 | 
             
                #
         | 
| 9 21 | 
             
                # @param [Timespan, Hash, Integer, String] value
         | 
| 10 22 | 
             
                # @return [Hash] Timespan in seconds
         | 
| 11 | 
            -
                def mongoize
         | 
| 12 | 
            -
                   | 
| 23 | 
            +
                def mongoize object
         | 
| 24 | 
            +
                  case object
         | 
| 25 | 
            +
                  when Timespan then object.mongoize
         | 
| 26 | 
            +
                  when Hash
         | 
| 27 | 
            +
                    ::Timespan.new object
         | 
| 28 | 
            +
                  else
         | 
| 29 | 
            +
                    object
         | 
| 30 | 
            +
                  end
         | 
| 13 31 | 
             
                end
         | 
| 14 32 |  | 
| 15 | 
            -
                 | 
| 16 | 
            -
             | 
| 17 | 
            -
             | 
| 18 | 
            -
             | 
| 19 | 
            -
             | 
| 20 | 
            -
                   | 
| 21 | 
            -
             | 
| 22 | 
            -
             | 
| 23 | 
            -
                     | 
| 24 | 
            -
             | 
| 25 | 
            -
                     | 
| 26 | 
            -
             | 
| 27 | 
            -
             | 
| 28 | 
            -
                  end
         | 
| 33 | 
            +
                # Deserialize a Timespan given the hash stored by Mongodb
         | 
| 34 | 
            +
                #
         | 
| 35 | 
            +
                # @param [Hash] Timespan as hash
         | 
| 36 | 
            +
                # @return [Timespan] deserialized Timespan
         | 
| 37 | 
            +
                def demongoize(object)
         | 
| 38 | 
            +
                  return if !object
         | 
| 39 | 
            +
                  case object
         | 
| 40 | 
            +
                  when Hash
         | 
| 41 | 
            +
                    ::Timespan.new :from => Serializer.from(object), :to => Serializer.to(object)
         | 
| 42 | 
            +
                  else
         | 
| 43 | 
            +
                    ::Timespan.new object
         | 
| 44 | 
            +
                  end        
         | 
| 45 | 
            +
                end
         | 
| 29 46 |  | 
| 30 | 
            -
             | 
| 31 | 
            -
             | 
| 32 | 
            -
             | 
| 33 | 
            -
                   | 
| 47 | 
            +
                # Converts the object that was supplied to a criteria and converts it
         | 
| 48 | 
            +
                # into a database friendly form.
         | 
| 49 | 
            +
                def evolve(object)
         | 
| 50 | 
            +
                  case object
         | 
| 51 | 
            +
                  when Timespan then object.mongoize
         | 
| 52 | 
            +
                  else object
         | 
| 53 | 
            +
                  end
         | 
| 34 54 | 
             
                end
         | 
| 35 55 | 
             
              end
         | 
| 36 56 | 
             
            end
         | 
| 37 | 
            -
             | 
| 38 | 
            -
             | 
| 39 | 
            -
            class Timespan
         | 
| 40 | 
            -
              include Mongoize
         | 
| 41 | 
            -
             | 
| 42 | 
            -
              protected
         | 
| 43 | 
            -
             | 
| 44 | 
            -
              include Mongoid::Fields::Timespan::Methods
         | 
| 45 | 
            -
              extend Mongoid::Fields::Timespan::Methods
         | 
| 46 | 
            -
            end
         | 
| @@ -0,0 +1,21 @@ | |
| 1 | 
            +
            module Mongoid
         | 
| 2 | 
            +
              module Timespanned
         | 
| 3 | 
            +
                extend ActiveSupport::Concern
         | 
| 4 | 
            +
             | 
| 5 | 
            +
                module ClassMethods
         | 
| 6 | 
            +
                  def timespan_setters name = :period  
         | 
| 7 | 
            +
                    define_method :"#{name}_start=" do |date|
         | 
| 8 | 
            +
                      self.send "#{name}=", ::Timespan.new(start_date: date, end_date: self.send(name).end_date)
         | 
| 9 | 
            +
                    end
         | 
| 10 | 
            +
             | 
| 11 | 
            +
                    define_method :"#{name}_end=" do |date|
         | 
| 12 | 
            +
                      self.send "#{name}=", ::Timespan.new(start_date: self.send(name).start_date, end_date: date)
         | 
| 13 | 
            +
                    end
         | 
| 14 | 
            +
             | 
| 15 | 
            +
                    define_method :"#{name}duration=" do |duration|
         | 
| 16 | 
            +
                      self.send "#{name}=", ::Timespan.new(start_date: self.send(name).start_date, duration: duration)
         | 
| 17 | 
            +
                    end
         | 
| 18 | 
            +
                  end
         | 
| 19 | 
            +
                end
         | 
| 20 | 
            +
              end
         | 
| 21 | 
            +
            end
         | 
| @@ -1,8 +1,13 @@ | |
| 1 1 | 
             
            class Account
         | 
| 2 2 | 
             
              include Mongoid::Document
         | 
| 3 | 
            +
              include Mongoid::Timespanned
         | 
| 3 4 |  | 
| 4 5 | 
             
              field :period, :type => ::Timespan, :between => true
         | 
| 5 6 |  | 
| 7 | 
            +
              delegate :start_date, to: :period
         | 
| 8 | 
            +
             | 
| 9 | 
            +
              timespan_setters :period
         | 
| 10 | 
            +
             | 
| 6 11 | 
             
              def self.create_it! duration
         | 
| 7 12 | 
             
                t = ::Timespan.new(duration: duration)
         | 
| 8 13 | 
             
                self.new period: t
         | 
| @@ -3,6 +3,10 @@ require 'timespan/mongoid/spec_helper' | |
| 3 3 | 
             
            describe TimeSpan do
         | 
| 4 4 | 
             
            	subject { account }
         | 
| 5 5 |  | 
| 6 | 
            +
              def tomorrow
         | 
| 7 | 
            +
                Date.today + 1.day
         | 
| 8 | 
            +
              end
         | 
| 9 | 
            +
             | 
| 6 10 | 
             
              let(:from) { Chronic.parse("1 day ago") }
         | 
| 7 11 | 
             
              let(:to)   { Time.now }
         | 
| 8 12 |  | 
| @@ -40,5 +44,15 @@ describe TimeSpan do | |
| 40 44 | 
             
                    DateTime.parse(subject.period.start_date.to_s).strftime('%d %b %Y').should == Date.today.strftime('%d %b %Y')
         | 
| 41 45 | 
             
                  end
         | 
| 42 46 | 
             
                end
         | 
| 47 | 
            +
             | 
| 48 | 
            +
                describe 'set new start_date' do
         | 
| 49 | 
            +
                  before :each do
         | 
| 50 | 
            +
                    subject.period_start = tomorrow
         | 
| 51 | 
            +
                  end
         | 
| 52 | 
            +
             | 
| 53 | 
            +
                  specify do
         | 
| 54 | 
            +
                    Date.parse(subject.start_date.to_s).should == tomorrow
         | 
| 55 | 
            +
                  end
         | 
| 56 | 
            +
                end
         | 
| 43 57 | 
             
              end
         | 
| 44 58 | 
             
            end
         | 
    
        data/timespan.gemspec
    CHANGED
    
    | @@ -5,11 +5,11 @@ | |
| 5 5 |  | 
| 6 6 | 
             
            Gem::Specification.new do |s|
         | 
| 7 7 | 
             
              s.name = "timespan"
         | 
| 8 | 
            -
              s.version = "0.4. | 
| 8 | 
            +
              s.version = "0.4.2"
         | 
| 9 9 |  | 
| 10 10 | 
             
              s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
         | 
| 11 11 | 
             
              s.authors = ["Kristian Mandrup"]
         | 
| 12 | 
            -
              s.date = "2012-09- | 
| 12 | 
            +
              s.date = "2012-09-19"
         | 
| 13 13 | 
             
              s.description = "Makes it easy to calculate time distance in different units"
         | 
| 14 14 | 
             
              s.email = "kmandrup@gmail.com"
         | 
| 15 15 | 
             
              s.extra_rdoc_files = [
         | 
| @@ -31,6 +31,7 @@ Gem::Specification.new do |s| | |
| 31 31 | 
             
                "lib/timespan/mongoid.rb",
         | 
| 32 32 | 
             
                "lib/timespan/mongoid/mongoid_2x.rb",
         | 
| 33 33 | 
             
                "lib/timespan/mongoid/mongoid_3x.rb",
         | 
| 34 | 
            +
                "lib/timespan/mongoid/timespanned.rb",
         | 
| 34 35 | 
             
                "lib/timespan/printer.rb",
         | 
| 35 36 | 
             
                "lib/timespan/rails/engine.rb",
         | 
| 36 37 | 
             
                "lib/timespan/span.rb",
         | 
    
        metadata
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            --- !ruby/object:Gem::Specification
         | 
| 2 2 | 
             
            name: timespan
         | 
| 3 3 | 
             
            version: !ruby/object:Gem::Version
         | 
| 4 | 
            -
              version: 0.4. | 
| 4 | 
            +
              version: 0.4.2
         | 
| 5 5 | 
             
              prerelease: 
         | 
| 6 6 | 
             
            platform: ruby
         | 
| 7 7 | 
             
            authors:
         | 
| @@ -9,7 +9,7 @@ authors: | |
| 9 9 | 
             
            autorequire: 
         | 
| 10 10 | 
             
            bindir: bin
         | 
| 11 11 | 
             
            cert_chain: []
         | 
| 12 | 
            -
            date: 2012-09- | 
| 12 | 
            +
            date: 2012-09-19 00:00:00.000000000 Z
         | 
| 13 13 | 
             
            dependencies:
         | 
| 14 14 | 
             
            - !ruby/object:Gem::Dependency
         | 
| 15 15 | 
             
              name: chronic
         | 
| @@ -209,6 +209,7 @@ files: | |
| 209 209 | 
             
            - lib/timespan/mongoid.rb
         | 
| 210 210 | 
             
            - lib/timespan/mongoid/mongoid_2x.rb
         | 
| 211 211 | 
             
            - lib/timespan/mongoid/mongoid_3x.rb
         | 
| 212 | 
            +
            - lib/timespan/mongoid/timespanned.rb
         | 
| 212 213 | 
             
            - lib/timespan/printer.rb
         | 
| 213 214 | 
             
            - lib/timespan/rails/engine.rb
         | 
| 214 215 | 
             
            - lib/timespan/span.rb
         | 
| @@ -246,7 +247,7 @@ required_ruby_version: !ruby/object:Gem::Requirement | |
| 246 247 | 
             
                  version: '0'
         | 
| 247 248 | 
             
                  segments:
         | 
| 248 249 | 
             
                  - 0
         | 
| 249 | 
            -
                  hash: - | 
| 250 | 
            +
                  hash: -2120967004770595531
         | 
| 250 251 | 
             
            required_rubygems_version: !ruby/object:Gem::Requirement
         | 
| 251 252 | 
             
              none: false
         | 
| 252 253 | 
             
              requirements:
         |