alfa 0.0.4.pre → 0.0.5.pre
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.
- checksums.yaml +7 -0
- data/assets/js/jquery/jquery-1.11.0.js +10337 -0
- data/assets/js/jquery/jquery-1.11.0.min.js +4 -0
- data/assets/js/jquery/jquery-latest.js +4 -2
- data/dummy/project/Gemfile.source +6 -0
- data/dummy/project/Rakefile +5 -0
- data/dummy/project/apps/admin/controllers/default.rb +4 -0
- data/dummy/project/apps/admin/layouts/base.tpl +14 -0
- data/dummy/project/apps/admin/layouts/index.tpl.source +5 -0
- data/dummy/project/apps/admin/routes.rb +3 -0
- data/dummy/project/apps/admin/templates/default/index.tpl +1 -0
- data/dummy/project/apps/frontend/controllers/default.rb +5 -0
- data/dummy/project/apps/frontend/layouts/base.tpl +14 -0
- data/dummy/project/apps/frontend/layouts/index.tpl.source +5 -0
- data/dummy/project/apps/frontend/routes.rb +11 -0
- data/dummy/project/apps/frontend/templates/default/index.tpl +1 -0
- data/dummy/project/config/cli_application.rb +14 -0
- data/dummy/project/config/config.rb +11 -0
- data/dummy/project/config/db.rb +11 -0
- data/dummy/project/config/env.rb +6 -0
- data/dummy/project/config/groups.rb +17 -0
- data/dummy/project/config/passwords/db.sample.yml +8 -0
- data/dummy/project/config/routes.rb +4 -0
- data/dummy/project/config/setup_load_paths.rb +14 -0
- data/dummy/project/config/web_application.rb +15 -0
- data/dummy/project/config.ru +3 -0
- data/dummy/project/db/main/schema.yml +3 -0
- data/dummy/project/public/favicon.ico +0 -0
- data/dummy/project/public/robots.txt +1 -0
- data/lib/alfa/application.rb +6 -5
- data/lib/alfa/commands/new.rb +49 -4
- data/lib/alfa/controller.rb +118 -1
- data/lib/alfa/database/mysql.rb +1 -1
- data/lib/alfa/exceptions.rb +13 -0
- data/lib/alfa/logger.rb +3 -0
- data/lib/alfa/router.rb +25 -0
- data/lib/alfa/ruty.rb +10 -0
- data/lib/alfa/support.rb +39 -4
- data/lib/alfa/template-inheritance.rb +3 -0
- data/lib/alfa/user.rb +63 -0
- data/lib/alfa/web_application.rb +113 -39
- data/lib/haml/alfa_patch.rb +25 -0
- data/lib/rack/file_alfa.rb +11 -0
- data/lib/ruty/tags/href.rb +12 -0
- data/lib/ruty/upgrade.rb +1 -1
- data/lib/template-inheritance/alfa_bugfix.rb +15 -0
- data/lib/template-inheritance/alfa_helpers.rb +93 -0
- data/lib/tilt/alfa_patch.rb +25 -0
- data/test/data/test_web_application/apps/admin/controllers/default.rb +20 -0
- data/test/data/test_web_application/apps/admin/routes.rb +3 -0
- data/test/data/test_web_application/apps/frontend/controllers/default.rb +35 -0
- data/test/data/test_web_application/apps/frontend/routes.rb +2 -2
- data/test/data/test_web_application/config/routes.rb +1 -0
- data/test/test_controller.rb +30 -2
- data/test/test_router.rb +24 -0
- data/test/test_support.rb +72 -19
- data/test/test_web_application.rb +72 -5
- data/version.rb +1 -0
- metadata +143 -74
- data/test/data/test_web_application/apps/frontend/controllers/kfk.rb +0 -9
@@ -0,0 +1,11 @@
|
|
1
|
+
Project::WebApplication.routes.draw do
|
2
|
+
|
3
|
+
route '/', :controller => :default, :action => :index, :layout => :index
|
4
|
+
#route '/:action', :controller => :default, :layout => :internal
|
5
|
+
#route '/:controller/:action', :layout => :internal
|
6
|
+
#route '/:controller/:action/:id', :layout => :internal
|
7
|
+
|
8
|
+
route 404, controller: :system, action: :page_404
|
9
|
+
route 500, controller: :system, action: :page_500
|
10
|
+
|
11
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
apps/frontend/default#index
|
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'alfa/cli_application'
|
2
|
+
require File.expand_path('../env', __FILE__)
|
3
|
+
require File.expand_path('../db', __FILE__)
|
4
|
+
require File.expand_path('../groups', __FILE__)
|
5
|
+
|
6
|
+
module Project
|
7
|
+
class CliApplication < Alfa::CliApplication
|
8
|
+
instance_eval(File.read(File.expand_path('../config.rb', __FILE__)), File.expand_path('../config.rb', __FILE__))
|
9
|
+
config[:run_mode] = :cli # :cli for command-line application
|
10
|
+
config[:log][:file] = File.join(PROJECT_ROOT, 'log/cli.log')
|
11
|
+
end
|
12
|
+
|
13
|
+
CliApplication.init!
|
14
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
# Eval in actual application's context (CliApplication, WebApplication or other)
|
2
|
+
|
3
|
+
config[:project_root] = PROJECT_ROOT
|
4
|
+
config[:document_root] = DOCUMENT_ROOT
|
5
|
+
config[:db][:main] = {
|
6
|
+
:instance => DB::Main,
|
7
|
+
:path => File.join(PROJECT_ROOT, 'db/main'), # require both instance and path
|
8
|
+
:maintain => true,
|
9
|
+
}
|
10
|
+
config[:groups] = Project::GROUPS
|
11
|
+
config[:templates_priority] = [:haml, :tpl]
|
@@ -0,0 +1,11 @@
|
|
1
|
+
require 'alfa/database'
|
2
|
+
require 'yaml'
|
3
|
+
|
4
|
+
# Main database for application private use
|
5
|
+
module DB
|
6
|
+
Main = Sequel.connect(YAML.load(File.open(File.expand_path('../passwords/db-main.yml', __FILE__))).symbolize_keys[:dsn], :encoding=>'utf8')
|
7
|
+
end
|
8
|
+
|
9
|
+
Dir[File.join(PROJECT_ROOT, 'db/main/models/*.rb')].each do |f|
|
10
|
+
require f
|
11
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module Project
|
2
|
+
# Here is hash of groups with list of grants in each.
|
3
|
+
# Guest group is required and may be empty, all other groups are optional.
|
4
|
+
GROUPS = {
|
5
|
+
# Add custom groups here. Example:
|
6
|
+
# :some_group => [:send_mails, :create_users, :moderate_forum],
|
7
|
+
# Note that :some_group=>[:some_grant] and :other_group=>[:some_grant] points to same :some_grant
|
8
|
+
|
9
|
+
# :admin_index - predefined (system required) grant for browse admin app
|
10
|
+
:admin => [:admin_index],
|
11
|
+
|
12
|
+
# Public group is associated with all users include non-logged in.
|
13
|
+
# This group is empty by default, but you can put some grants in it.
|
14
|
+
# For example :public => [:foo] means that all users respond to .grant?(:foo) as true.
|
15
|
+
:public => [],
|
16
|
+
}
|
17
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
# This file required by Phusion Passenger
|
2
|
+
|
3
|
+
if ENV['MY_RUBY_HOME'] && ENV['MY_RUBY_HOME'].include?('rvm')
|
4
|
+
begin
|
5
|
+
require 'rvm'
|
6
|
+
RVM.use_from_path! File.dirname(File.dirname(__FILE__))
|
7
|
+
rescue LoadError
|
8
|
+
raise "RVM gem is currently unavailable."
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
# If you're not using Bundler at all, remove lines bellow
|
13
|
+
ENV['BUNDLE_GEMFILE'] = File.expand_path('../Gemfile', File.dirname(__FILE__))
|
14
|
+
require 'bundler/setup'
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'alfa/web_application'
|
2
|
+
require File.expand_path('../env', __FILE__)
|
3
|
+
require File.expand_path('../db', __FILE__)
|
4
|
+
require File.expand_path('../groups', __FILE__)
|
5
|
+
|
6
|
+
module Project
|
7
|
+
class WebApplication < Alfa::WebApplication
|
8
|
+
instance_eval(File.read(File.expand_path('../config.rb', __FILE__)), File.expand_path('../config.rb', __FILE__))
|
9
|
+
config[:run_mode] = :development # :development or :production or :test
|
10
|
+
config[:log][:file] = File.join(PROJECT_ROOT, 'log/web.log')
|
11
|
+
config[:serve_static] = true
|
12
|
+
end
|
13
|
+
|
14
|
+
WebApplication.init!
|
15
|
+
end
|
Binary file
|
@@ -0,0 +1 @@
|
|
1
|
+
User-Agent: *
|
data/lib/alfa/application.rb
CHANGED
@@ -8,7 +8,7 @@ Encoding.default_internal = 'utf-8'
|
|
8
8
|
module Alfa
|
9
9
|
|
10
10
|
# Dirty hack. Using constant-hash to store values. Ruby forbid dynamically change constants, but it allow change items of constant-structs
|
11
|
-
# 1st using - in
|
11
|
+
# 1st using - in Rake tasks (see self.load_tasks)
|
12
12
|
VARS = {}
|
13
13
|
|
14
14
|
class Application
|
@@ -19,8 +19,9 @@ module Alfa
|
|
19
19
|
|
20
20
|
@config = Alfa::Config.new
|
21
21
|
|
22
|
-
def self.config(kwargs=
|
23
|
-
@config.merge! kwargs
|
22
|
+
def self.config(kwargs = nil)
|
23
|
+
@config.merge!(kwargs) if kwargs
|
24
|
+
@config
|
24
25
|
end
|
25
26
|
|
26
27
|
|
@@ -29,7 +30,7 @@ module Alfa
|
|
29
30
|
if @config[:log][:file]
|
30
31
|
@log_file = File.open(@config[:log][:file], File::WRONLY | File::APPEND | File::CREAT)
|
31
32
|
@logger = Alfa::Logger.new(@log_file)
|
32
|
-
str = "Application (pid=#{$$}) started at #{DateTime.now}"
|
33
|
+
str = "Application (pid=#{$$}) started in #{@config[:run_mode]} mode at #{DateTime.now}"
|
33
34
|
@logger.info "#{'='*str.length}\n#{str}"
|
34
35
|
@logger.info " PROJECT_ROOT: #{@config[:project_root]}"
|
35
36
|
@logger.info " DOCUMENT_ROOT: #{@config[:document_root]}\n"
|
@@ -50,7 +51,7 @@ module Alfa
|
|
50
51
|
|
51
52
|
|
52
53
|
def self.verify_config
|
53
|
-
raise Exceptions::E001 unless @config[:project_root]
|
54
|
+
raise Exceptions::E001.new('config[:project_root] should be defined') unless @config[:project_root]
|
54
55
|
end
|
55
56
|
end
|
56
57
|
end
|
data/lib/alfa/commands/new.rb
CHANGED
@@ -1,4 +1,49 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
1
|
+
require File.expand_path('../../../../version', __FILE__)
|
2
|
+
require 'fileutils'
|
3
|
+
require 'alfa/support'
|
4
|
+
|
5
|
+
PROJECT_NAME = $*[1]
|
6
|
+
|
7
|
+
sr = {
|
8
|
+
'#{ALFA_VERSION}' => ALFA_VERSION,
|
9
|
+
'#{PROJECT_NAME}' => PROJECT_NAME,
|
10
|
+
}
|
11
|
+
|
12
|
+
target_dir = File.join(Dir.pwd, PROJECT_NAME)
|
13
|
+
raise "file or directory #{PROJECT_NAME} already exists" if File.exists?(target_dir)
|
14
|
+
Dir.mkdir PROJECT_NAME
|
15
|
+
|
16
|
+
puts "Created new project in #{PROJECT_NAME}"
|
17
|
+
|
18
|
+
begin
|
19
|
+
print "Copy dummy project... "
|
20
|
+
source_base_len = File.expand_path('../../../../dummy/project', __FILE__).length + 1
|
21
|
+
Dir[File.expand_path('../../../../dummy/project/**/{*,.[^.]*}', __FILE__)].each do |path|
|
22
|
+
relpath = path[source_base_len..-1]
|
23
|
+
target = File.join(target_dir, relpath)
|
24
|
+
if File.directory?(path)
|
25
|
+
FileUtils.mkdir_p target, :mode => 0755
|
26
|
+
else
|
27
|
+
if File.extname(relpath) == '.source'
|
28
|
+
File.open(target.chomp('.source'), File::WRONLY | File::CREAT) do |f|
|
29
|
+
f.write(File.read(path).strtr(sr))
|
30
|
+
end
|
31
|
+
else
|
32
|
+
FileUtils.cp(path, target)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
print "done"
|
37
|
+
ensure
|
38
|
+
puts ""
|
39
|
+
end
|
40
|
+
|
41
|
+
begin
|
42
|
+
print "Bundle install... "
|
43
|
+
FileUtils.cd PROJECT_NAME
|
44
|
+
`bundle install`
|
45
|
+
print "done"
|
46
|
+
ensure
|
47
|
+
FileUtils.cd '..'
|
48
|
+
puts ""
|
49
|
+
end
|
data/lib/alfa/controller.rb
CHANGED
@@ -1,7 +1,124 @@
|
|
1
|
+
require 'alfa/support'
|
2
|
+
require 'alfa/exceptions'
|
3
|
+
|
1
4
|
module Alfa
|
2
5
|
class Controller
|
6
|
+
attr_accessor :application, :request, :config, :app_sym, :c_sym
|
7
|
+
|
3
8
|
def _instance_variables_hash
|
4
|
-
Hash[instance_variables.map { |name| [name.
|
9
|
+
Hash[instance_variables.map { |name| [name.to_sym, instance_variable_get(name)] } ]
|
10
|
+
end
|
11
|
+
|
12
|
+
def _clear_instance_variables
|
13
|
+
instance_variables.each {|name| remove_instance_variable(name)}
|
14
|
+
end
|
15
|
+
|
16
|
+
def href(*o)
|
17
|
+
kwargs = _extract_href_params(*o)
|
18
|
+
@application.routes.href(kwargs)
|
19
|
+
end
|
20
|
+
|
21
|
+
alias :href_to :href
|
22
|
+
|
23
|
+
def _extract_href_params(*o)
|
24
|
+
args, kwargs = Support.args_kwargs(*o)
|
25
|
+
if args.any?
|
26
|
+
if args.first.is_a?(Symbol)
|
27
|
+
kwargs[:action] = args.first
|
28
|
+
else
|
29
|
+
kwargs.merge! _string_to_aca(args.first.to_s)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
kwargs = {:app=>@app_sym}.merge kwargs
|
33
|
+
kwargs = {:controller=>@c_sym}.merge kwargs if kwargs[:action]
|
34
|
+
kwargs
|
35
|
+
end
|
36
|
+
|
37
|
+
# Convert string to App-Controller-Action hash
|
38
|
+
# 'app*controller#action'
|
39
|
+
def _string_to_aca(str)
|
40
|
+
res = {}
|
41
|
+
s1 = str.split('@')
|
42
|
+
raise Exceptions::E004.new("E004: Bad href argument #{str}: it should contain at most one @ symbol") if s1.length > 2
|
43
|
+
res[:app] = s1.last.to_sym if s1.length > 1
|
44
|
+
s2 = s1.first.split('#')
|
45
|
+
raise Exceptions::E004.new("E004: Bad href argument #{str}: it should contain at most one # symbol") if s2.length > 2
|
46
|
+
res[:controller] = s2.first.to_sym if s2.length > 1
|
47
|
+
res[:action] = s2.last.to_sym if s2.length > 0
|
48
|
+
res
|
49
|
+
end
|
50
|
+
|
51
|
+
|
52
|
+
def session
|
53
|
+
@request.session
|
54
|
+
end
|
55
|
+
|
56
|
+
# Return current user
|
57
|
+
def user
|
58
|
+
@user ||= (
|
59
|
+
if @request.session[:user_id] && (u = @application.config[:db][:main][:instance][:users].first(id: @request.session[:user_id]))
|
60
|
+
User.new(u)
|
61
|
+
else
|
62
|
+
GuestUser
|
63
|
+
end
|
64
|
+
)
|
65
|
+
end
|
66
|
+
|
67
|
+
|
68
|
+
def grant?(grant)
|
69
|
+
user.grant?(grant)
|
70
|
+
end
|
71
|
+
|
72
|
+
|
73
|
+
[300, 301, 302, 303].each do |code|
|
74
|
+
define_method ("redirect_#{code}".to_sym) do |url|
|
75
|
+
@application.redirect(url, code)
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
alias :redirect :redirect_302
|
80
|
+
|
81
|
+
|
82
|
+
def try_login(username, password)
|
83
|
+
u = @application.config[:db][:main][:instance][:users].first(login: username)
|
84
|
+
raise "No such login: #{username}" unless u
|
85
|
+
if u[:passhash] == Digest::MD5.hexdigest("#{u[:salt]}#{password}")
|
86
|
+
# success
|
87
|
+
session[:user_id] = u[:id]
|
88
|
+
return true
|
89
|
+
else
|
90
|
+
# fail
|
91
|
+
session[:user_id] = nil
|
92
|
+
raise 'login fail'
|
93
|
+
return false
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
|
98
|
+
def try_register(username, password)
|
99
|
+
@config[:db][:main][:instance].transaction do
|
100
|
+
unless @config[:db][:main][:instance][:users].first(:login=>username)
|
101
|
+
@logger.portion do |l|
|
102
|
+
salt = SecureRandom.hex(5)
|
103
|
+
passhash = Digest::MD5.hexdigest("#{salt}#{password}")
|
104
|
+
@config[:db][:main][:instance][:users].insert(:login=>username, :salt=>salt, :passhash=>passhash)
|
105
|
+
l.info("create new user login=#{username}, password=#{password}, salt=#{salt}, passhash=#{passhash}")
|
106
|
+
end
|
107
|
+
return true, "Registration done"
|
108
|
+
end
|
109
|
+
return false, "User with login #{username} already exists"
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
|
114
|
+
def try_logout
|
115
|
+
session[:user_id] = nil
|
116
|
+
@user = GuestUser
|
117
|
+
end
|
118
|
+
|
119
|
+
# Store flash message to session
|
120
|
+
def flash(message)
|
121
|
+
|
5
122
|
end
|
6
123
|
end
|
7
124
|
end
|
data/lib/alfa/database/mysql.rb
CHANGED
@@ -27,7 +27,7 @@ module Alfa
|
|
27
27
|
raise "Unacceptible engine in #{path}/schema.yml, expected to be mysql of mysql2" unless ['mysql', 'mysql2'].include? data[:engine]
|
28
28
|
Dir[File.join(path, 'models/*.rb')].each do |file|
|
29
29
|
require file
|
30
|
-
Kernel::const_get(Alfa::Support.
|
30
|
+
Kernel::const_get(Alfa::Support.camelcase_name(File.basename(file, '.rb'))).register_database self
|
31
31
|
end
|
32
32
|
|
33
33
|
end
|
data/lib/alfa/exceptions.rb
CHANGED
@@ -3,10 +3,23 @@ module Alfa
|
|
3
3
|
# Route not found
|
4
4
|
class Route404 < StandardError; end
|
5
5
|
|
6
|
+
class HttpRedirect < StandardError
|
7
|
+
attr_reader :url, :code
|
8
|
+
def initialize(url, code)
|
9
|
+
@url, @code = url, code
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
6
13
|
# Application's config.project_root required
|
7
14
|
class E001 < StandardError; end
|
8
15
|
|
9
16
|
# WebApplication's config.document_root required
|
10
17
|
class E002 < StandardError; end
|
18
|
+
|
19
|
+
# Href can't be build
|
20
|
+
class E003 < StandardError; end
|
21
|
+
|
22
|
+
# Bad str for href format
|
23
|
+
class E004 < StandardError; end
|
11
24
|
end
|
12
25
|
end
|
data/lib/alfa/logger.rb
CHANGED
data/lib/alfa/router.rb
CHANGED
@@ -1,3 +1,6 @@
|
|
1
|
+
require 'alfa/exceptions'
|
2
|
+
require 'rack/utils'
|
3
|
+
|
1
4
|
module Alfa
|
2
5
|
class Router
|
3
6
|
|
@@ -165,5 +168,27 @@ module Alfa
|
|
165
168
|
raise Alfa::Exceptions::Route404
|
166
169
|
end
|
167
170
|
|
171
|
+
|
172
|
+
def self.href(kwargs={})
|
173
|
+
params = kwargs[:params] || {}
|
174
|
+
kwargs.delete!(:params)
|
175
|
+
@routes.each do |route|
|
176
|
+
if route[:context].is_a?(Hash) # container
|
177
|
+
if route[:context][:app][:app] == kwargs[:app]
|
178
|
+
route[:routes].each do |r|
|
179
|
+
r[:placeholders] = r[:rule].scan(/:([a-z_][a-z0-9_]*)/).map{|m| m[0].to_sym}.sort unless r[:placeholders]
|
180
|
+
if (r[:placeholders] - kwargs.keys).empty? &&
|
181
|
+
(kwargs.keys & r[:options].keys).all?{|key| kwargs[key] == r[:options][key]} &&
|
182
|
+
(kwargs.keys - [:app] - r[:placeholders] - r[:options].keys).empty?
|
183
|
+
result = File.join(route[:context][:app][:path], r[:rule].strtr(kwargs.map{|key, value| [":#{key}", value.to_s]}))
|
184
|
+
result += "?#{::Rack::Utils.build_query(params)}" if params.any?
|
185
|
+
return result
|
186
|
+
end
|
187
|
+
end
|
188
|
+
end
|
189
|
+
end
|
190
|
+
end
|
191
|
+
raise Exceptions::E003.new("Can't build url by params #{kwargs}")
|
192
|
+
end
|
168
193
|
end
|
169
194
|
end
|
data/lib/alfa/ruty.rb
ADDED
data/lib/alfa/support.rb
CHANGED
@@ -31,13 +31,17 @@ module Alfa
|
|
31
31
|
module Support
|
32
32
|
extend self
|
33
33
|
|
34
|
-
def
|
34
|
+
def camelcase_name(arg)
|
35
35
|
arg.to_s.split('/').last.split('_').map(&:capitalize).join
|
36
36
|
end
|
37
37
|
|
38
|
-
def
|
39
|
-
|
40
|
-
|
38
|
+
def underscore_name(arg)
|
39
|
+
arg.to_s.split('/').last.scan(/[A-Z][a-z]*|[a-z]+/).map(&:downcase).join('_')
|
40
|
+
end
|
41
|
+
|
42
|
+
def args_kwargs(*args)
|
43
|
+
return args[0..-2], args.last if args.last.is_a?(Hash)
|
44
|
+
return args, {}
|
41
45
|
end
|
42
46
|
end
|
43
47
|
|
@@ -97,4 +101,35 @@ class Hash
|
|
97
101
|
end
|
98
102
|
self
|
99
103
|
end
|
104
|
+
|
105
|
+
|
106
|
+
def delete!(*keys)
|
107
|
+
keys.each{|key| self.delete(key)}
|
108
|
+
self
|
109
|
+
end
|
110
|
+
|
111
|
+
|
112
|
+
def except(*keys)
|
113
|
+
self.dup.delete!(*keys)
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
|
118
|
+
class String
|
119
|
+
# PHP's two argument version of strtr
|
120
|
+
def strtr(replace_pairs)
|
121
|
+
keys = replace_pairs.map {|a, b| a }
|
122
|
+
values = replace_pairs.map {|a, b| b }
|
123
|
+
self.gsub(
|
124
|
+
/(#{keys.map{|a| Regexp.quote(a) }.join( ')|(' )})/
|
125
|
+
) { |match| values[keys.index(match)] }
|
126
|
+
end
|
127
|
+
|
128
|
+
def strtr!(replace_pairs)
|
129
|
+
keys = replace_pairs.map {|a, b| a }
|
130
|
+
values = replace_pairs.map {|a, b| b }
|
131
|
+
self.gsub!(
|
132
|
+
/(#{keys.map{|a| Regexp.quote(a) }.join( ')|(' )})/
|
133
|
+
) { |match| values[keys.index(match)] }
|
134
|
+
end
|
100
135
|
end
|
data/lib/alfa/user.rb
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
module Alfa
|
2
|
+
class << self
|
3
|
+
attr_accessor :GROUPS
|
4
|
+
end
|
5
|
+
|
6
|
+
class GuestUser
|
7
|
+
def self.grants
|
8
|
+
@grants ||= Alfa.GROUPS[:public]
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.grant?(name)
|
12
|
+
grants.include?(name.to_sym)
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.groups
|
16
|
+
[]
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.group?(name)
|
20
|
+
groups.include?(name.to_sym)
|
21
|
+
end
|
22
|
+
|
23
|
+
def self.logged?
|
24
|
+
false
|
25
|
+
end
|
26
|
+
|
27
|
+
def [](key)
|
28
|
+
nil
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
class User
|
33
|
+
def initialize(properties)
|
34
|
+
@properties = properties
|
35
|
+
end
|
36
|
+
|
37
|
+
def grants
|
38
|
+
@groups_p ||= groups + [:public]
|
39
|
+
@groups_p.map{|g| Alfa.GROUPS[g] || []}.flatten
|
40
|
+
end
|
41
|
+
|
42
|
+
def grant?(name)
|
43
|
+
grants.include?(name.to_sym)
|
44
|
+
end
|
45
|
+
|
46
|
+
# @return Array
|
47
|
+
def groups
|
48
|
+
@groups ||= @properties[:groups].to_s.split(',').map{|s| s.strip.to_sym}
|
49
|
+
end
|
50
|
+
|
51
|
+
def group?(name)
|
52
|
+
groups.include?(name.to_sym)
|
53
|
+
end
|
54
|
+
|
55
|
+
def logged?
|
56
|
+
true
|
57
|
+
end
|
58
|
+
|
59
|
+
def [](key)
|
60
|
+
@properties[key]
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|