blue_print 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (86) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +17 -0
  3. data/.rspec +2 -0
  4. data/.travis.yml +19 -0
  5. data/Gemfile +5 -0
  6. data/LICENSE.txt +22 -0
  7. data/README.md +28 -0
  8. data/Rakefile +1 -0
  9. data/blue_print.gemspec +35 -0
  10. data/gemfiles/rails-3.x.gemfile +5 -0
  11. data/gemfiles/rails-4.x.gemfile +5 -0
  12. data/gemfiles/rails-head.gemfile +5 -0
  13. data/lib/blue_print.rb +21 -0
  14. data/lib/blue_print/active_if.rb +42 -0
  15. data/lib/blue_print/behavior.rb +72 -0
  16. data/lib/blue_print/context.rb +70 -0
  17. data/lib/blue_print/environment.rb +12 -0
  18. data/lib/blue_print/helper.rb +13 -0
  19. data/lib/blue_print/integration.rb +4 -0
  20. data/lib/blue_print/integration/action_controller.rb +38 -0
  21. data/lib/blue_print/railtie.rb +28 -0
  22. data/lib/blue_print/railtie/action_controller.rb +1 -0
  23. data/lib/blue_print/railtie/action_view.rb +1 -0
  24. data/lib/blue_print/railtie/active_record.rb +5 -0
  25. data/lib/blue_print/version.rb +3 -0
  26. data/lib/generators/blue_print/base.rb +34 -0
  27. data/lib/generators/rails/blue_print_generator.rb +43 -0
  28. data/lib/generators/rails/templates/context.rb +15 -0
  29. data/lib/generators/rails/templates/role.rb +8 -0
  30. data/lib/generators/rspec/blue_print_generator.rb +21 -0
  31. data/lib/generators/rspec/templates/context_spec.rb +11 -0
  32. data/lib/generators/rspec/templates/role_spec.rb +5 -0
  33. data/spec/dummy/.gitignore +15 -0
  34. data/spec/dummy/Gemfile +3 -0
  35. data/spec/dummy/README.rdoc +261 -0
  36. data/spec/dummy/Rakefile +7 -0
  37. data/spec/dummy/app/assets/images/rails.png +0 -0
  38. data/spec/dummy/app/assets/javascripts/application.js +15 -0
  39. data/spec/dummy/app/assets/stylesheets/application.css +13 -0
  40. data/spec/dummy/app/controllers/application_controller.rb +3 -0
  41. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  42. data/spec/dummy/app/mailers/.gitkeep +0 -0
  43. data/spec/dummy/app/models/.gitkeep +0 -0
  44. data/spec/dummy/app/views/layouts/application.html.erb +14 -0
  45. data/spec/dummy/config.ru +4 -0
  46. data/spec/dummy/config/application.rb +65 -0
  47. data/spec/dummy/config/boot.rb +6 -0
  48. data/spec/dummy/config/database.yml +25 -0
  49. data/spec/dummy/config/environment.rb +5 -0
  50. data/spec/dummy/config/environments/development.rb +37 -0
  51. data/spec/dummy/config/environments/production.rb +67 -0
  52. data/spec/dummy/config/environments/test.rb +34 -0
  53. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  54. data/spec/dummy/config/initializers/inflections.rb +15 -0
  55. data/spec/dummy/config/initializers/mime_types.rb +5 -0
  56. data/spec/dummy/config/initializers/secret_token.rb +7 -0
  57. data/spec/dummy/config/initializers/session_store.rb +8 -0
  58. data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
  59. data/spec/dummy/config/locales/en.yml +5 -0
  60. data/spec/dummy/config/routes.rb +58 -0
  61. data/spec/dummy/db/seeds.rb +7 -0
  62. data/spec/dummy/lib/assets/.gitkeep +0 -0
  63. data/spec/dummy/lib/tasks/.gitkeep +0 -0
  64. data/spec/dummy/log/.gitkeep +0 -0
  65. data/spec/dummy/public/404.html +26 -0
  66. data/spec/dummy/public/422.html +26 -0
  67. data/spec/dummy/public/500.html +25 -0
  68. data/spec/dummy/public/favicon.ico +0 -0
  69. data/spec/dummy/public/index.html +241 -0
  70. data/spec/dummy/public/robots.txt +5 -0
  71. data/spec/dummy/script/rails +6 -0
  72. data/spec/dummy/vendor/assets/javascripts/.gitkeep +0 -0
  73. data/spec/dummy/vendor/assets/stylesheets/.gitkeep +0 -0
  74. data/spec/dummy/vendor/plugins/.gitkeep +0 -0
  75. data/spec/lib/blue_print/active_if_spec.rb +54 -0
  76. data/spec/lib/blue_print/behavior_spec.rb +113 -0
  77. data/spec/lib/blue_print/context_spec.rb +119 -0
  78. data/spec/lib/blue_print/environment_spec.rb +17 -0
  79. data/spec/lib/blue_print/helper_spec.rb +38 -0
  80. data/spec/lib/blue_print_spec.rb +32 -0
  81. data/spec/lib/generators/rails/blue_print_generator_spec.rb +81 -0
  82. data/spec/lib/generators/rspec/blue_print_generator_spec.rb +47 -0
  83. data/spec/spec_helper.rb +40 -0
  84. data/spec/support/database_cleaner.rb +16 -0
  85. data/spec/support/generator_spec.rb +1 -0
  86. metadata +377 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 71dac53d8435a095608cad84dd4db5b03e349e6b
