activechronology 0.1.0 → 0.2.0
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.
- checksums.yaml +4 -4
 - data/README.md +28 -7
 - data/activechronology.gemspec +1 -0
 - data/lib/active_chronology/chronology.rb +18 -5
 - data/lib/active_chronology/version.rb +1 -1
 - metadata +16 -2
 
    
        checksums.yaml
    CHANGED
    
    | 
         @@ -1,7 +1,7 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            ---
         
     | 
| 
       2 
2 
     | 
    
         
             
            SHA1:
         
     | 
| 
       3 
     | 
    
         
            -
              metadata.gz:  
     | 
| 
       4 
     | 
    
         
            -
              data.tar.gz:  
     | 
| 
      
 3 
     | 
    
         
            +
              metadata.gz: ce0d3396fa9c9ac5ec7772499e21034e0ecf4cf8
         
     | 
| 
      
 4 
     | 
    
         
            +
              data.tar.gz: ba6d46bdb6ec231ad65050ed9263bb357023489b
         
     | 
| 
       5 
5 
     | 
    
         
             
            SHA512:
         
     | 
| 
       6 
     | 
    
         
            -
              metadata.gz:  
     | 
| 
       7 
     | 
    
         
            -
              data.tar.gz:  
     | 
| 
      
 6 
     | 
    
         
            +
              metadata.gz: 35b9f7859af8b3516b9ebc2d4e09f995134c186c1e20ece91f7b879a42f9db769a669088160c43c4c2b6a5d42b71593ddffe968d9df7c655ac4a8cc9158c60a1
         
     | 
| 
      
 7 
     | 
    
         
            +
              data.tar.gz: 1ca413b3cad3bb5af90fbf07bd2e3774937e1e421aa1b1d75030dfaaab0a37637f8043dfc1075134711884be6ee8de8e351ac591e81ff71f1cadf6492a14b363
         
     | 
    
        data/README.md
    CHANGED
    
    | 
         @@ -1,15 +1,13 @@ 
     | 
|
| 
       1 
     | 
    
         
            -
            #  
     | 
| 
      
 1 
     | 
    
         
            +
            # ActiveChronology
         
     | 
| 
       2 
2 
     | 
    
         | 
| 
       3 
     | 
    
         
            -
             
     | 
| 
       4 
     | 
    
         
            -
             
     | 
| 
       5 
     | 
    
         
            -
            TODO: Delete this and the text above, and describe your gem
         
     | 
| 
      
 3 
     | 
    
         
            +
            Easily scope your ActiveRecord models by timestamps or dates.
         
     | 
| 
       6 
4 
     | 
    
         | 
| 
       7 
5 
     | 
    
         
             
            ## Installation
         
     | 
| 
       8 
6 
     | 
    
         | 
| 
       9 
7 
     | 
    
         
             
            Add this line to your application's Gemfile:
         
     | 
| 
       10 
8 
     | 
    
         | 
| 
       11 
9 
     | 
    
         
             
            ```ruby
         
     | 
| 
       12 
     | 
    
         
            -
            gem 'activechronology'
         
     | 
| 
      
 10 
     | 
    
         
            +
            gem 'activechronology', require: 'active_chronology'
         
     | 
| 
       13 
11 
     | 
    
         
             
            ```
         
     | 
| 
       14 
12 
     | 
    
         | 
| 
       15 
13 
     | 
    
         
             
            And then execute:
         
     | 
| 
         @@ -22,7 +20,31 @@ Or install it yourself as: 
     | 
|
| 
       22 
20 
     | 
    
         | 
| 
       23 
21 
     | 
    
         
             
            ## Usage
         
     | 
| 
       24 
22 
     | 
    
         | 
| 
       25 
     | 
    
         
            -
             
     | 
| 
      
 23 
     | 
    
         
            +
            ### `set_chronology`
         
     | 
| 
      
 24 
     | 
    
         
            +
             
     | 
| 
      
 25 
     | 
    
         
            +
            From your ActiveRecord models, you can add `chronological` and `reverse_chronological`
         
     | 
| 
      
 26 
     | 
    
         
            +
            scopes by calling `set_chronology`. This accepts an attribute name as a parameter,
         
     | 
| 
      
 27 
     | 
    
         
            +
            and it defaults to `created_at`.
         
     | 
| 
      
 28 
     | 
    
         
            +
             
     | 
| 
      
 29 
     | 
    
         
            +
            ### `scope_by_timestamp`
         
     | 
| 
      
 30 
     | 
    
         
            +
             
     | 
| 
      
 31 
     | 
    
         
            +
            Calling `scope_by_timestamp` with a list of attribute names, you get the following
         
     | 
| 
      
 32 
     | 
    
         
            +
            methods defined, using `:created_at` as an example:
         
     | 
| 
      
 33 
     | 
    
         
            +
             
     | 
