repka 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in repka.gemspec
4
+ gemspec
@@ -0,0 +1,57 @@
1
+ # Repka gem — stats gathering for mongoid
2
+
3
+ Repka will help you easily gather stats from controllers if you use mongoid
4
+
5
+ ## Usage
6
+
7
+ `app/controllers/your_controller.rb`
8
+
9
+ ``` ruby
10
+ def index
11
+ rep :direct_visits # or inc :direct_visits
12
+ rep :visits, :source if params[:source]
13
+ rep :payed, nil, 3400.99 if params[:payed]
14
+ rep :payed_from, :paypal, 400 if params[:paypal]
15
+ rep :payed_from, :cash, 450.99 if params[:cash]
16
+ ```
17
+
18
+ `app/controllers/admin_controller.rb`
19
+
20
+ ``` ruby
21
+ def stats
22
+ @all_stats = repka_all_stats
23
+ @today_stats = repka_today_stats
24
+ @some_day_stats = RepkaStorage.first(:conditions => {:date => Date.new(2011,06,24)})
25
+ ```
26
+
27
+ ## Installation
28
+
29
+ Puts this line into `Gemfile` then run `$ bundle`:
30
+
31
+ ``` ruby
32
+ gem 'repka', '0.0.1'
33
+ ```
34
+
35
+ Or if you are old-school Rails 2 developer put this into `config/environment.rb` and run `$ rake gems:install`:
36
+
37
+ ``` ruby
38
+ config.gem 'repka', :version => '0.0.1'
39
+ ```
40
+
41
+ Or manually install gon gem: `$ gem install repka`
42
+
43
+ ## Contributors
44
+
45
+ * @gazay
46
+
47
+ ## License
48
+
49
+ The MIT License
50
+
51
+ Copyright (c) 2011 gazay
52
+
53
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
54
+
55
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
56
+
57
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,4 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'bundler'
4
+ Bundler::GemHelper.install_tasks
@@ -0,0 +1,11 @@
1
+ # encoding: utf-8
2
+
3
+ require 'action_controller'
4
+ require 'repka/base'
5
+
6
+ class RepkaStorage
7
+ include Mongoid::Document
8
+
9
+ field :date, type: Date, default: -> { Date.today }
10
+ field :storage, type: Hash, default: -> { Hash.new }
11
+ end
@@ -0,0 +1,66 @@
1
+ module Repka
2
+ module Base
3
+ def self.included base
4
+ base.send(:include, InstanceMethods)
5
+ end
6
+
7
+ module InstanceMethods
8
+ def rep(field, key=nil, value=nil)
9
+ field = field.to_s
10
+ key = key.to_s if key
11
+ value ||= 1
12
+
13
+ stat = RepkaStorage.first(:conditions => {:date => Date.today}) || RepkaStorage.create
14
+ storage = stat.storage
15
+ if storage[field].nil?
16
+ storage[field] = set_init(key, value)
17
+ elsif key && storage[field][key].nil?
18
+ storage[field] = storage[field].merge set_init(key, value)
19
+ end
20
+
21
+ if key.nil?
22
+ storage[field] += value
23
+ else
24
+ # OMG WTF WITH MONGO?! It can't rewrite hash of hashes
25
+ with_key = storage
26
+ stat.storage = {}
27
+ stat.save!
28
+ with_key[field][key] += value
29
+ stat.write_attribute(:storage, with_key)
30
+ end
31
+ stat.save!
32
+ end
33
+
34
+ def repka_all_stats
35
+ RepkaStorage.all
36
+ end
37
+
38
+ def repka_today_stats
39
+ RepkaStorage.first(:conditions => {:date => Date.today}) || RepkaStorage.create
40
+ end
41
+
42
+ private
43
+
44
+ def set_init(key=nil, value)
45
+ if key.nil?
46
+ if value.is_a? Integer
47
+ 0
48
+ elsif value.is_a? Float
49
+ 0.0
50
+ end
51
+ else
52
+ if value.is_a? Integer
53
+ { key => 0 }
54
+ elsif value.is_a? Float
55
+ { key => 0.0 }
56
+ end
57
+ end
58
+ end
59
+ end
60
+
61
+ end
62
+ end
63
+
64
+ class ActionController::Base
65
+ include Repka::Base
66
+ end
@@ -0,0 +1,3 @@
1
+ module Repka
2
+ VERSION = '0.0.1'
3
+ end
Binary file
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "repka/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "repka"
7
+ s.version = Repka::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ['gazay']
10
+ s.email = ['alex.gaziev@gmail.com']
11
+ s.homepage = ""
12
+ s.summary = %q{Repka will help you easily gather stats from controllers, if you use mongoid}
13
+ s.description = %q{Every day you can gathering smart stats with this tool}
14
+
15
+ s.rubyforge_project = "repka"
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+ end
metadata ADDED
@@ -0,0 +1,53 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: repka
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - gazay
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-06-06 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Every day you can gathering smart stats with this tool
15
+ email:
16
+ - alex.gaziev@gmail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - Gemfile
22
+ - README.md
23
+ - Rakefile
24
+ - lib/repka.rb
25
+ - lib/repka/base.rb
26
+ - lib/repka/version.rb
27
+ - pkg/repka-0.0.1.gem
28
+ - repka.gemspec
29
+ homepage: ''
30
+ licenses: []
31
+ post_install_message:
32
+ rdoc_options: []
33
+ require_paths:
34
+ - lib
35
+ required_ruby_version: !ruby/object:Gem::Requirement
36
+ none: false
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ required_rubygems_version: !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ! '>='
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ requirements: []
48
+ rubyforge_project: repka
49
+ rubygems_version: 1.7.2
50
+ signing_key:
51
+ specification_version: 3
52
+ summary: Repka will help you easily gather stats from controllers, if you use mongoid
53
+ test_files: []