4
+ data.tar.gz: f4a443d5dd7123450ca46f413d00ce875097c3c4
5
+ SHA512:
6
+ metadata.gz: a763f3cbd70716346034ab3e23676d12c0d6132540d680c551d3a349f8621823c64740625f065aec2ccc3d29f97e7281e7c248372427e39e6f573629fea50879
7
+ data.tar.gz: 421a1e3f72aadc6bda1cec34b4ec9e1917f2c97c32c3b233da68567d74086f0651fed38b414f079ca2fa6203b76af2fbd9c9894baf145aab92b38355d87d4037
@@ -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/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
@@ -0,0 +1,19 @@
1
+ language: ruby
2
+
3
+ rvm:
4
+ - 2.0.0
5
+ - 2.1.0
6
+ - 2.1.1
7
+ - ruby-head
8
+
9
+ gemfile:
10
+ - gemfiles/rails-3.x.gemfile
11
+ - gemfiles/rails-4.x.gemfile
12
+ - gemfiles/rails-head.gemfile
13
+
14
+ matrix:
15
+ allow_failures:
16
+ - rvm: ruby-head
17
+ - gemfile: gemfiles/rails-head.gemfile
18
+
19
+ script: 'bundle exec rspec'
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ gem 'rails'
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Magnet Inc
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.
@@ -0,0 +1,28 @@
1
+ # BluePrint
2
+
3
+ [![Build Status](https://travis-ci.org/magnet-inc/blue_print.png?branch=master)](https://travis-ci.org/magnet-inc/blue_print) [![Coverage Status](https://coveralls.io/repos/magnet-inc/blue_print/badge.png)](https://coveralls.io/r/magnet-inc/blue_print) [![Code Climate](https://codeclimate.com/github/magnet-inc/blue_print.png)](https://codeclimate.com/github/magnet-inc/blue_print)
4
+
5
+ The behavior switching framework for Rails
6
+
7
+ ## Requirements
8
+
9
+ - __Ruby__ >= _2.0.0_
10
+ - __Rails__ >= _3.1.0_
11
+
12
+ ## Installation
13
+
14
+ Add this line to your application's Gemfile:
15
+
16
+ gem 'blue_print', '~> 0.0.1'
17
+
18
+ ## Usage
19
+
20
+ TODO: Write usage instructions here
21
+
22
+ ## Contributing
23
+
24
+ 1. Fork it ( http://github.com/magnet-inc/blue_print/fork )
25
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
26
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
27
+ 4. Push to the branch (`git push origin my-new-feature`)
28
+ 5. Create new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,35 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'blue_print/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'blue_print'
8
+ spec.version = BluePrint::VERSION
9
+ spec.authors = ['Sho Kusano']
10
+ spec.email = ['rosylilly@aduca.org']
11
+ spec.summary = %q{The behavior switching framework for Rails}
12
+ spec.homepage = 'http://magnet-inc.github.io/blue_print'
13
+ spec.license = 'MIT'
14
+
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ['lib']
19
+
20
+ spec.add_development_dependency 'bundler'
21
+ spec.add_development_dependency 'rake'
22
+ spec.add_development_dependency 'rspec'
23
+ spec.add_development_dependency 'simplecov'
24
+ spec.add_development_dependency 'simplecov-console'
25
+ spec.add_development_dependency 'rails', '>= 3.1'
26
+ spec.add_development_dependency 'rspec-rails'
27
+ spec.add_development_dependency 'sqlite3'
28
+ spec.add_development_dependency 'database_cleaner'
29
+ spec.add_development_dependency 'generator_spec'
30
+ spec.add_development_dependency 'coveralls'
31
+
32
+ spec.add_dependency 'activesupport'
33
+ spec.add_dependency 'hashie'
34
+ spec.add_dependency 'thread-parent'
35
+ end
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec path: '../'
4
+
5
+ gem 'rails', '~> 3.1'
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec path: '../'
4
+
5
+ gem 'rails', '~> 4.0'
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec path: '../'
4
+
5
+ gem 'rails', github: 'rails/rails'
@@ -0,0 +1,21 @@
1
+ require 'thread_parent'
2
+ require 'blue_print/behavior'
3
+ require 'blue_print/version'
4
+ require 'blue_print/environment'
5
+ require 'blue_print/context'
6
+ require 'blue_print/helper'
7
+ require 'blue_print/railtie' if defined?(Rails)
8
+
9
+ module BluePrint
10
+ def self.env
11
+ Thread.current[:blue_print] ||= Thread.parents[:blue_print]
12
+ end
13
+
14
+ def self.env=(env)
15
+ Thread.current[:blue_print] = env
16
+ end
17
+
18
+ def self.clear!
19
+ Thread.current[:blue_print] = nil
20
+ end
21
+ end
@@ -0,0 +1,42 @@
1
+ require 'blue_print'
2
+
3
+ class BluePrint::ActiveIf
4
+ NAMED_ACTIVE_IFS_MAP = {}
5
+
6
+ def self.resolve(name)
7
+ if name.respond_to?(:active?)
8
+ name
9
+ else
10
+ NAMED_ACTIVE_IFS_MAP[name.to_s.to_sym]
11
+ end
12
+ end
13
+
14
+ def initialize(name = nil, &logic)
15
+ @name = name
16
+ @logic = logic
17
+
18
+ NAMED_ACTIVE_IFS_MAP[name] = self unless anonymous?
19
+ end
20
+
21
+ def name
22
+ @name ? @name.to_s.to_sym : nil
23
+ end
24
+
25
+ def anonymous?
26
+ name.nil?
27
+ end
28
+
29
+ def cache_key
30
+ @cache_key ||= "blue_print_active_id_#{name || object_id}".to_sym
31
+ end
32
+
33
+ def active?
34
+ return BluePrint.env[cache_key] if BluePrint.env.key?(cache_key)
35
+
36
+ BluePrint.env[cache_key] = BluePrint.env.within(&@logic)
37
+ end
38
+
39
+ def deactive?
40
+ !active?
41
+ end
42
+ end
@@ -0,0 +1,72 @@
1
+ require 'blue_print'
2
+ require 'blue_print/helper'
3
+
4
+ module BluePrint::Behavior
5
+ def self.auto_generate_class_methods?(base)
6
+ base.is_a?(Module) && !base.name.match(/ClassMethods$/)
7
+ end
8
+
9
+ def self.extended(base)
10
+ if auto_generate_class_methods?(base)
11
+ base.module_eval <<-EOC
12
+ module ClassMethods
13
+ extend BluePrint::Behavior
14
+ end
15
+ EOC
16
+ end
17
+ end
18
+
19
+ def prepended(base)
20
+ base.send(:include, BluePrint::Helper)
21
+
22
+ if const_defined?('ClassMethods')
23
+ singleton = class << base; self; end
24
+ singleton.send(:prepend, const_get('ClassMethods'))
25
+ end
26
+ end
27
+
28
+ def context_name
29
+ @context_name ||= name.to_s.underscore.tap do |name|
30
+ name.sub!(/\/class_methods$/, '')
31
+ name.sub!(%r{/[^/]+?$}, '')
32
+ end.to_sym
33
+ end
34
+
35
+ def context
36
+ context_name.to_s.camelize.safe_constantize
37
+ end
38
+
39
+ def behavior_name
40
+ @behavior_name ||=
41
+ name.to_s.underscore.sub(/\/class_methods$/, '').gsub('/', '__').to_sym
42
+ end
43
+
44
+ def override_methods
45
+ @override_methods ||= []
46
+ end
47
+
48
+ def define_safe_method(target, punctuation, method_name)
49
+ alias_method("#{target}_with_#{behavior_name}#{punctuation}", method_name)
50
+
51
+ module_eval <<-EOC
52
+ def #{method_name}(*args)
53
+ within_cotext_of(#{context}, proc { super(*args) }) do
54
+ #{target}_with_#{behavior_name}#{punctuation}(*args)
55
+ end
56
+ end
57
+ EOC
58
+ end
59
+
60
+ def method_added(method_name)
61
+ return if @ignore_added_hook
62
+
63
+ override_methods.push(method_name)
64
+
65
+ @ignore_added_hook = true
66
+ aliased_target = method_name.to_s.sub(/([?!=])$/, '')
67
+ punctuation = Regexp.last_match ? Regexp.last_match[1] : ''
68
+
69
+ define_safe_method(aliased_target, punctuation, method_name)
70
+ @ignore_added_hook = false
71
+ end
72
+ end
@@ -0,0 +1,70 @@
1
+ require 'active_support/inflector'
2
+ require 'blue_print'
3
+ require 'blue_print/active_if'
4
+
5
+ class BluePrint::Context
6
+ NAMED_CONTEXT_MAP = {}
7
+
8
+ def self.inherited(klass)
9
+ NAMED_CONTEXT_MAP[klass.context_name] = klass
10
+ end
11
+
12
+ def self.resolve(name)
13
+ if name.respond_to?(:active?)
14
+ return name
15
+ else
16
+ NAMED_CONTEXT_MAP[name.to_s.underscore.to_sym]
17
+ end
18
+ end
19
+
20
+ def self.active_ifs
21
+ @active_ifs ||= []
22
+ end
23
+
24
+ def self.active_if(*names, &logic)
25
+ active_ifs.concat(
26
+ names.map { |name| BluePrint::ActiveIf.resolve(name) }.reject(&:nil?)
27
+ )
28
+ active_ifs.push(BluePrint::ActiveIf.new(&logic)) if logic
29
+ end
30
+
31
+ def self.context_name
32
+ @context_name ||= name.to_s.underscore.to_sym
33
+ end
34
+
35
+ def self.active?
36
+ return BluePrint.env[context_name] if BluePrint.env.key?(context_name)
37
+
38
+ action!
39
+
40
+ BluePrint.env[context_name] = !!active_ifs.reduce do |memo, active_if|
41
+ memo && active_if.active?
42
+ end
43
+ end
44
+
45
+ def self.deactive?
46
+ !active?
47
+ end
48
+
49
+ def self.casting
50
+ @casting ||= Hash.new { |casting, klass| casting[klass] = [] }
51
+ end
52
+
53
+ def self.cast(actor, as: [])
54
+ as = [as] unless as.kind_of?(Array)
55
+ as.map! { |role| role.is_a?(Module) ? role : role.to_s.safe_constantize }
56
+ casting[actor] = casting[actor] | as
57
+ end
58
+
59
+ def self.action!
60
+ return if @acted
61
+
62
+ casting.each_pair do |klass, roles|
63
+ roles.each do |role|
64
+ klass.send(:prepend, role)
65
+ end
66
+ end
67
+
68
+ @acted = true
69
+ end
70
+ end
@@ -0,0 +1,12 @@
1
+ require 'hashie'
2
+ require 'blue_print'
3
+
4
+ class BluePrint::Environment < Hashie::Mash
5
+ def initialize(context)
6
+ @context = context
7
+ end
8
+
9
+ def within(&block)
10
+ @context.instance_exec(self, &block)
11
+ end
12
+ end
@@ -0,0 +1,13 @@
1
+ require 'blue_print'
2
+
3
+ module BluePrint::Helper
4
+ def within_cotext_of(name_or_context, fallback = nil, &block)
5
+ context = BluePrint::Context.resolve(name_or_context)
6
+
7
+ if context.active?
8
+ yield BluePrint.env
9
+ else
10
+ fallback.respond_to?(:call) && fallback.call
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,4 @@
1
+ require 'blue_print'
2
+
3
+ module BluePrint::Integration
4
+ end
@@ -0,0 +1,38 @@
1
+ require 'active_support/concern'
2
+ require 'blue_print/integration'
3
+ require 'blue_print/helper'
4
+
5
+ module BluePrint::Integration::ActionController
6
+ extend ActiveSupport::Concern
7
+
8
+ include BluePrint::Helper
9
+
10
+ included do
11
+ if respond_to?(:before_action)
12
+ before_action(:build_blue_print_environment)
13
+ else
14
+ before_filter(:build_blue_print_environment)
15
+ end
16
+
17
+ if respond_to?(:after_action)
18
+ after_action(:clear_blue_print_environment)
19
+ else
20
+ after_filter(:clear_blue_print_environment)
21
+ end
22
+ end
23
+
24
+ private
25
+
26
+ def build_blue_print_environment
27
+ BluePrint.env = BluePrint::Environment.new(self)
28
+ end
29
+
30
+ def clear_blue_print_environment
31
+ BluePrint.clear!
32
+ end
33
+ end
34
+
35
+ ::ActionController::Base.send(
36
+ :include,
37
+ BluePrint::Integration::ActionController
38
+ )