active_logic 0.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.
Files changed (58) hide show
  1. checksums.yaml +7 -0
  2. data/Rakefile +32 -0
  3. data/lib/active_logic.rb +18 -0
  4. data/lib/active_logic/acts_as_logic.rb +113 -0
  5. data/lib/active_logic/config.rb +21 -0
  6. data/lib/active_logic/version.rb +3 -0
  7. data/lib/generators/active_logic/install_generator.rb +18 -0
  8. data/lib/generators/active_logic/templates/initializer.rb +4 -0
  9. data/lib/generators/active_logic/utils.rb +19 -0
  10. data/lib/tasks/active_logic_tasks.rake +13 -0
  11. data/test/activelogic_test.rb +7 -0
  12. data/test/acts_as_logic_test.rb +5 -0
  13. data/test/dummy/README.rdoc +28 -0
  14. data/test/dummy/Rakefile +6 -0
  15. data/test/dummy/app/assets/javascripts/application.js +13 -0
  16. data/test/dummy/app/assets/stylesheets/application.css +13 -0
  17. data/test/dummy/app/controllers/application_controller.rb +5 -0
  18. data/test/dummy/app/helpers/application_helper.rb +2 -0
  19. data/test/dummy/app/models/deal.rb +3 -0
  20. data/test/dummy/app/models/declaration.rb +2 -0
  21. data/test/dummy/app/views/layouts/application.html.erb +14 -0
  22. data/test/dummy/bin/bundle +3 -0
  23. data/test/dummy/bin/rails +4 -0
  24. data/test/dummy/bin/rake +4 -0
  25. data/test/dummy/config.ru +4 -0
  26. data/test/dummy/config/application.rb +22 -0
  27. data/test/dummy/config/boot.rb +5 -0
  28. data/test/dummy/config/database.yml +25 -0
  29. data/test/dummy/config/environment.rb +5 -0
  30. data/test/dummy/config/environments/development.rb +29 -0
  31. data/test/dummy/config/environments/production.rb +80 -0
  32. data/test/dummy/config/environments/test.rb +36 -0
  33. data/test/dummy/config/initializers/active_logic.rb +4 -0
  34. data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
  35. data/test/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  36. data/test/dummy/config/initializers/inflections.rb +16 -0
  37. data/test/dummy/config/initializers/mime_types.rb +5 -0
  38. data/test/dummy/config/initializers/secret_token.rb +12 -0
  39. data/test/dummy/config/initializers/session_store.rb +3 -0
  40. data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
  41. data/test/dummy/config/locales/en.yml +23 -0
  42. data/test/dummy/config/routes.rb +56 -0
  43. data/test/dummy/db/development.sqlite3 +0 -0
  44. data/test/dummy/db/migrate/20140601105836_create_deals.rb +9 -0
  45. data/test/dummy/db/migrate/20140603111616_create_declarations.rb +11 -0
  46. data/test/dummy/db/schema.rb +30 -0
  47. data/test/dummy/log/development.log +69 -0
  48. data/test/dummy/log/test.log +36 -0
  49. data/test/dummy/public/404.html +58 -0
  50. data/test/dummy/public/422.html +58 -0
  51. data/test/dummy/public/500.html +57 -0
  52. data/test/dummy/public/favicon.ico +0 -0
  53. data/test/dummy/test/fixtures/deals.yml +7 -0
  54. data/test/dummy/test/fixtures/declarations.yml +11 -0
  55. data/test/dummy/test/models/deal_test.rb +5 -0
  56. data/test/dummy/test/models/declaration_test.rb +7 -0
  57. data/test/test_helper.rb +15 -0
  58. metadata +174 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 3593a455629121dbb4453a6578b327398f3648a0
