fauxparse-credentials 1.0.0 → 1.0.1

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/History.txt CHANGED
@@ -1,3 +1,10 @@
1
+ === 1.0.1 / 2009-04-23
2
+
3
+ * Now sort of framework-agnostic. I say "sort of", because what I've actually
4
+ done is just copy and paste the chunks I needed out of ActiveSupport. I guess
5
+ the goal would be to rewrite around these, but since I basically never need
6
+ this for anything except Rails, it seems like overkill.
7
+
1
8
  === 1.0.0 / 2009-04-23
2
9
 
3
10
  * Gemification and README
data/Rakefile CHANGED
@@ -1,16 +1,3 @@
1
- # -*- ruby -*-
2
-
3
- require 'rubygems'
4
- require 'hoe'
5
- require './lib/credentials.rb'
6
-
7
- Hoe.new('credentials', Credentials::VERSION) do |p|
8
- # p.rubyforge_name = 'credentialsx' # if different than lowercase project name
9
- p.developer('Matt Powell', 'fauxparse@gmail.com')
10
- end
11
-
12
- # vim: syntax=Ruby
13
-
14
1
  require 'rake'
15
2
  require 'spec/rake/spectask'
16
3
  require 'rcov/rcovtask'
data/lib/credentials.rb CHANGED
@@ -1,2 +1,9 @@
1
+ require "credentials/actor"
2
+ require "credentials/rulebook"
1
3
  require "credentials/class_methods"
2
4
  require "credentials/inflector"
5
+ require "credentials/rules/rule"
6
+ Dir.glob(File.dirname(__FILE__) + "/credentials/rules/*.rb").each { |f| require f }
7
+ Dir.glob(File.dirname(__FILE__) + "/credentials/support/*.rb").each { |f| require f } unless defined?(ActiveSupport)
8
+
9
+ Object.send :extend, Credentials::ClassMethods
@@ -23,5 +23,3 @@ module Credentials
23
23
  end
24
24
  end
25
25
  end
26
-
27
- ActiveRecord::Base.send :extend, Credentials::ClassMethods
@@ -1,36 +1,127 @@
1
+ require "singleton"
2
+
1
3
  module Credentials
2
4
  module Inflector
5
+ extend self
6
+
7
+ class Inflections
8
+ include Singleton
9
+
10
+ attr_reader :actors
11
+
12
+ def actors
13
+ @actors ||= []
14
+ end
15
+
16
+ def actor(rule, replacement)
17
+ actors.insert 0, [ rule, replacement ]
18
+ end
19
+
20
+ unless defined?(ActiveSupport)
21
+ attr_reader :plurals, :singulars, :uncountables
22
+
23
+ def initialize
24
+ @plurals, @singulars, @uncountables = [], [], []
25
+ end
26
+
27
+ def plurals
28
+ @plurals ||= []
29
+ end
30
+
31
+ def plural(rule, replacement)
32
+ plurals.insert 0, [ rule, replacement ]
33
+ end
34
+ def plural(rule, replacement)
35
+ @uncountables.delete(rule) if rule.is_a?(String)
36
+ @uncountables.delete(replacement)
37
+ @plurals.insert(0, [rule, replacement])
38
+ end
39
+
40
+ def singular(rule, replacement)
41
+ @uncountables.delete(rule) if rule.is_a?(String)
42
+ @uncountables.delete(replacement)
43
+ @singulars.insert(0, [rule, replacement])
44
+ end
45
+
46
+ def irregular(singular, plural)
47
+ @uncountables.delete(singular)
48
+ @uncountables.delete(plural)
49
+ if singular[0,1].upcase == plural[0,1].upcase
50
+ plural(Regexp.new("(#{singular[0,1]})#{singular[1..-1]}$", "i"), '\1' + plural[1..-1])
51
+ singular(Regexp.new("(#{plural[0,1]})#{plural[1..-1]}$", "i"), '\1' + singular[1..-1])
52
+ else
53
+ plural(Regexp.new("#{singular[0,1].upcase}(?i)#{singular[1..-1]}$"), plural[0,1].upcase + plural[1..-1])
54
+ plural(Regexp.new("#{singular[0,1].downcase}(?i)#{singular[1..-1]}$"), plural[0,1].downcase + plural[1..-1])
55
+ singular(Regexp.new("#{plural[0,1].upcase}(?i)#{plural[1..-1]}$"), singular[0,1].upcase + singular[1..-1])
56
+ singular(Regexp.new("#{plural[0,1].downcase}(?i)#{plural[1..-1]}$"), singular[0,1].downcase + singular[1..-1])
57
+ end
58
+ end
59
+
60
+ def uncountable(*words)
61
+ (@uncountables << words).flatten!
62
+ end
63
+ end
64
+ end
65
+
66
+ def inflections
67
+ if block_given?
68
+ yield Inflections.instance
69
+ else
70
+ Inflections.instance
71
+ end
72
+ end
73
+
3
74
  def actorize(word)
