five-star 0.1.0

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: 408c38478386dfd30afac94401ef9f609a4102c1
4
+ data.tar.gz: ce73a704c2f2ef208f45d5ab95759e05f30f19be
5
+ SHA512:
6
+ metadata.gz: f8fb0c43adac3af9fd729f1326ab1e3b8dfc0e18ea1e72f4a7065582cdfc5ece24ca2b7b7c5d867380ca6919d06ab1d80a8f3037ab973b1fcee873962c9af6ee
7
+ data.tar.gz: f988375e92f8224ae05b7d5956da34134fe47080ea74d5d1510631017760ad8e184e3bd81d57e17fc2682fb3a272aee7b80e3110ca57fd7642f1238715070355
@@ -0,0 +1,35 @@
1
+ *.gem
2
+ *.rbc
3
+ /.config
4
+ /coverage/
5
+ /InstalledFiles
6
+ /pkg/
7
+ /spec/reports/
8
+ /test/tmp/
9
+ /test/version_tmp/
10
+ /tmp/
11
+
12
+ ## Specific to RubyMotion:
13
+ .dat*
14
+ .repl_history
15
+ build/
16
+
17
+ ## Documentation cache and generated files:
18
+ /.yardoc/
19
+ /_yardoc/
20
+ /doc/
21
+ /rdoc/
22
+
23
+ ## Environment normalisation:
24
+ /.bundle/
25
+ /vendor/bundle
26
+ /lib/bundler/man/
27
+
28
+ # for a library or gem, you might want to ignore these files since the code is
29
+ # intended to run in multiple environments; otherwise, check them in:
30
+ Gemfile.lock
31
+ .ruby-version
32
+ .ruby-gemset
33
+
34
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
35
+ .rvmrc
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,18 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.0
4
+ - 2.1
5
+ - 2.2
6
+ - jruby
7
+ - rbx
8
+ - ruby-head
9
+ matrix:
10
+ allow_failures:
11
+ - rvm: ruby-head
12
+ - rvm: rbx
13
+
14
+ before_install: gem install bundler
15
+
16
+ addons:
17
+ code_climate:
18
+ repo_token: aaa2bcd71ac641474a13caabe294beeb26abcc7c4dd82f3395a7b9193bb20d02
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in five-star.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Robert Murray
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 all
13
+ 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 THE
21
+ SOFTWARE.
22
+
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Rob Murray
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.
@@ -0,0 +1,167 @@
1
+ # FiveStar
2
+ ### A generic rating library :star:
3
+
4
+ [![Build Status](https://travis-ci.org/rob-murray/five-star.svg?branch=master)](https://travis-ci.org/rob-murray/five-star)
5
+ [![Code Climate](https://codeclimate.com/github/rob-murray/five-star.png)](https://codeclimate.com/github/rob-murray/five-star)
6
+ [![Coverage Status](https://coveralls.io/repos/rob-murray/five-star/badge.png)](https://coveralls.io/r/rob-murray/five-star)
7
+ [![Dependency Status](https://gemnasium.com/rob-murray/five-star.svg)](https://gemnasium.com/rob-murray/five-star)
8
+ [![Gem Version](https://badge.fury.io/rb/five-star.svg)](http://badge.fury.io/rb/five-star)
9
+
10
+ :star: **FiveStar** :star: is a library to build a rating system - it allows you to rate *something* in your domain by classification or criteria you define. This library gives you the structure to rate your object with as many of these different classifications as you like with the overall rating as a weighted average.
11
+
12
+ This uses Plain Old Ruby Objects so can be used in any project. Implement or use whatever persistence layer you want.
13
+
14
+ > Not sure if this is a gem or just a code design pattern :/
15
+
16
+ ## Example
17
+
18
+ Say you implemented a film rating system, you have lots of `Film` objects each with different attributes and you would like to show an overall rating for each film based on classifications below.
19
+
20
+ * Gore - Amount of blood in the movie - weighting: 40%
21
+ * Sex - Number of references to sex in the movie - weighting: 40%
22
+ * Swearing - Number of swear words in the movie - weighting: 20%
23
+
24
+ This could be implemented as follows.
25
+
26
+ ```ruby
27
+ class Film
28
+ include FiveStar.rateable
29
+
30
+ rate_with GoreRater, SwearingRater, SexRater
31
+
32
+ # rest of you implementation
33
+ end
34
+
35
+ class GoreRater < FiveStar.base_rater
36
+ rating_weight 0.4
37
+
38
+ def description
39
+ "This Film was rated #{rating} for gore"
40
+ end
41
+
42
+ def rating
43
+ # count the pints of blood spilt in the film
44
+ end
45
+ end
46
+
47
+ class SwearingRater < FiveStar.base_rater
48
+ rating_weight 0.2
49
+
50
+ def description
51
+ "This Film was has #{number_of_swear_words} and was rated at #{rating}"
52
+ end
53
+
54
+ def rating
55
+ # count the number of swear words in the film
56
+ end
57
+ end
58
+
59
+ film = Film.new
60
+ film.rating # => 6
61
+ film.rating_descriptions # => ["This Film was rated 8 for gore", ...]
62
+
63
+ ```
64
+
65
+ ## Installation
66
+
67
+ Add this line to your application's Gemfile:
68
+
69
+ ```ruby
70
+ gem 'five-star'
71
+ ```
72
+
73
+ And then execute:
74
+
75
+ $ bundle
76
+
77
+ Or install it yourself as:
78
+
79
+ $ gem install five-star
80
+
81
+
82
+ ## Usage
83
+
84
+ There are two components required, the thing being rated and how it is rated - the thing you want rated is defined as being rateable and you can have one or more "raters" to give it a rating. You must implement your these raters and provide the classes to the item being rated, this library takes care of the rest.
85
+
86
+ * `Rateable` - This is the object that can be rated based on
87
+ * `Rater` - A class that knows how to give a rating to the object being rated
88
+
89
+ Each `Rater` must return a rating within the scale given and a weighting which will be used to calculate the overall rating.
90
+
91
+ The current rating scale used is 0 - 10 as floating point numbers that can be rounded as you require.
92
+
93
+ ### Rateable
94
+
95
+ This module when included gives the object the following interface on the instance.
96
+
97
+ * `rating` - `[Float]` - The overall rating calculated
98
+ * `rating_descriptions` - `[Array]` - A list of the description from each rater class
99
+
100
+ The classes used to rate the object can be specified using the class method `rate_with(*class_names)` and passing in one or more rating classes.
101
+
102
+ ```ruby
103
+ class Film
104
+ include FiveStar.rateable
105
+
106
+ rate_with GoreRater, SwearingRater, SexRater
107
+ ...
108
+
109
+ film = Film.new
110
+ film.rating # => 6
111
+ film.rating_descriptions # => ["This Film was rated 8 for gore", ...]
112
+ ```
113
+
114
+ ### Rater
115
+
116
+ You can create your own rating classes however you like but they should respond to the following methods;
117
+
118
+ * `rating` - `[Float]` - The rating for the object being rated
119
+ * `description` - `[String]` - A description of the rating, eg reason it was rated at that value
120
+ * `weighting` - `[Float]` - The weighting for this classification
121
+
122
+ The **FiveStar** will call `build` method on the `Rater` with the argument of the instance of class being rated which should do any setup and return an instance of the rater.
123
+
124
+ For simplicity you can subclass `FiveStar.base_rater` and get the following for free.
125
+
126
+ * `rating_weight` - Class method to set the weighting for this rater
127
+ * `rateable` - Instance method referencing the object being rated
128
+ * `min_rating` - Instance method returning the minimum rating value
129
+ * `max_rating` - Instance method returning the maximum rating value
130
+
131
+ For example a basic version could be something like this;
132
+
133
+ ```ruby
134
+ class GoreRater < FiveStar.base_rater
135
+ rating_weight 0.4
136
+
137
+ def description
138
+ "This Film was rated #{rating} for gore"
139
+ end
140
+
141
+ def rating
142
+ # calculate rating somehow
143
+ end
144
+ end
145
+ ```
146
+
147
+ ### Rating calculation
148
+
149
+ The calculation used will be a weighted average based on each rating and the weighting. If weighting is not required then all will be use the default value and therefore be weighted the same which is just a normal mean average calculation.
150
+
151
+ The minimum rating is 0.0 and the maximum is 10.0
152
+
153
+
154
+ ## Development
155
+
156
+ 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.
157
+
158
+ 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).
159
+
160
+ ## Contributing
161
+
162
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/five-star. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
163
+
164
+
165
+ ## License
166
+
167
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -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
@@ -0,0 +1,11 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "five-star"
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
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,30 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "five-star/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "five-star"
8
+ spec.version = FiveStar::VERSION
9
+ spec.authors = ["Rob Murray"]
10
+ spec.email = ["robmurray17@gmail.com"]
11
+
12
+ spec.summary = %q{:star: A generic rating library :star:}
13
+ spec.description = %q{A Rating system for your Ruby objects; Rate an object with one or more rating classifications.}
14
+ spec.homepage = "https://github.com/rob-murray/five-star"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.required_ruby_version = ">= 2.0.0"
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.10"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ spec.add_development_dependency "rspec"
27
+ spec.add_development_dependency "pry"
28
+ spec.add_development_dependency "coveralls"
29
+ spec.add_development_dependency "codeclimate-test-reporter"
30
+ end
@@ -0,0 +1,16 @@
1
+ require "five-star/version"
2
+ require "five-star/base_rater"
3
+ require "five-star/rateable"
4
+ require "five-star/rating_calculator"
5
+
6
+ module FiveStar
7
+ class << self
8
+ def rateable
9
+ Rateable
10
+ end
11
+
12
+ def base_rater
13
+ BaseRater
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,43 @@
1
+ module FiveStar
2
+ class BaseRater
3
+ def self.build(rateable)
4
+ new(rateable)
5
+ end
6
+
7
+ def self.rating_weight(weighting)
8
+ @weighting = weighting
9
+ end
10
+
11
+ def self.weighting
12
+ @weighting ||= 1.0
13
+ end
14
+
15
+ def initialize(rateable)
16
+ @rateable = rateable
17
+ end
18
+
19
+ def description
20
+ "#{self.class} rated #{rateable.class} at #{rating} with weighting of #{weighting}"
21
+ end
22
+
23
+ def rating
24
+ 0
25
+ end
26
+
27
+ def weighting
28
+ self.class.weighting
29
+ end
30
+
31
+ protected
32
+
33
+ attr_reader :rateable
34
+
35
+ def max_rating
36
+ 10
37
+ end
38
+
39
+ def min_rating
40
+ 0
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,45 @@
1
+ module FiveStar
2
+ module Rateable
3
+ def self.included(base)
4
+ base.extend(ClassMethods)
5
+ end
6
+
7
+ module ClassMethods
8
+ def rate_with(*klasses)
9
+ @rating_klasses = Array(klasses)
10
+ end
11
+
12
+ def rating_klasses
13
+ @rating_klasses ||= []
14
+ end
15
+ end
16
+
17
+ def rating
18
+ rating_calculator.rate(raters)
19
+ end
20
+
21
+ def rating_descriptions
22
+ raters.map { |rater| rater.description }
23
+ end
24
+
25
+ protected
26
+
27
+ def rateable
28
+ self
29
+ end
30
+
31
+ private
32
+
33
+ def raters
34
+ @raters ||= rating_klasses.map { |rater| rater.build(rateable) }
35
+ end
36
+
37
+ def rating_klasses
38
+ rateable.class.rating_klasses
39
+ end
40
+
41
+ def rating_calculator
42
+ RatingCalculator
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,24 @@
1
+ module FiveStar
2
+ class RatingCalculator
3
+ def self.rate(raters)
4
+ new(raters).calculate_rating
5
+ end
6
+
7
+ def initialize(raters)
8
+ @raters = raters
9
+ end
10
+
11
+ def calculate_rating
12
+ return 0 unless raters.any?
13
+
14
+ sum_total = raters.map { |rater| rater.rating * rater.weighting }.inject(&:+)
15
+ weights_total = raters.map(&:weighting).inject(&:+)
16
+
17
+ sum_total / weights_total.to_f
18
+ end
19
+
20
+ private
21
+
22
+ attr_reader :raters
23
+ end
24
+ end
@@ -0,0 +1,3 @@
1
+ module FiveStar
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,146 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: five-star
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Rob Murray
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-11-14 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: coveralls
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: codeclimate-test-reporter
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: A Rating system for your Ruby objects; Rate an object with one or more
98
+ rating classifications.
99
+ email:
100
+ - robmurray17@gmail.com
101
+ executables: []
102
+ extensions: []
103
+ extra_rdoc_files: []
104
+ files:
105
+ - ".gitignore"
106
+ - ".rspec"
107
+ - ".travis.yml"
108
+ - CODE_OF_CONDUCT.md
109
+ - Gemfile
110
+ - LICENSE
111
+ - LICENSE.txt
112
+ - README.md
113
+ - Rakefile
114
+ - bin/console
115
+ - bin/setup
116
+ - five-star.gemspec
117
+ - lib/five-star.rb
118
+ - lib/five-star/base_rater.rb
119
+ - lib/five-star/rateable.rb
120
+ - lib/five-star/rating_calculator.rb
121
+ - lib/five-star/version.rb
122
+ homepage: https://github.com/rob-murray/five-star
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: 2.0.0
135
+ required_rubygems_version: !ruby/object:Gem::Requirement
136
+ requirements:
137
+ - - ">="
138
+ - !ruby/object:Gem::Version
139
+ version: '0'
140
+ requirements: []
141
+ rubyforge_project:
142
+ rubygems_version: 2.4.8
143
+ signing_key:
144
+ specification_version: 4
145
+ summary: ":star: A generic rating library :star:"
146
+ test_files: []