4
+ data.tar.gz: 93239ef28165d559468f15d620244af4b686c241
5
+ SHA512:
6
+ metadata.gz: c9011574080aa1b3eed97fcaf63dfcceace31e7bf182651851ae8f068006f373cc36821967ebd4b8afe5e94d293738f72496b36ec07a55feeb9606091ace2765
7
+ data.tar.gz: 7bc85f8120ce4f9a00ac2dd763b00808cc55a3e306f969c911718a4b125e7d39991e515c44954844aafda78f01cff19c5ee22759a20ca3eb26a8ce62bc3eeb5a
data/Rakefile ADDED
@@ -0,0 +1,32 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'Activelogic'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.rdoc')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+
18
+
19
+
20
+ Bundler::GemHelper.install_tasks
21
+
22
+ require 'rake/testtask'
23
+
24
+ Rake::TestTask.new(:test) do |t|
25
+ t.libs << 'lib'
26
+ t.libs << 'test'
27
+ t.pattern = 'test/**/*_test.rb'
28
+ t.verbose = false
29
+ end
30
+
31
+
32
+ task default: :test
@@ -0,0 +1,18 @@
1
+ require 'active_logic/version'
2
+ require 'active_logic/config'
3
+
4
+ require 'active_logic/acts_as_logic'
5
+
6
+ module ActiveLogic
7
+ def self.config
8
+ if block_given?
9
+ yield(ActiveLogic::Config)
10
+ else
11
+ ActiveLogic::Config
12
+ end
13
+ end
14
+
15
+ def self.model
16
+ ActiveLogic.config.model_name.to_s.camelize.constantize
17
+ end
18
+ end
@@ -0,0 +1,113 @@
1
+ require 'active_record/base'
2
+
3
+ module ActiveLogic
4
+ module ActsAsLogic
5
+ extend ActiveSupport::Concern
6
+
7
+ included do
8
+
9
+ end
10
+
11
+ module ClassMethods
12
+ def acts_as_logic(options = {})
13
+ include ActiveLogic::ActsAsLogic::LocalInstanceMethods
14
+ end
15
+ end
16
+
17
+ module LocalInstanceMethods
18
+ def declare(verb, *arguments)
19
+ if not items_are_records?(arguments)
20
+ throw 'Arguments should be all records.'
21
+ end
22
+
23
+ ActiveLogic.model.find_or_create_by subject: record_to_json(self), verb: verb, arguments: records_to_json(arguments)
24
+ end
25
+
26
+ def undeclare(verb, *arguments)
27
+ query(verb, *arguments).each do |declaration|
28
+ declaration.destroy
29
+ end
30
+ end
31
+
32
+ def ask(verb, *arguments)
33
+ query(verb, *arguments).count > 0
34
+ end
35
+
36
+ private
37
+ def query(verb, *arguments)
38
+ if(symbol_or_string?(verb) and items_are_records?(arguments))
39
+ result = ActiveLogic.model.find_by(subject: record_to_json(self), verb: verb.to_s, arguments: records_to_json(arguments))
40
+ if result
41
+ return [ result ]
42
+ else
43
+ return []
44
+ end
45
+ end
46
+
47
+ results = []
48
+ return ActiveLogic.model.find_by(subject: record_to_json(self)).each do |declaration|
49
+ declaration_verb = declaration.verb
50
+ declaration_arguments = ActiveSupport::JSON.decode(declaration.arguments)
51
+ if not declaration_arguments.count == arguments.count
52
+ next
53
+ end
54
+
55
+ valid = match_query(verb, declaration.verb) do |query, origin|
56
+ query.to_s == origin
57
+ end
58
+
59
+ declaration_arguments.each do |declaration_argument, i|
60
+ vaild = vaild && match_query(arguments[i], declaration_argument) do |query, origin|
61
+ query.class.name == origin['class'] and query.id == origin['id']
62
+ end
63
+ end
64
+
65
+ valid
66
+ end
67
+ end
68
+
69
+ def match_query(query, origin, &block)
70
+ if query.kind_of? Proc
71
+ if query(origin)
72
+ return true
73
+ else
74
+ return false
75
+ end
76
+ else
77
+ if block(query, origin)
78
+ return true
79
+ else
80
+ return false
81
+ end
82
+ end
83
+ end
84
+
85
+ def record_to_hash(record)
86
+ { class: record.class.name, id: record.id }
87
+ end
88
+
89
+ def record_to_json(record)
90
+ record_to_hash(record).to_json
91
+ end
92
+
93
+ def records_to_json(records)
94
+ records.map{|record| record_to_hash(record) }.to_json
95
+ end
96
+
97
+ def items_are_records?(records)
98
+ records.each do |p|
99
+ if not p.kind_of? ActiveRecord::Base
100
+ return false
101
+ end
102
+ end
103
+ return true
104
+ end
105
+
106
+ def symbol_or_string?(item)
107
+ item.kind_of? Symbol or item.kind_of? String
108
+ end
109
+ end
110
+ end
111
+ end
112
+
113
+ ActiveRecord::Base.send :include, ActiveLogic::ActsAsLogic
@@ -0,0 +1,21 @@
1
+ require 'active_support/core_ext/module/attribute_accessors'
2
+
3
+ module ActiveLogic
4
+ module Config
5
+ DEFAULT_MODEL_NAME = :declaration
6
+
7
+ def self.model_name
8
+ @@model_name
9
+ end
10
+
11
+ def self.model_name=(value)
12
+ @@model_name = value
13
+ end
14
+
15
+ def self.reset
16
+ @@model_name = DEFAULT_MODEL_NAME
17
+ end
18
+
19
+ reset
20
+ end
21
+ end
@@ -0,0 +1,3 @@
1
+ module ActiveLogic
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,18 @@
1
+ require File.expand_path('../utils', __FILE__)
2
+ require 'rails/generators'
3
+
4
+ module ActiveLogic
5
+ class InstallGenerator < Rails::Generators::Base
6
+ source_root File.expand_path('../templates', __FILE__)
7
+ include Generators::Utils::InstanceMethods
8
+
9
+ argument :_model_name, type: :string, required: false, desc: 'ActiveLogic declaration model name'
10
+ desc 'ActiveLogic installation generator'
11
+
12
+ def install
13
+ @model_name = ask_for('What model name do you want to put new declarations?', 'declaration', _model_name)
14
+ generate 'model', "#{@model_name} subject:string verb:string arguments:text"
15
+ template 'initializer.rb', 'config/initializers/active_logic.rb'
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,4 @@
1
+ ActiveLogic.config do |config|
2
+ # Logic declaration model name
3
+ config.model_name = :<%= @model_name %>
4
+ end
@@ -0,0 +1,19 @@
1
+ module ActiveLogic
2
+ module Generators
3
+ module Utils
4
+ module InstanceMethods
5
+ def display(output, color = :green)
6
+ say(" - #{output}", color)
7
+ end
8
+
9
+ def ask_for(wording, default_value = nil, override_if_present_value = nil)
10
+ if override_if_present_value.present?
11
+ display("Using [#{override_if_present_value}] for question '#{wording}'") && override_if_present_value
12
+ else
13
+ ask(" ? #{wording} Press <enter> for [#{default_value}] >", :yellow).presence || default_value
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,13 @@
1
+ # desc "Explaining what the task does"
2
+ # task :activelogic do
3
+ # # Task goes here
4
+ # end
5
+
6
+ namespace :active_logic do
7
+
8
+ desc "Setup ActiveLogic Plugin"
9
+ task :install do
10
+ system 'rails generate activelogic:install'
11
+ end
12
+
13
+ end
@@ -0,0 +1,7 @@
1
+ require 'test_helper'
2
+
3
+ class ActivelogicTest < ActiveSupport::TestCase
4
+ test "truth" do
5
+ assert_kind_of Module, Activelogic
6
+ end
7
+ end
@@ -0,0 +1,5 @@
1
+ require 'test_helper'
2
+
3
+ class ActsAsLogicTest < ActiveSupport::TestCase
4
+
5
+ end
@@ -0,0 +1,28 @@
1
+ == README
2
+
3
+ This README would normally document whatever steps are necessary to get the
4
+ application up and running.
5
+
6
+ Things you may want to cover:
7
+
8
+ * Ruby version
9
+
10
+ * System dependencies
11
+
12
+ * Configuration
13
+
14
+ * Database creation
15
+
16
+ * Database initialization
17
+
18
+ * How to run the test suite
19
+
20
+ * Services (job queues, cache servers, search engines, etc.)
21
+
22
+ * Deployment instructions
23
+
24
+ * ...
25
+
26
+
27
+ Please feel free to use a different markup language if you do not plan to run
28
+ <tt>rake doc:app</tt>.
@@ -0,0 +1,6 @@
1
+ # Add your own tasks in files placed in lib/tasks ending in .rake,
2
+ # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
3
+
4
+ require File.expand_path('../config/application', __FILE__)
5
+
6
+ Dummy::Application.load_tasks
@@ -0,0 +1,13 @@
1
+ // This is a manifest file that'll be compiled into application.js, which will include all the files
2
+ // listed below.
3
+ //
4
+ // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
5
+ // or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
6
+ //
7
+ // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
8
+ // compiled file.
9
+ //
10
+ // Read Sprockets README (https://github.com/sstephenson/sprockets#sprockets-directives) for details
11
+ // about supported directives.
12
+ //
13
+ //= require_tree .
@@ -0,0 +1,13 @@
1
+ /*
2
+ * This is a manifest file that'll be compiled into application.css, which will include all the files
3
+ * listed below.
4
+ *
5
+ * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6
+ * or vendor/assets/stylesheets of plugins, if any, can be referenced here using a relative path.
7
+ *
8
+ * You're free to add application-wide styles to this file and they'll appear at the top of the
9
+ * compiled file, but it's generally better to create a new file per style scope.
10
+ *
11
+ *= require_self
12
+ *= require_tree .
13
+ */
@@ -0,0 +1,5 @@
1
+ class ApplicationController < ActionController::Base
2
+ # Prevent CSRF attacks by raising an exception.
3
+ # For APIs, you may want to use :null_session instead.
4
+ protect_from_forgery with: :exception
5
+ end
@@ -0,0 +1,2 @@
1
+ module ApplicationHelper
2
+ end
@@ -0,0 +1,3 @@
1
+ class Deal < ActiveRecord::Base
2
+ acts_as_logic
3
+ end
@@ -0,0 +1,2 @@
1
+ class Declaration < ActiveRecord::Base
2
+ end
@@ -0,0 +1,14 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>Dummy</title>
5
+ <%= stylesheet_link_tag "application", media: "all", "data-turbolinks-track" => true %>
6
+ <%= javascript_include_tag "application", "data-turbolinks-track" => true %>
7
+ <%= csrf_meta_tags %>
8
+ </head>
9
+ <body>
10
+
11
+ <%= yield %>
12
+
13
+ </body>
14
+ </html>
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env ruby2.0
2
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__)
3
+ load Gem.bin_path('bundler', 'bundle')
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby2.0
2
+ APP_PATH = File.expand_path('../../config/application', __FILE__)
3
+ require_relative '../config/boot'
4
+ require 'rails/commands'
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby2.0
2
+ require_relative '../config/boot'
3
+ require 'rake'
4
+ Rake.application.run
@@ -0,0 +1,4 @@
1
+ # This file is used by Rack-based servers to start the application.
2
+
3
+ require ::File.expand_path('../config/environment', __FILE__)
4
+ run Rails.application
@@ -0,0 +1,22 @@
1
+ require File.expand_path('../boot', __FILE__)
2
+
3
+ require 'rails/all'
4
+
5
+ Bundler.require(*Rails.groups)
6
+ require "active_logic"
7
+
8
+ module Dummy
9
+ class Application < Rails::Application
10
+ # Settings in config/environments/* take precedence over those specified here.
11
+ # Application configuration should go into files in config/initializers
12
+ # -- all .rb files in that directory are automatically loaded.
13
+
14
+ # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
15
+ # Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
16
+ # config.time_zone = 'Central Time (US & Canada)'
17
+
18
+ # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
19
+ # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
20
+ # config.i18n.default_locale = :de
21
+ end
22
+ end
@@ -0,0 +1,5 @@
1
+ # Set up gems listed in the Gemfile.
2
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../../../Gemfile', __FILE__)
3
+
4
+ require 'bundler/setup' if File.exists?(ENV['BUNDLE_GEMFILE'])
5
+ $LOAD_PATH.unshift File.expand_path('../../../../lib', __FILE__)
@@ -0,0 +1,25 @@
1
+ # SQLite version 3.x
2
+ # gem install sqlite3
3
+ #
4
+ # Ensure the SQLite 3 gem is defined in your Gemfile
5
+ # gem 'sqlite3'
6
+ development:
7
+ adapter: sqlite3
8
+ database: db/development.sqlite3
9
+ pool: 5
10
+ timeout: 5000
11
+
12
+ # Warning: The database defined as "test" will be erased and
13
+ # re-generated from your development database when you run "rake".
14
+ # Do not set this db to the same as development or production.
15
+ test:
16
+ adapter: sqlite3
17
+ database: db/test.sqlite3
18
+ pool: 5
19
+ timeout: 5000
20
+
21
+ production:
22
+ adapter: sqlite3
23
+ database: db/production.sqlite3
24
+ pool: 5
25
+ timeout: 5000