scales-core 0.0.1.beta.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +29 -0
- data/Rakefile +8 -0
- data/bin/scale +14 -0
- data/bin/scalify +6 -0
- data/lib/.DS_Store +0 -0
- data/lib/scales-core.rb +13 -0
- data/lib/scales-core/.DS_Store +0 -0
- data/lib/scales-core/base.rb +31 -0
- data/lib/scales-core/boot/autoload.rb +19 -0
- data/lib/scales-core/boot/initializers/em.rb +3 -0
- data/lib/scales-core/boot/initializers/goliath.rb +2 -0
- data/lib/scales-core/boot/initializers/json.rb +1 -0
- data/lib/scales-core/boot/initializers/redis.rb +1 -0
- data/lib/scales-core/boot/initializers/securerandom.rb +1 -0
- data/lib/scales-core/boot/initializers/thor.rb +1 -0
- data/lib/scales-core/config.rb +46 -0
- data/lib/scales-core/helper.rb +6 -0
- data/lib/scales-core/helper/content_types.rb +29 -0
- data/lib/scales-core/helper/partial_resolver.rb +36 -0
- data/lib/scales-core/pub_sub.rb +6 -0
- data/lib/scales-core/pub_sub/async.rb +19 -0
- data/lib/scales-core/pub_sub/sync.rb +17 -0
- data/lib/scales-core/queue.rb +8 -0
- data/lib/scales-core/queue/async.rb +18 -0
- data/lib/scales-core/queue/sync.rb +18 -0
- data/lib/scales-core/scalify.rb +17 -0
- data/lib/scales-core/scalify/templates/cache.yml +20 -0
- data/lib/scales-core/scalify/templates/scaleup.rb +6 -0
- data/lib/scales-core/storage.rb +17 -0
- data/lib/scales-core/storage/async.rb +89 -0
- data/lib/scales-core/storage/sync.rb +93 -0
- data/lib/scales-core/version.rb +5 -0
- data/scales-core.gemspec +29 -0
- data/spec/.DS_Store +0 -0
- data/spec/base_spec.rb +19 -0
- data/spec/config_spec.rb +43 -0
- data/spec/gem_spec.rb +7 -0
- data/spec/helper.rb +57 -0
- data/spec/partial_resolver_spec.rb +45 -0
- data/spec/pub_sub_spec.rb +51 -0
- data/spec/queue_spec.rb +37 -0
- data/spec/scalify_spec.rb +13 -0
- data/spec/storage_spec.rb +147 -0
- metadata +213 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Thomas Fankhauser
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# Scales::Core
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'scales-core'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install scales-core
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
TODO: Write usage instructions here
|
22
|
+
|
23
|
+
## Contributing
|
24
|
+
|
25
|
+
1. Fork it
|
26
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
28
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/bin/scale
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'rake'
|
4
|
+
|
5
|
+
Rake.application.instance_eval do
|
6
|
+
@name = "scale"
|
7
|
+
@rakefiles = ['config/scaleup.rb']
|
8
|
+
@rakefile = 'config/scaleup.rb'
|
9
|
+
standard_exception_handling do
|
10
|
+
init(@name)
|
11
|
+
load_rakefile
|
12
|
+
top_level
|
13
|
+
end
|
14
|
+
end
|
data/bin/scalify
ADDED
data/lib/.DS_Store
ADDED
Binary file
|
data/lib/scales-core.rb
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'scales-core/boot/autoload'
|
2
|
+
|
3
|
+
module Scales
|
4
|
+
autoload :Helper, "scales-core/helper"
|
5
|
+
autoload :Config, "scales-core/config"
|
6
|
+
autoload :Storage, "scales-core/storage"
|
7
|
+
autoload :Queue, "scales-core/queue"
|
8
|
+
autoload :PubSub, "scales-core/pub_sub"
|
9
|
+
autoload :Scalify, "scales-core/scalify"
|
10
|
+
end
|
11
|
+
|
12
|
+
require "scales-core/version"
|
13
|
+
require "scales-core/base"
|
Binary file
|
@@ -0,0 +1,31 @@
|
|
1
|
+
module Scales
|
2
|
+
class << self
|
3
|
+
|
4
|
+
def config
|
5
|
+
Config.config
|
6
|
+
end
|
7
|
+
|
8
|
+
def env
|
9
|
+
ENV['SCALES_ENV'] ||= "development"
|
10
|
+
end
|
11
|
+
|
12
|
+
def env=(env)
|
13
|
+
ENV['SCALES_ENV'] = env
|
14
|
+
end
|
15
|
+
|
16
|
+
def pwd
|
17
|
+
Config.pwd
|
18
|
+
end
|
19
|
+
|
20
|
+
def pwd=(pwd)
|
21
|
+
Config.pwd = pwd
|
22
|
+
end
|
23
|
+
|
24
|
+
def try_to_setup_env!
|
25
|
+
return if ARGV.empty?
|
26
|
+
return if ARGV.first =~ /^\-/
|
27
|
+
Scales.env = ARGV.pop
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'colorize'
|
2
|
+
require 'json'
|
3
|
+
require 'yaml'
|
4
|
+
|
5
|
+
autoload :Redis, "scales-core/boot/initializers/redis"
|
6
|
+
autoload :EventMachine, "scales-core/boot/initializers/em"
|
7
|
+
autoload :Thor, "scales-core/boot/initializers/thor"
|
8
|
+
|
9
|
+
module EventMachine
|
10
|
+
autoload :Synchrony, "scales-core/boot/initializers/em"
|
11
|
+
autoload :Hiredis, "scales-core/boot/initializers/em"
|
12
|
+
end
|
13
|
+
|
14
|
+
module EM
|
15
|
+
autoload :Synchrony, "scales-core/boot/initializers/em"
|
16
|
+
autoload :Hiredis, "scales-core/boot/initializers/em"
|
17
|
+
end
|
18
|
+
|
19
|
+
ENV['SCALES_ENV'] ||= "development"
|
@@ -0,0 +1 @@
|
|
1
|
+
require 'json'
|
@@ -0,0 +1 @@
|
|
1
|
+
require 'redis'
|
@@ -0,0 +1 @@
|
|
1
|
+
require 'securerandom'
|
@@ -0,0 +1 @@
|
|
1
|
+
require 'thor/group'
|
@@ -0,0 +1,46 @@
|
|
1
|
+
module Scales
|
2
|
+
module Config
|
3
|
+
require 'ostruct'
|
4
|
+
|
5
|
+
DEFAULTS = {
|
6
|
+
:host => "localhost",
|
7
|
+
:port => 6379,
|
8
|
+
:password => nil,
|
9
|
+
:database => 0,
|
10
|
+
:partials => false
|
11
|
+
}
|
12
|
+
|
13
|
+
@@pwd = "."
|
14
|
+
CONFIG_PATHS = ['config/cache.yml', 'cache.yml']
|
15
|
+
|
16
|
+
class << self
|
17
|
+
|
18
|
+
def config
|
19
|
+
@@config ||= OpenStruct.new DEFAULTS.merge(load!)
|
20
|
+
end
|
21
|
+
|
22
|
+
def reset!
|
23
|
+
@@config = nil
|
24
|
+
end
|
25
|
+
|
26
|
+
def load!
|
27
|
+
load_paths = CONFIG_PATHS.map{ |path| File.exists?(File.join(@@pwd, path)) }
|
28
|
+
return {} unless load_paths.any?
|
29
|
+
|
30
|
+
cache = File.join(@@pwd, CONFIG_PATHS[load_paths.index(true)])
|
31
|
+
config = YAML.load_file(cache)[Scales.env]
|
32
|
+
|
33
|
+
Hash[config.map{|(k,v)| [k.to_sym,v]}]
|
34
|
+
end
|
35
|
+
|
36
|
+
def pwd=(pwd)
|
37
|
+
@@pwd = pwd
|
38
|
+
end
|
39
|
+
|
40
|
+
def pwd
|
41
|
+
@@pwd
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module Scales
|
2
|
+
module Helper
|
3
|
+
module ContentTypes
|
4
|
+
|
5
|
+
TYPES = {
|
6
|
+
:html => 'text/html',
|
7
|
+
:htm => 'text/html',
|
8
|
+
:txt => 'text/plain',
|
9
|
+
:css => 'text/css',
|
10
|
+
:yaml => 'text/yaml',
|
11
|
+
:js => 'application/javascript',
|
12
|
+
:json => 'application/json',
|
13
|
+
:rss => 'application/rss+xml',
|
14
|
+
:xml => 'application/xml',
|
15
|
+
:pdf => 'application/pdf',
|
16
|
+
:zip => 'application/zip',
|
17
|
+
:jpg => 'image/jpeg',
|
18
|
+
:png => 'image/png'
|
19
|
+
}
|
20
|
+
|
21
|
+
class ::Symbol
|
22
|
+
def to_content_type
|
23
|
+
Scales::Helper::ContentTypes::TYPES[self]
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
module Scales
|
2
|
+
module Helper
|
3
|
+
module PartialResolver
|
4
|
+
|
5
|
+
PARTIAL_REGEX = /(Scales\.partial ["|'](.*)["|'])/
|
6
|
+
|
7
|
+
class << self
|
8
|
+
|
9
|
+
def resolve(redis, key)
|
10
|
+
value = redis.get(key)
|
11
|
+
|
12
|
+
i = 0
|
13
|
+
|
14
|
+
while includes_partial?(value) do
|
15
|
+
value = resolve_partial(redis, value)
|
16
|
+
end
|
17
|
+
value
|
18
|
+
end
|
19
|
+
|
20
|
+
def includes_partial?(value)
|
21
|
+
value =~ PARTIAL_REGEX
|
22
|
+
end
|
23
|
+
|
24
|
+
def resolve_partial(redis, value)
|
25
|
+
matched = value.match(PARTIAL_REGEX)
|
26
|
+
tag, key = matched[1], matched[2]
|
27
|
+
partial = redis.get(Storage::PARTIAL_PREFIX + key)
|
28
|
+
|
29
|
+
value.gsub(tag, partial)
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module Scales
|
2
|
+
module PubSub
|
3
|
+
module Async
|
4
|
+
class << self
|
5
|
+
|
6
|
+
def publish(channel, message)
|
7
|
+
Storage::Async.publish(channel, message)
|
8
|
+
end
|
9
|
+
|
10
|
+
def subscribe(channel)
|
11
|
+
out = Storage::Async.subscribe(channel)
|
12
|
+
Storage::Async.connection.del(channel)
|
13
|
+
out
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module Scales
|
2
|
+
class Scalify < Thor::Group
|
3
|
+
include Thor::Actions
|
4
|
+
|
5
|
+
def self.source_root
|
6
|
+
File.expand_path "../scalify/templates", __FILE__
|
7
|
+
end
|
8
|
+
|
9
|
+
def create_scaleup_file
|
10
|
+
template 'scaleup.rb', "config/scaleup.rb"
|
11
|
+
end
|
12
|
+
|
13
|
+
def create_cache_file
|
14
|
+
template 'cache.yml', "config/cache.yml"
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|