| 
      
 34 
     | 
    
         
            +
            ```
         
     | 
| 
      
 35 
     | 
    
         
            +
            created_after(time)
         
     | 
| 
      
 36 
     | 
    
         
            +
            created_before(time)
         
     | 
| 
      
 37 
     | 
    
         
            +
            created_between(start time, end time)
         
     | 
| 
      
 38 
     | 
    
         
            +
            ```
         
     | 
| 
      
 39 
     | 
    
         
            +
             
     | 
| 
      
 40 
     | 
    
         
            +
            Here's an example class:
         
     | 
| 
      
 41 
     | 
    
         
            +
             
     | 
| 
      
 42 
     | 
    
         
            +
            ```ruby
         
     | 
| 
      
 43 
     | 
    
         
            +
            class User
         
     | 
| 
      
 44 
     | 
    
         
            +
              set_chronology :last_seen_at
         
     | 
| 
      
 45 
     | 
    
         
            +
              scope_by_timestamp :last_seen_at
         
     | 
| 
      
 46 
     | 
    
         
            +
            end
         
     | 
| 
      
 47 
     | 
    
         
            +
            ```
         
     | 
| 
       26 
48 
     | 
    
         | 
| 
       27 
49 
     | 
    
         
             
            ## Development
         
     | 
| 
       28 
50 
     | 
    
         | 
| 
         @@ -38,4 +60,3 @@ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERN 
     | 
|
| 
       38 
60 
     | 
    
         
             
            ## License
         
     | 
| 
       39 
61 
     | 
    
         | 
| 
       40 
62 
     | 
    
         
             
            The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
         
     | 
| 
       41 
     | 
    
         
            -
             
     | 
    
        data/activechronology.gemspec
    CHANGED
    
    | 
         @@ -21,6 +21,7 @@ Gem::Specification.new do |spec| 
     | 
|
| 
       21 
21 
     | 
    
         
             
              spec.add_development_dependency "bundler", "~> 1.11"
         
     | 
| 
       22 
22 
     | 
    
         
             
              spec.add_development_dependency "rake", "~> 10.0"
         
     | 
| 
       23 
23 
     | 
    
         
             
              spec.add_development_dependency "rspec", "~> 3.0"
         
     | 
| 
      
 24 
     | 
    
         
            +
              spec.add_development_dependency "sqlite3"
         
     | 
| 
       24 
25 
     | 
    
         | 
| 
       25 
26 
     | 
    
         
             
              spec.add_dependency "rails", "> 3"
         
     | 
| 
       26 
27 
     | 
    
         
             
            end
         
     | 
| 
         @@ -14,19 +14,32 @@ module ActiveChronology 
     | 
|
| 
       14 
14 
     | 
    
         
             
                    attributes.each do |attribute|
         
     | 
| 
       15 
15 
     | 
    
         
             
                      name = attribute.to_s.sub(/_(at|on|time|date)$/, '')
         
     | 
| 
       16 
16 
     | 
    
         | 
