lifetime 0.1.1

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 63a706ae6c2d4afc3488aff48da2e3e55a0b9c0cc6b3e4782ada76adafbfe068
4
+ data.tar.gz: df9cf38e4b8c603374ba5b155b524fa89ca20081e95a396beec5c9e6132659c2
5
+ SHA512:
6
+ metadata.gz: 727e89dcbcf442d79b3cd360c871102352e319a252b7ab1abd363ac50b5208bfba1db2c4936eae26fd14bc2f996d88d1aabe589def2598d7a2cca8571727f132
7
+ data.tar.gz: d6f6cc722261df168bb7d55ad8421bd496dfa28ece03e0928b927900a180c0d50d58a0f8a2a04da460e3289a1ec227380945ba55a364f406be7dc97ce0b461a4
@@ -0,0 +1,29 @@
1
+ name: Ruby Gem
2
+
3
+ on:
4
+ push:
5
+ tags:
6
+ - v*
7
+
8
+ jobs:
9
+ build:
10
+ name: Build + Publish
11
+ runs-on: ubuntu-latest
12
+
13
+ steps:
14
+ - uses: actions/checkout@v2
15
+ - name: Set up Ruby 2.6
16
+ uses: actions/setup-ruby@v1
17
+ with:
18
+ version: 2.6.x
19
+
20
+ - name: Publish to RubyGems
21
+ run: |
22
+ mkdir -p $HOME/.gem
23
+ touch $HOME/.gem/credentials
24
+ chmod 0600 $HOME/.gem/credentials
25
+ printf -- "---\n:rubygems_api_key: ${GEM_HOST_API_KEY}\n" > $HOME/.gem/credentials
26
+ gem build *.gemspec
27
+ gem push *.gem
28
+ env:
29
+ GEM_HOST_API_KEY: ${{secrets.RUBYGEMS_AUTH_TOKEN}}
@@ -0,0 +1,25 @@
1
+ name: Ruby
2
+
3
+ on:
4
+ push:
5
+ branches: [ master ]
6
+ pull_request:
7
+ branches: [ master ]
8
+
9
+ jobs:
10
+ build:
11
+
12
+ runs-on: ubuntu-latest
13
+
14
+ steps:
15
+ - uses: actions/checkout@v2
16
+ - name: Set up Ruby 2.6
17
+ uses: actions/setup-ruby@v1
18
+ with:
19
+ ruby-version: 2.6.x
20
+ - name: Build and test with Rake
21
+ run: |
22
+ sudo apt-get install -y libsqlite3-dev
23
+ gem install bundler
24
+ bundle install --jobs 4 --retry 3
25
+ bundle exec rake
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.5.1
7
+ before_install: gem install bundler -v 2.0.1
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in lifetime.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,59 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ lifetime (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ activemodel (5.0.7.2)
10
+ activesupport (= 5.0.7.2)
11
+ activerecord (5.0.7.2)
12
+ activemodel (= 5.0.7.2)
13
+ activesupport (= 5.0.7.2)
14
+ arel (~> 7.0)
15
+ activesupport (5.0.7.2)
16
+ concurrent-ruby (~> 1.0, >= 1.0.2)
17
+ i18n (>= 0.7, < 2)
18
+ minitest (~> 5.1)
19
+ tzinfo (~> 1.1)
20
+ arel (7.1.4)
21
+ concurrent-ruby (1.1.5)
22
+ database_cleaner (1.7.0)
23
+ diff-lcs (1.3)
24
+ i18n (1.5.1)
25
+ concurrent-ruby (~> 1.0)
26
+ minitest (5.13.0)
27
+ rake (13.0.1)
28
+ rspec (3.9.0)
29
+ rspec-core (~> 3.9.0)
30
+ rspec-expectations (~> 3.9.0)
31
+ rspec-mocks (~> 3.9.0)
32
+ rspec-core (3.9.0)
33
+ rspec-support (~> 3.9.0)
34
+ rspec-expectations (3.9.0)
35
+ diff-lcs (>= 1.2.0, < 2.0)
36
+ rspec-support (~> 3.9.0)
37
+ rspec-mocks (3.9.0)
38
+ diff-lcs (>= 1.2.0, < 2.0)
39
+ rspec-support (~> 3.9.0)
40
+ rspec-support (3.9.0)
41
+ sqlite3 (1.3.13)
42
+ thread_safe (0.3.6)
43
+ tzinfo (1.2.5)
44
+ thread_safe (~> 0.1)
45
+
46
+ PLATFORMS
47
+ ruby
48
+
49
+ DEPENDENCIES
50
+ activerecord (~> 5.0.0)
51
+ bundler
52
+ database_cleaner (~> 1.7.0)
53
+ lifetime!
54
+ rake (~> 13.0)
55
+ rspec (~> 3.0)
56
+ sqlite3 (~> 1.3.6)
57
+
58
+ BUNDLED WITH
59
+ 2.1.4
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 azohoori
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,59 @@
1
+ # Lifetime
2
+
3
+ Use this for your ActiveRecord models that are time bound with a start
4
+ and stop time.
5
+
6
+ ![Ruby](https://github.com/Cisco-AMP/lifetime/workflows/Ruby/badge.svg)
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ ```ruby
13
+ gem 'lifetime'
14
+ ```
15
+
16
+ And then execute:
17
+
18
+ $ bundle
19
+
20
+ Or install it yourself as:
21
+
22
+ $ gem install lifetime
23
+
24
+ ## Usage
25
+
26
+ Include lifetime to you model:
27
+
28
+ ``` ruby
29
+ #use the default lifetime fields: start_at, end_at
30
+ class License < ActiveRecord::Base
31
+ include Lifetime
32
+ end
33
+
34
+ #use the custom lifetime fields
35
+ class License < ActiveRecord::Base
36
+ include Lifetime
37
+ lifetime_fields :start_date, :end_date
38
+ end
39
+
40
+ License.lifetime #returns lifetime of the license
41
+ License.first.lifetime_expired? #returns true if the license is expired
42
+ ```
43
+
44
+ Check specs for more usage
45
+
46
+
47
+ ## Development
48
+
49
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
50
+
51
+ 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).
52
+
53
+ ## Contributing
54
+
55
+ Bug reports and pull requests are welcome on GitHub at https://github.com/Cisco-AMP/lifetime.
56
+
57
+ ## License
58
+
59
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "lifetime"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,3 @@
1
+ module Lifetime
2
+ VERSION = "0.1.1"
3
+ end
data/lib/lifetime.rb ADDED
@@ -0,0 +1,65 @@
1
+ require "lifetime/version"
2
+ require "active_support"
3
+
4
+ module Lifetime
5
+ extend ActiveSupport::Concern
6
+
7
+ module ClassMethods
8
+ attr_accessor :lifetime_start_field, :lifetime_end_field
9
+ def lifetime_fields *args
10
+ self.lifetime_start_field = args[0].to_s
11
+ self.lifetime_end_field = args[1].to_s
12
+ end
13
+ end
14
+
15
+ included do
16
+ self.lifetime_start_field = "start_at"
17
+ self.lifetime_end_field = "end_at"
18
+ scope :lifetime_active, ->(time = Time.now) { where(" #{lifetime_start_field} <= ? AND #{lifetime_end_field} >= ?", time, time).lifetime_ordered }
19
+ scope :lifetime_inactive, ->(time = Time.now) { where(" #{lifetime_start_field} > ? OR #{lifetime_end_field} < ?", time, time) }
20
+ scope :lifetime_expired, ->(time = Time.now) { where(" #{lifetime_end_field} < ?", time).order("#{lifetime_end_field} desc") }
21
+ scope :lifetime_future, ->(time = Time.now) { where(" #{lifetime_start_field} > ?", time).lifetime_ordered }
22
+ scope :lifetime_ordered, -> {order("#{lifetime_start_field} asc")}
23
+ end
24
+
25
+ def lifetime_start_at
26
+ send(self.class.lifetime_start_field)
27
+ end
28
+
29
+ def lifetime_end_at
30
+ send(self.class.lifetime_end_field)
31
+ end
32
+
33
+ def lifetime_active?(time = Time.now)
34
+ lifetime_start_at <= time && lifetime_end_at >= time
35
+ end
36
+
37
+ def lifetime_inactive?(time = Time.now)
38
+ !lifetime_active?
39
+ end
40
+
41
+ def lifetime_expired?(time = Time.now)
42
+ lifetime_end_at < time
43
+ end
44
+
45
+ def lifetime_future?(time = Time.now)
46
+ lifetime_start_at > time
47
+ end
48
+
49
+ def lifetime_next_future(time = Time.now)
50
+ self.class.lifetime_future(time).first
51
+ end
52
+
53
+ def lifetime_last_expired(time = Time.now)
54
+ self.class.lifetime_expired(time).first
55
+ end
56
+
57
+ def lifetime_overlaps?(another)
58
+ lifetime.overlaps? another.lifetime
59
+ end
60
+
61
+ def lifetime
62
+ lifetime_start_at..lifetime_end_at
63
+ end
64
+
65
+ end
data/lifetime.gemspec ADDED
@@ -0,0 +1,43 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "lifetime/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "lifetime"
8
+ spec.version = Lifetime::VERSION
9
+ spec.authors = ["Ariyan Zohoorian", "S M Shahnewaz"]
10
+ spec.email = ["azohoori@cisco.com", "sshahnew@cisco.com"]
11
+
12
+ spec.summary = %q{Start and end timestamp logic for ActiveRecord}
13
+ spec.description = %q{Provides a common set of APIs for ActiveRecord models that have a start and end time}
14
+ spec.homepage = "https://github.com/Cisco-AMP/lifetime"
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
19
+ if spec.respond_to?(:metadata)
20
+ #spec.metadata["homepage_uri"] = spec.homepage
21
+ #spec.metadata["source_code_uri"] = "TODO: Put your gem's public repo URL here."
22
+ #spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."
23
+ else
24
+ raise "RubyGems 2.0 or newer is required to protect against " \
25
+ "public gem pushes."
26
+ end
27
+
28
+ # Specify which files should be added to the gem when it is released.
29
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
30
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
31
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
32
+ end
33
+ spec.bindir = "exe"
34
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
35
+ spec.require_paths = ["lib"]
36
+
37
+ spec.add_development_dependency "activerecord", "~> 5.0.0"
38
+ spec.add_development_dependency "sqlite3", '~> 1.3.6'
39
+ spec.add_development_dependency "database_cleaner", "~> 1.7.0"
40
+ spec.add_development_dependency "bundler"
41
+ spec.add_development_dependency "rake", "~> 13.0"
42
+ spec.add_development_dependency "rspec", "~> 3.0"
43
+ end
metadata ADDED
@@ -0,0 +1,145 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: lifetime
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Ariyan Zohoorian
8
+ - S M Shahnewaz
9
+ autorequire:
10
+ bindir: exe
11
+ cert_chain: []
12
+ date: 2020-03-20 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activerecord
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - "~>"
19
+ - !ruby/object:Gem::Version
20
+ version: 5.0.0
21
+ type: :development
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "~>"
26
+ - !ruby/object:Gem::Version
27
+ version: 5.0.0
28
+ - !ruby/object:Gem::Dependency
29
+ name: sqlite3
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - "~>"
33
+ - !ruby/object:Gem::Version
34
+ version: 1.3.6
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - "~>"
40
+ - !ruby/object:Gem::Version
41
+ version: 1.3.6
42
+ - !ruby/object:Gem::Dependency
43
+ name: database_cleaner
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - "~>"
47
+ - !ruby/object:Gem::Version
48
+ version: 1.7.0
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - "~>"
54
+ - !ruby/object:Gem::Version
55
+ version: 1.7.0
56
+ - !ruby/object:Gem::Dependency
57
+ name: bundler
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'
70
+ - !ruby/object:Gem::Dependency
71
+ name: rake
72
+ requirement: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - "~>"
75
+ - !ruby/object:Gem::Version
76
+ version: '13.0'
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - "~>"
82
+ - !ruby/object:Gem::Version
83
+ version: '13.0'
84
+ - !ruby/object:Gem::Dependency
85
+ name: rspec
86
+ requirement: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - "~>"
89
+ - !ruby/object:Gem::Version
90
+ version: '3.0'
91
+ type: :development
92
+ prerelease: false
93
+ version_requirements: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - "~>"
96
+ - !ruby/object:Gem::Version
97
+ version: '3.0'
98
+ description: Provides a common set of APIs for ActiveRecord models that have a start
99
+ and end time
100
+ email:
101
+ - azohoori@cisco.com
102
+ - sshahnew@cisco.com
103
+ executables: []
104
+ extensions: []
105
+ extra_rdoc_files: []
106
+ files:
107
+ - ".github/workflows/gempush.yml"
108
+ - ".github/workflows/ruby.yml"
109
+ - ".gitignore"
110
+ - ".rspec"
111
+ - ".travis.yml"
112
+ - Gemfile
113
+ - Gemfile.lock
114
+ - LICENSE.txt
115
+ - README.md
116
+ - Rakefile
117
+ - bin/console
118
+ - bin/setup
119
+ - lib/lifetime.rb
120
+ - lib/lifetime/version.rb
121
+ - lifetime.gemspec
122
+ homepage: https://github.com/Cisco-AMP/lifetime
123
+ licenses:
124
+ - MIT
125
+ metadata: {}
126
+ post_install_message:
127
+ rdoc_options: []
128
+ require_paths:
129
+ - lib
130
+ required_ruby_version: !ruby/object:Gem::Requirement
131
+ requirements:
132
+ - - ">="
133
+ - !ruby/object:Gem::Version
134
+ version: '0'
135
+ required_rubygems_version: !ruby/object:Gem::Requirement
136
+ requirements:
137
+ - - ">="
138
+ - !ruby/object:Gem::Version
139
+ version: '0'
140
+ requirements: []
141
+ rubygems_version: 3.0.3
142
+ signing_key:
143
+ specification_version: 4
144
+ summary: Start and end timestamp logic for ActiveRecord
145
+ test_files: []