scales-core 0.0.1.beta.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (47) hide show
  1. data/.gitignore +17 -0
  2. data/Gemfile +4 -0
  3. data/LICENSE +22 -0
  4. data/README.md +29 -0
  5. data/Rakefile +8 -0
  6. data/bin/scale +14 -0
  7. data/bin/scalify +6 -0
  8. data/lib/.DS_Store +0 -0
  9. data/lib/scales-core.rb +13 -0
  10. data/lib/scales-core/.DS_Store +0 -0
  11. data/lib/scales-core/base.rb +31 -0
  12. data/lib/scales-core/boot/autoload.rb +19 -0
  13. data/lib/scales-core/boot/initializers/em.rb +3 -0
  14. data/lib/scales-core/boot/initializers/goliath.rb +2 -0
  15. data/lib/scales-core/boot/initializers/json.rb +1 -0
  16. data/lib/scales-core/boot/initializers/redis.rb +1 -0
  17. data/lib/scales-core/boot/initializers/securerandom.rb +1 -0
  18. data/lib/scales-core/boot/initializers/thor.rb +1 -0
  19. data/lib/scales-core/config.rb +46 -0
  20. data/lib/scales-core/helper.rb +6 -0
  21. data/lib/scales-core/helper/content_types.rb +29 -0
  22. data/lib/scales-core/helper/partial_resolver.rb +36 -0
  23. data/lib/scales-core/pub_sub.rb +6 -0
  24. data/lib/scales-core/pub_sub/async.rb +19 -0
  25. data/lib/scales-core/pub_sub/sync.rb +17 -0
  26. data/lib/scales-core/queue.rb +8 -0
  27. data/lib/scales-core/queue/async.rb +18 -0
  28. data/lib/scales-core/queue/sync.rb +18 -0
  29. data/lib/scales-core/scalify.rb +17 -0
  30. data/lib/scales-core/scalify/templates/cache.yml +20 -0
  31. data/lib/scales-core/scalify/templates/scaleup.rb +6 -0
  32. data/lib/scales-core/storage.rb +17 -0
  33. data/lib/scales-core/storage/async.rb +89 -0
  34. data/lib/scales-core/storage/sync.rb +93 -0
  35. data/lib/scales-core/version.rb +5 -0
  36. data/scales-core.gemspec +29 -0
  37. data/spec/.DS_Store +0 -0
  38. data/spec/base_spec.rb +19 -0
  39. data/spec/config_spec.rb +43 -0
  40. data/spec/gem_spec.rb +7 -0
  41. data/spec/helper.rb +57 -0
  42. data/spec/partial_resolver_spec.rb +45 -0
  43. data/spec/pub_sub_spec.rb +51 -0
  44. data/spec/queue_spec.rb +37 -0
  45. data/spec/scalify_spec.rb +13 -0
  46. data/spec/storage_spec.rb +147 -0
  47. metadata +213 -0
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in scales-core.gemspec
4
+ gemspec
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
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+ require 'rspec/core/rake_task'
4
+
5
+ RSpec::Core::RakeTask.new(:spec) do |rspec|
6
+ rspec.rspec_opts = "-f d", "--color"
7
+ end
8
+ task :default => :spec
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
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'scales-core'
4
+
5
+ Dir.chdir ARGV.first unless ARGV.first.nil?
6
+ Scales::Scalify.start
data/lib/.DS_Store ADDED
Binary file
@@ -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,3 @@
1
+ require 'eventmachine'
2
+ require 'em-synchrony'
3
+ require 'em-synchrony/em-hiredis'
@@ -0,0 +1,2 @@
1
+ require 'goliath'
2
+ require 'goliath/test_helper'
@@ -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,6 @@
1
+ module Scales
2
+ module Helper
3
+ autoload :ContentTypes, "scales-core/helper/content_types"
4
+ autoload :PartialResolver, "scales-core/helper/partial_resolver"
5
+ end
6
+ 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,6 @@
1
+ module Scales
2
+ module PubSub
3
+ autoload :Sync, "scales-core/pub_sub/sync"
4
+ autoload :Async, "scales-core/pub_sub/async"
5
+ end
6
+ 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
+ module PubSub
3
+ module Sync
4
+ class << self
5
+
6
+ def publish(channel, message)
7
+ Storage::Sync.publish(channel, message)
8
+ end
9
+
10
+ def subscribe(channel)
11
+ Storage::Sync.subscribe(channel)
12
+ end
13
+
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,8 @@
1
+ module Scales
2
+ module Queue
3
+ NAME = "scales_request_queue"
4
+
5
+ autoload :Sync, "scales-core/queue/sync"
6
+ autoload :Async, "scales-core/queue/async"
7
+ end
8
+ end
@@ -0,0 +1,18 @@
1
+ module Scales
2
+ module Queue
3
+ module Async
4
+ class << self
5
+
6
+ def add(job)
7
+ Storage::Async.add(Queue::NAME, job)
8
+ end
9
+
10
+ def pop
11
+ Storage::Async.pop(Queue::NAME)
12
+ end
13
+
14
+ end
15
+
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,18 @@
1
+ module Scales
2
+ module Queue
3
+ module Sync
4
+ class << self
5
+
6
+ def add(job)
7
+ Storage::Sync.add(Queue::NAME, job)
8
+ end
9
+
10
+ def pop
11
+ Storage::Sync.pop(Queue::NAME)
12
+ end
13
+
14
+ end
15
+
16
+ end
17
+ end
18
+ 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