| 
       17 
     | 
    
         
            -
                      scope "#{name}_after",   single_time_scope(attribute, ' 
     | 
| 
       18 
     | 
    
         
            -
                      scope "#{name}_before",  single_time_scope(attribute, ' 
     | 
| 
       19 
     | 
    
         
            -
                      scope "#{name}_between",  
     | 
| 
      
 17 
     | 
    
         
            +
                      scope "#{name}_after",   single_time_scope(attribute, '>')
         
     | 
| 
      
 18 
     | 
    
         
            +
                      scope "#{name}_before",  single_time_scope(attribute, '<')
         
     | 
| 
      
 19 
     | 
    
         
            +
                      scope "#{name}_between", between_time_scope(attribute)
         
     | 
| 
       20 
20 
     | 
    
         
             
                    end
         
     | 
| 
       21 
21 
     | 
    
         
             
                  end
         
     | 
| 
       22 
22 
     | 
    
         | 
| 
       23 
23 
     | 
    
         
             
                  private
         
     | 
| 
       24 
24 
     | 
    
         | 
| 
       25 
     | 
    
         
            -
                  def single_time_scope(attribute,  
     | 
| 
       26 
     | 
    
         
            -
                    -> (time) do
         
     | 
| 
      
 25 
     | 
    
         
            +
                  def single_time_scope(attribute, base_operator)
         
     | 
| 
      
 26 
     | 
    
         
            +
                    -> (time, options = {}) do
         
     | 
| 
      
 27 
     | 
    
         
            +
                      operator = options[:exclusive] ? base_operator : "#{base_operator}="
         
     | 
| 
      
 28 
     | 
    
         
            +
             
     | 
| 
       27 
29 
     | 
    
         
             
                      time.present? ? where("#{table_name}.#{attribute} #{operator} ?", time) : all
         
     | 
| 
       28 
30 
     | 
    
         
             
                    end
         
     | 
| 
       29 
31 
     | 
    
         
             
                  end
         
     | 
| 
      
 32 
     | 
    
         
            +
             
     | 
| 
      
 33 
     | 
    
         
            +
                  def between_time_scope(attribute)
         
     | 
| 
      
 34 
     | 
    
         
            +
                    -> (start_time, end_time, options = {}) do
         
     | 
| 
      
 35 
     | 
    
         
            +
                      if options[:exclusive]
         
     | 
| 
      
 36 
     | 
    
         
            +
                        where(
         
     | 
| 
      
 37 
     | 
    
         
            +
                          "#{table_name}.#{attribute} > ? AND #{table_name}.#{attribute} < ?", start_time, end_time)
         
     | 
| 
      
 38 
     | 
    
         
            +
                      else
         
     | 
| 
      
 39 
     | 
    
         
            +
                        where(attribute => start_time..end_time)
         
     | 
| 
      
 40 
     | 
    
         
            +
                      end
         
     | 
| 
      
 41 
     | 
    
         
            +
                    end
         
     | 
| 
      
 42 
     | 
    
         
            +
                  end
         
     | 
| 
       30 
43 
     | 
    
         
             
                end
         
     | 
| 
       31 
44 
     | 
    
         
             
              end
         
     | 
| 
       32 
45 
     | 
    
         
             
            end
         
     | 
    
        metadata
    CHANGED
    
    | 
         @@ -1,7 +1,7 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            --- !ruby/object:Gem::Specification
         
     | 
| 
       2 
2 
     | 
    
         
             
            name: activechronology
         
     | 
| 
       3 
3 
     | 
    
         
             
            version: !ruby/object:Gem::Version
         
     | 
| 
       4 
     | 
    
         
            -
              version: 0. 
     | 
| 
      
 4 
     | 
    
         
            +
              version: 0.2.0
         
     | 
| 
       5 
5 
     | 
    
         
             
            platform: ruby
         
     | 
| 
       6 
6 
     | 
    
         
             
            authors:
         
     | 
| 
       7 
7 
     | 
    
         
             
            - Kyle Edson
         
     | 
| 
         @@ -9,7 +9,7 @@ authors: 
     | 
|
| 
       9 
9 
     | 
    
         
             
            autorequire: 
         
     | 
| 
       10 
10 
     | 
    
         
             
            bindir: exe
         
     | 
| 
       11 
11 
     | 
    
         
             
            cert_chain: []
         
     | 
| 
       12 
     | 
    
         
            -
            date:  
     | 
| 
      
 12 
     | 
    
         
            +
            date: 2017-02-07 00:00:00.000000000 Z
         
     | 
| 
       13 
13 
     | 
    
         
             
            dependencies:
         
     | 
| 
       14 
14 
     | 
    
         
             
            - !ruby/object:Gem::Dependency
         
     | 
| 
       15 
15 
     | 
    
         
             
              name: bundler
         
     | 
| 
         @@ -53,6 +53,20 @@ dependencies: 
     | 
|
| 
       53 
53 
     | 
    
         
             
                - - "~>"
         
     | 
| 
       54 
54 
     | 
    
         
             
                  - !ruby/object:Gem::Version
         
     | 
| 
       55 
55 
     | 
    
         
             
                    version: '3.0'
         
     | 
| 
      
 56 
     | 
    
         
            +
            - !ruby/object:Gem::Dependency
         
     | 
| 
      
 57 
     | 
    
         
            +
              name: sqlite3
         
     | 
| 
      
 58 
     | 
    
         
            +
              requirement: !ruby/object:Gem::Requirement
         
     | 
| 
      
 59 
     | 
    
         
            +
                requirements:
         
     | 
| 
      
 60 
     | 
    
         
            +
                - - ">="
         
     | 
| 
      
 61 
     | 
    
         
            +
                  - !ruby/object:Gem::Version
         
     | 
| 
      
 62 
     | 
    
         
            +
                    version: '0'
         
     | 
| 
      
 63 
     | 
    
         
            +
              type: :development
         
     | 
| 
      
 64 
     | 
    
         
            +
              prerelease: false
         
     | 
| 
      
 65 
     | 
    
         
            +
              version_requirements: !ruby/object:Gem::Requirement
         
     | 
| 
      
 66 
     | 
    
         
            +
                requirements:
         
     | 
| 
      
 67 
     | 
    
         
            +
                - - ">="
         
     | 
| 
      
 68 
     | 
    
         
            +
                  - !ruby/object:Gem::Version
         
     | 
| 
      
 69 
     | 
    
         
            +
                    version: '0'
         
     | 
| 
       56 
70 
     | 
    
         
             
            - !ruby/object:Gem::Dependency
         
     | 
| 
       57 
71 
     | 
    
         
             
              name: rails
         
     | 
| 
       58 
72 
     | 
    
         
             
              requirement: !ruby/object:Gem::Requirement
         
     |