nano_metrics 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d14b04c3a2a3daa65b41c5c61ab1c32fc4bdacd4
4
+ data.tar.gz: c95f98df2ac8c656571245acf3edda24521a379b
5
+ SHA512:
6
+ metadata.gz: 95b523ec4d5ffd19c1d98c56ea811e29616cdfef52a7927bfef4950795868452857827472958ea9d42246cb60734b6008cf10f9fb00570a65e8c5fe8ca30259f
7
+ data.tar.gz: 5d60f0aa2585cda50e7a0ffbca9b5a2a27e44a7999329979db56ad4cadd2c0d8eaff539f9a6e99306b1afb292fc773eaf4f5ad3c997bdd5f7a210d09b8f82ea1
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in nano_metrics.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Pat Allan and Inspire9
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,29 @@
1
+ # NanoMetrics
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'nano_metrics', '0.0.1'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install nano_metrics
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
@@ -0,0 +1 @@
1
+ require 'bundler/gem_tasks'
@@ -0,0 +1,26 @@
1
+ class NanoMetrics::Context < ActiveRecord::Base
2
+ self.table_name = 'nano_contexts'
3
+
4
+ has_many :metrics, class_name: 'NanoMetrics::Metric'
5
+ belongs_to :metricable, polymorphic: true
6
+
7
+ validates :metricable, presence: true
8
+ validates :action, presence: true, uniqueness: {
9
+ scope: [:metricable_id, :metricable_type]
10
+ }
11
+
12
+ def self.find_or_create(action, metricable)
13
+ for_action_and_metricable(action, metricable) || create(
14
+ action: action,
15
+ metricable: metricable
16
+ )
17
+ end
18
+
19
+ def self.for_action_and_metricable(action, metricable)
20
+ where(
21
+ action: action,
22
+ metricable_id: metricable.id,
23
+ metricable_type: metricable.class.name
24
+ ).first
25
+ end
26
+ end
@@ -0,0 +1,16 @@
1
+ class NanoMetrics::Metric < ActiveRecord::Base
2
+ self.table_name = 'nano_metrics'
3
+
4
+ belongs_to :context
5
+
6
+ validates :context, presence: true
7
+ validates :time, presence: true
8
+
9
+ before_validation :set_time, on: :create
10
+
11
+ private
12
+
13
+ def set_time
14
+ self.time ||= Time.zone.now
15
+ end
16
+ end
@@ -0,0 +1,27 @@
1
+ class CreateNanoMetricsTables < ActiveRecord::Migration
2
+ def up
3
+ create_table :nano_metrics do |t|
4
+ t.integer :context_id, null: false
5
+ t.datetime :time, null: false
6
+ t.timestamps
7
+ end
8
+
9
+ add_index :nano_metrics, :context_id
10
+
11
+ create_table :nano_contexts do |t|
12
+ t.string :action, null: false
13
+ t.string :metricable_type, null: false
14
+ t.integer :metricable_id, null: false
15
+ end
16
+
17
+ add_index :nano_contexts, :action
18
+ add_index :nano_contexts, [:metricable_type, :metricable_id]
19
+ add_index :nano_contexts, [:action, :metricable_type, :metricable_id],
20
+ unique: true, name: 'unique_nano_contexts'
21
+ end
22
+
23
+ def down
24
+ drop_table :nano_contexts
25
+ drop_table :nano_metrics
26
+ end
27
+ end
@@ -0,0 +1,7 @@
1
+ module NanoMetrics
2
+ def self.increment(action, object)
3
+ NanoMetrics::Context.find_or_create(action, object).metrics.create
4
+ end
5
+ end
6
+
7
+ require 'nano_metrics/engine'
@@ -0,0 +1,5 @@
1
+ require 'rails/engine'
2
+
3
+ class NanoMetrics::Engine < Rails::Engine
4
+ engine_name :nano_metrics
5
+ end
@@ -0,0 +1,20 @@
1
+ # coding: utf-8
2
+ Gem::Specification.new do |spec|
3
+ spec.name = 'nano_metrics'
4
+ spec.version = '0.0.1'
5
+ spec.authors = ['Pat Allan']
6
+ spec.email = ['pat@freelancing-gods.com']
7
+ spec.summary = %q{Simple metrics as a Rails Engine}
8
+ spec.description = %q{Track metrics within your Rails app.}
9
+ spec.homepage = ''
10
+ spec.license = 'MIT'
11
+
12
+ spec.files = `git ls-files`.split($/)
13
+ spec.test_files = `git ls-files -- {spec}/*`.split("\n")
14
+ spec.require_paths = ['lib']
15
+
16
+ spec.add_runtime_dependency 'activerecord', '>= 3.2.0'
17
+ spec.add_development_dependency 'combustion', '~> 0.5.1'
18
+ spec.add_development_dependency 'rspec-rails', '~> 2.13'
19
+ spec.add_development_dependency 'sqlite3', '~> 1.3.7'
20
+ end
@@ -0,0 +1,30 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'Saving metrics' do
4
+ let(:user) { User.create! }
5
+
6
+ it "saves a new value" do
7
+ NanoMetrics.increment :view, user
8
+
9
+ NanoMetrics::Context.for_action_and_metricable(:view, user).metrics.count.
10
+ should == 1
11
+ end
12
+
13
+ it "saves new contexts if they don't exist" do
14
+ NanoMetrics.increment :view, user
15
+
16
+ NanoMetrics::Context.where(
17
+ action: 'view', metricable_id: user.id, metricable_type: 'User'
18
+ ).count.should == 1
19
+ end
20
+
21
+ it "re-uses existing contexts when appropriate" do
22
+ NanoMetrics::Context.create action: :view, metricable: user
23
+
24
+ NanoMetrics.increment :view, user
25
+
26
+ NanoMetrics::Context.where(
27
+ action: 'view', metricable_id: user.id, metricable_type: 'User'
28
+ ).count.should == 1
29
+ end
30
+ end
@@ -0,0 +1,3 @@
1
+ class User < ActiveRecord::Base
2
+ #
3
+ end
@@ -0,0 +1,3 @@
1
+ test:
2
+ adapter: sqlite3
3
+ database: db/combustion_test.sqlite
@@ -0,0 +1,6 @@
1
+ ActiveRecord::Schema.define do
2
+ create_table :users, :force => true do |t|
3
+ t.string :name
4
+ t.timestamps
5
+ end
6
+ end
@@ -0,0 +1 @@
1
+ *.log
@@ -0,0 +1,11 @@
1
+ require 'bundler'
2
+
3
+ Bundler.require :default, :development
4
+
5
+ Combustion.initialize! :active_record
6
+
7
+ require 'rspec/rails'
8
+
9
+ RSpec.configure do |config|
10
+ config.use_transactional_fixtures = true
11
+ end
metadata ADDED
@@ -0,0 +1,118 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: nano_metrics
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Pat Allan
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-08-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activerecord
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: 3.2.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: 3.2.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: combustion
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: 0.5.1
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: 0.5.1
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec-rails
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '2.13'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '2.13'
55
+ - !ruby/object:Gem::Dependency
56
+ name: sqlite3
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 1.3.7
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: 1.3.7
69
+ description: Track metrics within your Rails app.
70
+ email:
71
+ - pat@freelancing-gods.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - .gitignore
77
+ - Gemfile
78
+ - LICENSE.txt
79
+ - README.md
80
+ - Rakefile
81
+ - app/models/nano_metrics/context.rb
82
+ - app/models/nano_metrics/metric.rb
83
+ - db/migrate/1_create_nano_metrics_tables.rb
84
+ - lib/nano_metrics.rb
85
+ - lib/nano_metrics/engine.rb
86
+ - nano_metrics.gemspec
87
+ - spec/acceptance/saving_metrics_spec.rb
88
+ - spec/internal/app/models/user.rb
89
+ - spec/internal/config/database.yml
90
+ - spec/internal/db/combustion_test.sqlite
91
+ - spec/internal/db/schema.rb
92
+ - spec/internal/log/.gitignore
93
+ - spec/spec_helper.rb
94
+ homepage: ''
95
+ licenses:
96
+ - MIT
97
+ metadata: {}
98
+ post_install_message:
99
+ rdoc_options: []
100
+ require_paths:
101
+ - lib
102
+ required_ruby_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - '>='
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ required_rubygems_version: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - '>='
110
+ - !ruby/object:Gem::Version
111
+ version: '0'
112
+ requirements: []
113
+ rubyforge_project:
114
+ rubygems_version: 2.0.2
115
+ signing_key:
116
+ specification_version: 4
117
+ summary: Simple metrics as a Rails Engine
118
+ test_files: []