maymay 0.0.1 → 0.0.2

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.
data/Gemfile CHANGED
@@ -1,3 +1,5 @@
1
1
  source "http://rubygems.org"
2
2
 
3
+ gem 'rake'
4
+
3
5
  gemspec
data/README.md CHANGED
@@ -1,5 +1,16 @@
1
+ [![Build Status](https://secure.travis-ci.org/redstonelabs/maymay.png)](http://travis-ci.org/redstonelabs/maymay)
2
+
1
3
  # MayMay is a dead simple auth solution for Rails without any magic
2
4
 
5
+ ## Installation
6
+
7
+ ```ruby
8
+ gem 'maymay'
9
+ # gem 'maymay', github: 'redstonelabs/maymay'
10
+ ```
11
+
12
+ ## Usage
13
+
3
14
  ```ruby
4
15
  ability = MayMay::Ability.new(user)
5
16
 
@@ -32,3 +43,9 @@ class PostsController < ApplicationController
32
43
  end
33
44
  end
34
45
  ```
46
+
47
+ If you are not authorized you'll get an `MayMay::Unauthorized` exception.
48
+
49
+ If you make a typo you'll get an `MayMay::UndefinedAbility` exception.
50
+
51
+ Yep. It's a simplified [cancan](https://github.com/ryanb/cancan) gem.
data/Rakefile CHANGED
@@ -1,7 +1,6 @@
1
1
  require 'rubygems'
2
- require 'rake'
3
2
  require 'rspec/core/rake_task'
4
3
 
5
4
  RSpec::Core::RakeTask.new
6
5
 
7
- task default: :spec
6
+ task :default => :spec
@@ -1,7 +1,7 @@
1
1
  module MayMay
2
2
  class Ability
3
3
  def initialize(user)
4
- @abilities = Hash.new([])
4
+ @abilities = {}
5
5
  @user = user
6
6
  end
7
7
 
@@ -10,8 +10,10 @@ module MayMay
10
10
  end
11
11
 
12
12
  def may?(action, subject, object = nil)
13
+ unless @abilities.has_key?(subject)
14
+ raise UndefinedAbility.new(action, subject)
15
+ end
13
16
  actions = @abilities[subject].find_all { |a| a[1] == action }
14
- raise UndefinedAbility if actions.empty?
15
17
  actions.all? do |a|
16
18
  a[0] == !!(!a[2] || a[2].call(@user, object))
17
19
  end
@@ -22,11 +24,13 @@ module MayMay
22
24
  end
23
25
 
24
26
  def may(action, subject, &block)
27
+ @abilities[subject] ||= []
25
28
  @abilities[subject] << [true, action, block]
26
29
  nil
27
30
  end
28
31
 
29
32
  def maynot(action, subject, &block)
33
+ @abilities[subject] ||= []
30
34
  @abilities[subject] << [false, action, block]
31
35
  nil
32
36
  end
@@ -9,5 +9,5 @@ end
9
9
  if defined? ActionController::Base
10
10
  ActionController::Base.send :include, MayMay::ControllerMethods
11
11
  ActionController::Base.helper_method :may?, :maynot?
12
- ActionController::Base.delegate :may?, :maynot?, :authorize!, to: :ability
12
+ ActionController::Base.delegate :may?, :maynot?, :authorize!, :to => :ability
13
13
  end
@@ -1,5 +1,11 @@
1
1
  module MayMay
2
2
  class UndefinedAbility < StandardError
3
+ TEMPLATE = "There are no action %s for %s"
4
+
5
+ def initialize(action, subject)
6
+ message = TEMPLATE % [action, subject].map(&:inspect)
7
+ super(message)
8
+ end
3
9
  end
4
10
 
5
11
  class Unauthorized < StandardError
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: maymay
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-10-26 00:00:00.000000000 Z
12
+ date: 2012-12-10 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
@@ -63,7 +63,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
63
63
  version: 1.3.4
64
64
  requirements: []
65
65
  rubyforge_project: maymay
66
- rubygems_version: 1.8.24
66
+ rubygems_version: 1.8.23
67
67
  signing_key:
68
68
  specification_version: 3
69
69
  summary: Abstract authorization solution for Rails.