gioco 0.0.0 → 0.1.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.
@@ -0,0 +1,130 @@
1
+ module Gioco
2
+
3
+ class SetupGenerator < Rails::Generators::NamedBase
4
+ source_root File.expand_path("../../templates", __FILE__)
5
+
6
+ desc "Setup Gioco for some resource"
7
+ class_option :points, :type => :boolean, :default => false, :desc => "Setup gioco with points-system based"
8
+
9
+ def generate_models
10
+ generate("model", "badge name:string #{(options[:points]) ? "points:integer" : ""} default:boolean")
11
+ generate("model", "level badge_id:integer #{file_name}_id:integer")
12
+ generate("migration", "add_points_to_#{file_name.pluralize} points:integer") if options[:points]
13
+ end
14
+
15
+ def creating_templates
16
+ @points = ( options[:points] ) ? true : false
17
+ template "gioco.rb", "config/initializers/gioco.rb"
18
+ end
19
+
20
+ def setup_relations
21
+ add_relationship( "badge", "levels", "has_many", false, "destroy" )
22
+ add_relationship( "badge", file_name.pluralize, "has_many", "levels" )
23
+
24
+ add_relationship( file_name, "levels", "has_many" )
25
+ add_relationship( file_name, "badges", "has_many", "levels" )
26
+
27
+ add_relationship( "level", file_name, "belongs_to" )
28
+ add_relationship( "level", "badge", "belongs_to" )
29
+ end
30
+
31
+ def create_rakes
32
+ rakefile("gioco.rake") do
33
+ <<-EOS
34
+ # -*- encoding: utf-8 -*-
35
+ namespace :gioco do
36
+
37
+ desc "Used to add a new badge at Gioco scheme"
38
+
39
+ task :add_badge, [:name, #{(options[:points]) ? ":points, " : ""}:default] => :environment do |t, args|
40
+ args.default = ( args.default ) ? eval(args.default) : false
41
+
42
+ if !args.name #{(options[:points]) ? "&& !args.points" : ""}
43
+ puts "There are missing some arguments"
44
+
45
+ else
46
+ badge = Badge.create({
47
+ :name => args.name,
48
+ #{(options[:points]) ? ":points => args.points," : ""}
49
+ :default => args.default
50
+ })
51
+
52
+ if args.default
53
+ resources = #{file_name.capitalize}.find(:all)
54
+ resources.each do |r|
55
+ r.badges << badge
56
+ end
57
+ end
58
+
59
+ end
60
+
61
+ end
62
+
63
+ desc "Used to remove an old badge at Gioco scheme"
64
+
65
+ task :remove_badge, [:name] => :environment do |t, args|
66
+
67
+ if !args.name
68
+ puts "There are missing some arguments"
69
+
70
+ else
71
+ badge = Badge.find_by_name( args.name )
72
+ badge.destroy
73
+ end
74
+
75
+ end
76
+
77
+ end
78
+ EOS
79
+ end
80
+ end
81
+
82
+ def migrating
83
+ puts <<-EOS
84
+
85
+ =======================================
86
+ > Now is time to run rake db:migrate
87
+ =======================================
88
+
89
+ EOS
90
+ rake("db:migrate")
91
+ end
92
+
93
+ def instructions
94
+ puts <<-EOS
95
+
96
+ =======================================================
97
+
98
+ Gioco successfully installed.
99
+
100
+ Now you are able to add Badges using:
101
+ rake gioco:add_badge[BADGE_NAME,POINTS,DEFAULT]
102
+
103
+ If you installed gioco without points option:
104
+ rake gioco:add_badge[BADGE_NAME,DEFAULT]
105
+
106
+ And to remove Badges using:
107
+ rake gioco:remove_badge[BADGE_NAME]
108
+
109
+ For usage and more infomation go to the documentation:
110
+ http://joaomdmoura.github.com/gioco/
111
+
112
+ =======================================================
113
+
114
+ EOS
115
+ end
116
+
117
+ private
118
+
119
+ def add_relationship ( model, related, relation, through = false, dependent = false )
120
+ gsub_file "app/models/#{model}.rb", get_class_header(model), "#{get_class_header(model)}
121
+ #{relation} :#{related} #{(through) ? ", :through => :#{through}" : ""} #{(dependent) ? ", :dependent => :#{dependent}" : ""}"
122
+ end
123
+
124
+ def get_class_header ( model_name )
125
+ "class #{model_name.capitalize} < ActiveRecord::Base"
126
+ end
127
+
128
+ end
129
+
130
+ end
@@ -0,0 +1,2 @@
1
+ Gioco::Resources::NAME = "<%= file_name %>"
2
+ Gioco::Resources::POINTS = <%= @points %>
data/lib/gioco.rb CHANGED
@@ -1,3 +1,68 @@
1
1
  module Gioco
2
2
 
3
+ class Core
4
+
5
+ def self.get_resource( rid )
6
+ eval(Gioco::Resources::NAME.capitalize).find( rid )
7
+ end
8
+
9
+ def self.get_badge( bid )
10
+ Badge.find( bid )
11
+ end
12
+
13
+ def self.get_level( rid, bid )
14
+ Level.where( "#{Gioco::Resources::NAME}_id = #{rid} AND badge_id = #{bid}" )[0]
15
+ end
16
+
17
+ end
18
+
19
+ class Resources < Core
20
+
21
+ def self.change_points( rid, points, resource = false )
22
+ resource = get_resource( rid ) if !resource
23
+ new_pontuation = resource.points.to_i + points
24
+ old_pontuation = resource.points.to_i
25
+ status = nil
26
+
27
+ resource.update_attributes( {:points => new_pontuation } )
28
+
29
+ if old_pontuation < new_pontuation
30
+
31
+ related_badges = Badge.where( "points <= #{new_pontuation}" )
32
+
33
+ related_badges.each do |badge|
34
+ Badges.add( nil, nil, resource, badge )
35
+ end
36
+
37
+ else
38
+
39
+ related_badges = Badge.where( "points > #{new_pontuation} AND points <= #{old_pontuation}" )
40
+
41
+ related_badges.each do |badge|
42
+ Badges.remove( resource.id, badge.id )
43
+ end
44
+
45
+ end
46
+
47
+ end
48
+
49
+ end
50
+
51
+ class Badges < Core
52
+
53
+ def self.add( rid, badge_id, resource = false, badge = false )
54
+ resource = get_resource( rid ) if !resource
55
+ badge = get_badge( badge_id ) if !badge
56
+
57
+ resource.update_attributes( {:points => badge.points } ) if Gioco::Resources::POINTS && badge.points > resource.points.to_i
58
+ resource.badges << badge if !resource.badges.include?(badge)
59
+ end
60
+
61
+ def self.remove( rid, badge_id, resource = false, badge = false )
62
+ level = get_level( rid, badge_id )
63
+ level.destroy
64
+ end
65
+
66
+ end
67
+
3
68
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gioco
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.0
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,31 +9,19 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-03-04 00:00:00.000000000Z
13
- dependencies:
14
- - !ruby/object:Gem::Dependency
15
- name: cucumber
16
- requirement: !ruby/object:Gem::Requirement
17
- none: false
18
- requirements:
19
- - - ! '>='
20
- - !ruby/object:Gem::Version
21
- version: '0'
22
- type: :development
23
- prerelease: false
24
- version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
- requirements:
27
- - - ! '>='
28
- - !ruby/object:Gem::Version
29
- version: '0'
30
- description: A gamification gem to Ruby applications.
12
+ date: 2012-04-25 00:00:00.000000000Z
13
+ dependencies: []
14
+ description: Gioco is a easy to implement gamification gem based on plug and play
15
+ concept.Doesn't matter if you already have a full and functional database, Gioco
16
+ will smoothly integrateeverything and provide all methods that you might need.
31
17
  email: joaomdmoura@gmail.com
32
18
  executables: []
33
19
  extensions: []
34
20
  extra_rdoc_files: []
35
21
  files:
36
22
  - lib/gioco.rb
23
+ - lib/generators/gioco/setup_generator.rb
24
+ - lib/generators/templates/gioco.rb
37
25
  - init.rb
38
26
  homepage: http://joaomdmoura.github.com/gioco/
39
27
  licenses: []
@@ -58,5 +46,5 @@ rubyforge_project:
58
46
  rubygems_version: 1.8.18
59
47
  signing_key:
60
48
  specification_version: 3
61
- summary: A gamification gem to Ruby applications.
49
+ summary: A gamification gem to Ruby on Rails applications.
62
50
  test_files: []