acts_as_monitor 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .svn
4
+ .bundle
5
+ .config
6
+ .yardoc
7
+ Gemfile.lock
8
+ InstalledFiles
9
+ _yardoc
10
+ coverage
11
+ doc/
12
+ lib/bundler/man
13
+ pkg
14
+ rdoc
15
+ spec/reports
16
+ test/tmp
17
+ test/version_tmp
18
+ tmp
data/CHANGELOG.md ADDED
@@ -0,0 +1,8 @@
1
+ 0.1.0 ToDo
2
+ - Ajax to have warning and error list clicking on icon
3
+
4
+ 0.0.2 (May 23, 2011)
5
+ - Working copy
6
+
7
+ 0.0.1 (May 23, 2011)
8
+ - First deploy not working
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in acts_as_monitor.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 TODO: Write your name
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.
data/README.md ADDED
@@ -0,0 +1,69 @@
1
+ # ActsAsMonitor
2
+
3
+ Provide a way to monitor the status of a model.
4
+ The health of the model is checked by private method, defined by user
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ gem 'acts_as_monitor'
11
+
12
+ And then execute:
13
+
14
+ $ bundle
15
+
16
+ Or install it yourself as:
17
+
18
+ $ gem install acts_as_monitor
19
+
20
+ Copy icons and other files
21
+
22
+ rails g acts_as_monitor:install
23
+
24
+ ## Usage
25
+ #app/models/user.rb
26
+ class User < ActiveRecord::Base
27
+ acts_as_monitor
28
+
29
+ ... your code ...
30
+
31
+ private
32
+
33
+ def warn_stange?
34
+ ... do some checks and return true or false
35
+ true
36
+ end
37
+
38
+ def warn_not_now?
39
+ ... do some checks and return true or false
40
+ false
41
+ end
42
+
43
+ def error_terrible?
44
+ ... do some checks and return true or false
45
+ true
46
+ end
47
+ end
48
+
49
+ You can now check the status of your model using the following code:
50
+ @user = User.new
51
+
52
+ @user.status
53
+ > {:warn => [:warn_strange?], :error => [:error_terrible?]}
54
+
55
+ @user.status_flag
56
+ > :red
57
+
58
+ You can use the monitor_tag helper to view a red/green/yellow rapresentation:
59
+ #app/views/users/index.html.erb
60
+ ...
61
+ <%= monitor_tag(@user) %>
62
+
63
+ ## Contributing
64
+
65
+ 1. Fork it
66
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
67
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
68
+ 4. Push to the branch (`git push origin my-new-feature`)
69
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+ require 'rake/testtask'
4
+ #~ RSpec::Core::RakeTask.new
5
+
6
+ #~ task :default => :spec
7
+ #~ task :test => :spec
8
+ Rake::TestTask.new do |t|
9
+ t.libs << "test"
10
+ t.test_files = FileList['test/**/*_test.rb']
11
+ t.verbose = true
12
+ end
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/acts_as_monitor/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Andrea Bignozzi"]
6
+ gem.email = ["skylord73@gmail.com"]
7
+ gem.description = %q{Monitor model via private methods warn_*? and error_*?}
8
+ gem.summary = %q{Monitor models }
9
+
10
+ gem.files = `git ls-files`.split($\)
11
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
12
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
13
+ gem.name = "acts_as_monitor"
14
+ gem.require_paths = ["lib"]
15
+ gem.version = ActsAsMonitor::VERSION
16
+
17
+ gem.add_dependency "rails", "3.0.9"
18
+
19
+ gem.add_development_dependency "rake"
20
+ gem.add_development_dependency "rspec"
21
+
22
+ end
@@ -0,0 +1,7 @@
1
+ module ActsAsMonitor
2
+ class MonitorController < ::ApplicationController
3
+
4
+ def index
5
+ end
6
+
7
+ end
@@ -0,0 +1,11 @@
1
+ module ActsAsMonitor
2
+ module Helper
3
+ def monitor_tag(object)
4
+ raise ActsAsMonitor::MissingMethod unless object.respond_to?(:status_flag)
5
+ icon = I18n.t(object.status_flag, :scope => "acts_as_monitor.icons",
6
+ :default => "acts_as_monitor_#{object.status_flag.to_s}.png")
7
+ image_tag icon, :alt => I18n.t(:alt_image, :scope => "acts_as_monitor.icons",
8
+ :default => "Click to view details")
9
+ end
10
+ end
11
+ end
@@ -0,0 +1 @@
1
+ <p>My engine page</p>
data/config/routes.rb ADDED
@@ -0,0 +1,4 @@
1
+ # CURRENT FILE :: config/routes.rb
2
+ Rails.application.routes.draw do
3
+ get "monitor" => "acts_as_monitor/monitor#index", :as => :acts_as_monitor
4
+ end
@@ -0,0 +1,15 @@
1
+ require "acts_as_monitor/version"
2
+ require "acts_as_monitor/monitor"
3
+ require File.dirname(__FILE__) + '/../app/helpers/monitor_helper'
4
+
5
+ module ActsAsMonitor
6
+ class MissingMethod < StandardError
7
+ def message
8
+ I18n.t(:missing_method, :scope => "acts_as_monitor.errors", :default => "Add acts_as_monitor to your class")
9
+ end
10
+ end
11
+
12
+ #Include my Helper to ActionView (as they where in application_helper)
13
+ ActionView::Base.send :include, ActsAsMonitor::Helper
14
+
15
+ end
@@ -0,0 +1,15 @@
1
+ module ActsAsMonitor
2
+ class Engine < Rails::Engine
3
+ initialize "team_page.load_app_instance_data" do |app|
4
+ TeamPage.setup do |config|
5
+ config.app_root = app.root
6
+ end
7
+ end
8
+
9
+ initialize "team_page.load_static_assets" do |app|
10
+ app.middleware.use ::ActionDispatch::Static, "#{root}/public"
11
+ end
12
+
13
+ end
14
+
15
+ end
@@ -0,0 +1,9 @@
1
+ #Defines Custom Exceptions
2
+ module ActsAsMonitor
3
+
4
+ class MissingMethod < StandardError
5
+ def message
6
+ I18n.t(:missing_method, :scope => "acts_as_monitor.errors", :default => "Add acts_as_monitor to your class")
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,74 @@
1
+ module ActsAsMonitor
2
+ module Monitor
3
+ def self.included(base) # :nodoc:
4
+ base.send :extend, ClassMethods
5
+ end
6
+
7
+ # Class methods for the mixin
8
+ module ClassMethods
9
+ # defines the class method to inject monitor methods
10
+ #
11
+ #==Example
12
+ # class MyModel < ActiveRecord::Base
13
+ # acts_as_monitor
14
+ # ...
15
+ # private
16
+ #
17
+ # def warn_test?
18
+ # whatever you want that return true in a warning condition
19
+ # end
20
+ #
21
+ # def error_test?
22
+ # whatever you want that return true in an error condition
23
+ # end
24
+ def acts_as_monitor(options={})
25
+ #~ cattr_accessor :xlsx_i18n, :xlsx_columns
26
+ #~ self.xlsx_i18n = options.delete(:i18n) || false
27
+ #~ self.xlsx_columns = options.delete(:columns) || self.column_names.map { |c| c = c.to_sym }
28
+ extend ActsAsMonitor::Monitor::SingletonMethods
29
+ include ActsAsMonitor::Monitor::InstanceMethods
30
+ end
31
+
32
+
33
+ end
34
+
35
+ # Singleton methods for the mixin
36
+ module SingletonMethods
37
+ end
38
+
39
+ #Instance methods for the mixin
40
+ module InstanceMethods
41
+ #Used to have full status description
42
+ #
43
+ # {:wan => [wan_name1?, ...], :error => [error_name1?...]}
44
+ #where warn_name1? and error_name1? are private instance method defined in the monitored model
45
+ def status
46
+ search = /^(warn|error)_.*\?/
47
+ out = {:warn => [], :error => []}
48
+ methods = self.private_methods.map{|m| m.match(search) }.compact
49
+ methods.each do |method|
50
+ out[method[1].to_sym] << method[0].to_sym if send(method[0])
51
+ end
52
+ out
53
+ end
54
+
55
+ #Used to have a quick status of the model
56
+ #* :red => Errors
57
+ #* :yellow => Warnings
58
+ #* :green => Nornal status
59
+ def status_flag
60
+ st = status
61
+ return :red unless st[:error].blank?
62
+ return :yellow unless st[:warn].blank?
63
+ return :green
64
+ end
65
+ end
66
+
67
+
68
+ end
69
+
70
+ end
71
+
72
+ require 'active_record'
73
+ ActiveRecord::Base.send :include, ActsAsMonitor::Monitor
74
+
@@ -0,0 +1,3 @@
1
+ module ActsAsMonitor
2
+ VERSION = "0.0.2"
3
+ end
@@ -0,0 +1,31 @@
1
+ require 'rails/generators'
2
+ module ActsAsMonitor
3
+ class InstallGenerator < Rails::Generators::Base
4
+ desc "Install generator for ActsAsMonitor gem"
5
+ source_root File.expand_path("../templates", __FILE__)
6
+
7
+ def copy_icons
8
+ icons = ["acts_as_monitor_green.png", "acts_as_monitor_red.png", "acts_as_monitor_yellow.png"]
9
+ icons.each do |icon|
10
+ destination = "public/images/" + icon
11
+ copy_file icon, destination
12
+ end
13
+ end
14
+
15
+ def copy_locales
16
+ file = "acts_as_monitor.it.yml"
17
+ destination = "config/locales/" + file
18
+ copy_file file, destination
19
+ end
20
+ end
21
+
22
+ def print_usage
23
+ usage = <<-START
24
+ Add acts_as_monitor to model to monitor
25
+ Add a private method warn_*? and error_*? to define warning and error status
26
+ Use monitor_tag(model_instance) in your view
27
+ START
28
+ puts(usage)
29
+ end
30
+
31
+ end
@@ -0,0 +1,9 @@
1
+ it:
2
+ acts_as_monitor:
3
+ icons:
4
+ red: acts_as_monitor_red.png
5
+ yellow: acts_as_monitor_yellow.png
6
+ green: acts_as_monitor_green.png
7
+ alt_image: "Clicca per visualizzare i dettagli"
8
+ errors:
9
+ missing_method: "Aggiungi acts_as_monitor alla classe da monitorare"
Binary file
data/test/database.yml ADDED
@@ -0,0 +1,3 @@
1
+ sqlite3:
2
+ adapter: sqlite3
3
+ database: test/acts_as_monitor.sqlite3.db
@@ -0,0 +1,47 @@
1
+ require 'test/unit'
2
+ require 'active_record'
3
+ require 'action_controller'
4
+ require 'action_controller/test_case'
5
+ require 'action_view/test_case'
6
+ require 'acts_as_monitor'
7
+
8
+ config = YAML::load(IO.read(File.dirname(__FILE__) + '/database.yml'))
9
+ ActiveRecord::Base.establish_connection(config['sqlite3'])
10
+
11
+ ActiveRecord::Schema.define(:version => 0) do
12
+ begin
13
+ drop_table :test_classes, :force => true
14
+ rescue
15
+ #dont really care if the tables are not dropped
16
+ end
17
+
18
+ create_table(:test_classes, :force => true) do |t|
19
+ t.string :name
20
+ end
21
+
22
+ end
23
+
24
+ class TestClassNoMonitor
25
+ end
26
+
27
+ class TestClass < ActiveRecord::Base
28
+ acts_as_monitor
29
+ attr_accessor :warn, :error
30
+
31
+ after_initialize do
32
+ @warn = false
33
+ @error = false
34
+ end
35
+
36
+
37
+ private
38
+
39
+ def warn_test?
40
+ @warn
41
+ end
42
+
43
+ def error_test?
44
+ @error
45
+ end
46
+
47
+ end
@@ -0,0 +1,52 @@
1
+ require 'test_helper'
2
+
3
+ class ActsAsMonitorTest < ActiveSupport::TestCase
4
+ include ActionView::Helpers
5
+ include ActsAsMonitor::Helper
6
+
7
+ test "Methods exists!" do
8
+ @test_class = TestClass.new
9
+ assert TestClass.respond_to?(:acts_as_monitor), "acts_as_monitor missing!"
10
+ assert @test_class.respond_to?(:status), "status missing!"
11
+ assert @test_class.respond_to?(:status_flag), "status_flag missing!"
12
+ end
13
+
14
+ test "Status ok" do
15
+ @test_class = TestClass.new()
16
+ assert @test_class.status == {:warn => [], :error => []} , "Error: #{@test_class.status.inspect}"
17
+ assert @test_class.status_flag == :green , "Error: #{@test_class.status_flag.inspect}"
18
+ end
19
+
20
+ test "Status warn" do
21
+ @test_class = TestClass.new(:warn => true)
22
+ assert @test_class.status == {:warn => [:warn_test? ], :error => []} , "Error: #{@test_class.status.inspect}"
23
+ assert @test_class.status_flag == :yellow , "Error: #{@test_class.status_flag.inspect}"
24
+ end
25
+
26
+ test "Status error" do
27
+ @test_class = TestClass.new(:error => true)
28
+ assert @test_class.status == {:warn => [], :error => [:error_test? ]}, "Error: #{@test_class.status.inspect}"
29
+ assert @test_class.status_flag == :red , "Error: #{@test_class.status_flag.inspect}"
30
+ end
31
+
32
+ test "Status error & warn " do
33
+ @test_class = TestClass.new(:error => true, :warn => true)
34
+ assert @test_class.status == {:warn => [:warn_test? ], :error => [:error_test? ]}, "Error: #{@test_class.status.inspect}"
35
+ assert @test_class.status_flag == :red , "Error: #{@test_class.status_flag.inspect}"
36
+ end
37
+
38
+ test "monitor_tag return image" do
39
+ @test_class = TestClass.new
40
+ @image = image_tag("acts_as_monitor_#{@test_class.status_flag.to_s}.png" , :alt => "Click to view details")
41
+ assert monitor_tag(@test_class) == @image , "fail monitor tag :#{monitor_tag(TestClass.new)}"
42
+ end
43
+
44
+ test "monitor_tag Rise exception" do
45
+ assert_raise ActsAsMonitor::MissingMethod do
46
+ monitor_tag(TestClassNoMonitor.new)
47
+ end
48
+ end
49
+
50
+
51
+
52
+ end
metadata ADDED
@@ -0,0 +1,138 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: acts_as_monitor
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 2
10
+ version: 0.0.2
11
+ platform: ruby
12
+ authors:
13
+ - Andrea Bignozzi
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2012-05-24 00:00:00 +02:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: rails
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - "="
28
+ - !ruby/object:Gem::Version
29
+ hash: 21
30
+ segments:
31
+ - 3
32
+ - 0
33
+ - 9
34
+ version: 3.0.9
35
+ type: :runtime
36
+ version_requirements: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ name: rake
39
+ prerelease: false
40
+ requirement: &id002 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ hash: 3
46
+ segments:
47
+ - 0
48
+ version: "0"
49
+ type: :development
50
+ version_requirements: *id002
51
+ - !ruby/object:Gem::Dependency
52
+ name: rspec
53
+ prerelease: false
54
+ requirement: &id003 !ruby/object:Gem::Requirement
55
+ none: false
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ hash: 3
60
+ segments:
61
+ - 0
62
+ version: "0"
63
+ type: :development
64
+ version_requirements: *id003
65
+ description: Monitor model via private methods warn_*? and error_*?
66
+ email:
67
+ - skylord73@gmail.com
68
+ executables: []
69
+
70
+ extensions: []
71
+
72
+ extra_rdoc_files: []
73
+
74
+ files:
75
+ - .gitignore
76
+ - CHANGELOG.md
77
+ - Gemfile
78
+ - LICENSE
79
+ - README.md
80
+ - Rakefile
81
+ - acts_as_monitor.gemspec
82
+ - app/controllers/acts_as_monitor/monitor_controller.rb
83
+ - app/helpers/monitor_helper.rb
84
+ - app/views/acts_as_monitor/indext.html.erb
85
+ - config/routes.rb
86
+ - lib/acts_as_monitor.rb
87
+ - lib/acts_as_monitor/engine.rb
88
+ - lib/acts_as_monitor/exceptions.rb
89
+ - lib/acts_as_monitor/monitor.rb
90
+ - lib/acts_as_monitor/version.rb
91
+ - lib/generators/acts_as_monitor/install/install_generator.rb
92
+ - lib/generators/acts_as_monitor/install/templates/acts_as_monitor.it.yml
93
+ - lib/generators/acts_as_monitor/install/templates/acts_as_monitor_green.png
94
+ - lib/generators/acts_as_monitor/install/templates/acts_as_monitor_red.png
95
+ - lib/generators/acts_as_monitor/install/templates/acts_as_monitor_yellow.png
96
+ - test/acts_as_monitor.sqlite3.db
97
+ - test/database.yml
98
+ - test/test_helper.rb
99
+ - test/units/acts_as_monitor_test.rb
100
+ has_rdoc: true
101
+ homepage:
102
+ licenses: []
103
+
104
+ post_install_message:
105
+ rdoc_options: []
106
+
107
+ require_paths:
108
+ - lib
109
+ required_ruby_version: !ruby/object:Gem::Requirement
110
+ none: false
111
+ requirements:
112
+ - - ">="
113
+ - !ruby/object:Gem::Version
114
+ hash: 3
115
+ segments:
116
+ - 0
117
+ version: "0"
118
+ required_rubygems_version: !ruby/object:Gem::Requirement
119
+ none: false
120
+ requirements:
121
+ - - ">="
122
+ - !ruby/object:Gem::Version
123
+ hash: 3
124
+ segments:
125
+ - 0
126
+ version: "0"
127
+ requirements: []
128
+
129
+ rubyforge_project:
130
+ rubygems_version: 1.3.7
131
+ signing_key:
132
+ specification_version: 3
133
+ summary: Monitor models
134
+ test_files:
135
+ - test/acts_as_monitor.sqlite3.db
136
+ - test/database.yml
137
+ - test/test_helper.rb
138
+ - test/units/acts_as_monitor_test.rb