4
75
  result = word.to_s.dup
5
76
  inflections.actors.each { |(rule, replacement)| break if result.gsub!(rule, replacement) } unless result.empty?
6
77
  result
7
78
  end
8
- end
79
+
80
+ unless defined?(ActiveSupport)
81
+ def pluralize(word)
82
+ result = word.to_s.dup
9
83
 
10
- module Inflections
11
- attr_reader :actors
84
+ if word.empty? || inflections.uncountables.include?(result.downcase)
85
+ result
86
+ else
87
+ inflections.plurals.each { |(rule, replacement)| break if result.gsub!(rule, replacement) }
88
+ result
89
+ end
90
+ end
12
91
 
13
- def actors
14
- @actors ||= []
15
- end
16
-
17
- def actor(rule, replacement)
18
- actors.insert 0, [ rule, replacement ]
92
+ def singularize(word)
93
+ result = word.to_s.dup
94
+
95
+ if inflections.uncountables.include?(result.downcase)
96
+ result
97
+ else
98
+ inflections.singulars.each { |(rule, replacement)| break if result.gsub!(rule, replacement) }
99
+ result
100
+ end
101
+ end
19
102
  end
20
103
  end
21
104
 
22
105
  module StringExtensions
23
106
  def actorize
24
- ActiveSupport::Inflector.actorize(self)
107
+ Credentials::Inflector.actorize(self)
108
+ end
109
+
110
+ unless defined?(ActiveSupport)
111
+ def pluralize
112
+ Credentials::Inflector.pluralize(self)
113
+ end
114
+
115
+ def singularize
116
+ Credentials::Inflector.singularize(self)
117
+ end
25
118
  end
26
119
  end
27
120
  end
28
121
 
29
- ActiveSupport::Inflector.send :extend, Credentials::Inflector
30
- ActiveSupport::Inflector::Inflections.send :include, Credentials::Inflections
31
122
  String.send :include, Credentials::StringExtensions
32
123
 
33
- ActiveSupport::Inflector.inflections do |inflect|
124
+ Credentials::Inflector.inflections do |inflect|
34
125
  inflect.actor(/$/, 'er')
35
126
  inflect.actor(/e$/, 'er')
36
127
  inflect.actor(/ate$/, 'ator')
@@ -8,11 +8,11 @@ module Credentials
8
8
  end
9
9
 
10
10
  def can(verb, *args)
11
- @rules << Rules::Can.new(@klass, verb, *args)
11
+ @rules << Credentials::Rules::Can.new(@klass, verb, *args)
12
12
  end
13
13
 
14
14
  def cannot(verb, *args)
15
- @rules << Rules::Cannot.new(@klass, verb, *args)
15
+ @rules << Credentials::Rules::Cannot.new(@klass, verb, *args)
16
16
  end
17
17
 
18
18
  def can?(actor, verb, *args)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fauxparse-credentials
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matt Powell