autodeps 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 5a82a1eed613fef969c31d65598dfa94f222f79c
4
+ data.tar.gz: aba6eadbf53b938b03bfc5c1f8bc34f4fc886479
5
+ SHA512:
6
+ metadata.gz: f488ed931e63f8c34def7194d816f385f050e8405ea618d09ae67706218aa8fbfe7f5daea7bd02527cd7d57a4450b3552e60768c4b4152870bac1fafcdc09980
7
+ data.tar.gz: 86806e9a550bf0a6dbabc132d24eb84089cb219e9de0bba02d8a1b991ed8736f546f207af83f449bdd459dc33b72fcf81d485aff1f607d4d12188ba0eccc3bfa
data/.gitignore ADDED
@@ -0,0 +1,20 @@
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
18
+
19
+ *.bak
20
+ .idea
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in autodeps.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 femto
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
+ # Autodeps
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'autodeps'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install autodeps
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it ( http://github.com/<my-github-username>/autodeps/fork )
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add 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 @@
1
+ require "bundler/gem_tasks"
data/autodeps.gemspec ADDED
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'autodeps/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "autodeps"
8
+ spec.version = Autodeps::VERSION
9
+ spec.authors = ["femto"]
10
+ spec.email = ["femtowin@gmail.com"]
11
+ spec.summary = %q{autodeps}
12
+ spec.description = %q{autodeps}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.5"
22
+ spec.add_development_dependency "rake"
23
+ end
data/lib/autodeps.rb ADDED
@@ -0,0 +1,11 @@
1
+ require "autodeps/version"
2
+
3
+ module Autodeps
4
+ # Your code goes here...
5
+ end
6
+
7
+ require "autodeps/autodeps"
8
+ require "autodeps/computation"
9
+ require "autodeps/dependency"
10
+ require "autodeps/reactive_data"
11
+ require "autodeps/persistency"
@@ -0,0 +1,70 @@
1
+ module Autodeps
2
+ @pending_computations = []
3
+ @after_flush_callbacks = []
4
+ @constructingComputation = false
5
+ @active = false
6
+
7
+ class << self
8
+ attr_accessor :active
9
+ def add_pending_computation(pending_computation)
10
+ @pending_computations << pending_computation
11
+ end
12
+
13
+ def autorun(&block)
14
+ raise 'Autodeps.autorun requires a block' if block.nil?
15
+
16
+ @constructingComputation = true
17
+ c = Computation.new(block, Autodeps.current_computation);
18
+
19
+ return c
20
+ end
21
+
22
+ def flush
23
+
24
+ #if (inFlush)
25
+ # throw new Error("Can't call Deps.flush while flushing");
26
+ #
27
+ # if (inCompute)
28
+ # throw new Error("Can't flush inside Deps.autorun");
29
+
30
+ @inFlush = true
31
+ @willFlush = true
32
+
33
+
34
+ while (@pending_computations.length > 0 ||
35
+ @after_flush_callbacks.length > 0) do
36
+
37
+ #recompute all pending computations
38
+ comps = @pending_computations;
39
+ @pending_computations = [];
40
+
41
+ comps.each do |comp|
42
+ comp.recompute();
43
+
44
+ #if (afterFlushCallbacks.length) {
45
+ # // call one afterFlush callback, which may
46
+ #// invalidate more computations
47
+ #var func = afterFlushCallbacks.shift();
48
+ #try {
49
+ # func();
50
+ #} catch (e) {
51
+ # _debugFunc()("Exception from Deps afterFlush function:",
52
+ # e.stack || e.message);
53
+ #}
54
+ end
55
+ end
56
+
57
+ inFlush = false;
58
+ willFlush = false;
59
+
60
+ end
61
+
62
+ def current_computation
63
+ Thread.current["Autodeps::current_computation"]
64
+ end
65
+ def current_computation=(computation)
66
+ Thread.current["Autodeps::current_computation"] = computation
67
+ self.active = !! computation;
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,83 @@
1
+ module Autodeps
2
+ class Computation
3
+ attr_accessor :stopped, :invalidated, :first_run, :parent, :block, :recomputing
4
+ def initialize(block, parent=nil)
5
+ self.stopped = false;
6
+ self.invalidated = false;
7
+ self.first_run = true;
8
+
9
+ self.parent = parent;
10
+ self.block = block;
11
+ self.recomputing = false;
12
+
13
+ errored = true;
14
+ begin
15
+ self.compute();
16
+ errored = false;
17
+ rescue => e
18
+ raise e
19
+ ensure
20
+ self.first_run = false;
21
+ self.stop() if errored
22
+ end
23
+ end
24
+
25
+ def compute
26
+ self.invalidated = false;
27
+
28
+ previous = Autodeps.current_computation;
29
+ Autodeps.current_computation = self;
30
+
31
+ in_compute = true;
32
+ begin
33
+ block.call(self)
34
+ ensure
35
+ Autodeps.current_computation = previous;
36
+ in_compute = false;
37
+ end
38
+ end
39
+
40
+ def recompute
41
+ self.recomputing = true
42
+
43
+ while (self.invalidated && !self.stopped)
44
+ self.compute() rescue nil
45
+ end
46
+
47
+ self.recomputing = false;
48
+ end
49
+
50
+ def stop
51
+ if (! self.stopped)
52
+ self.stopped = true;
53
+ self.invalidate();
54
+ end
55
+ end
56
+
57
+ def require_flush
58
+ Autodeps::flush
59
+ end
60
+
61
+
62
+ def invalidate
63
+ #we request an immediate flush because we don't have timeout
64
+
65
+
66
+
67
+ if (! self.invalidated)
68
+ # if we're currently in _recompute(), don't enqueue
69
+ # ourselves, since we'll rerun immediately anyway.
70
+ if (! self.recomputing && ! self.stopped)
71
+ self.invalidated = true;
72
+ Autodeps.add_pending_computation(self);
73
+ require_flush();
74
+ end
75
+
76
+
77
+
78
+ # callbacks can't add callbacks, because
79
+ #self.invalidated === true.
80
+ end
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,31 @@
1
+ module Autodeps
2
+ class Dependency
3
+ attr_accessor :dependents
4
+ def initialize
5
+ @dependents = []
6
+ end
7
+ def depend(computation = Autodeps.current_computation)
8
+
9
+ if (!computation)
10
+ return false if (!Autodeps.active)
11
+ computation = Deps.current_computation;
12
+ end
13
+ if !@dependents.include?(computation)
14
+ @dependents << computation
15
+ #computation.onInvalidate(function () {
16
+ # delete self._dependentsById[id];
17
+ #});
18
+ return true
19
+ else
20
+ return false;
21
+ end
22
+
23
+ end
24
+
25
+ def changed
26
+ @dependents.each do |computation|
27
+ computation.invalidate
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,57 @@
1
+ module Autodeps
2
+ module Persistency
3
+ class Mapping
4
+ attr_accessor :dependent, :key_mapping, :value_mapping
5
+ def initialize(dependent, key_mapping, value_mapping)
6
+ @dependent = dependent
7
+ @key_mapping = key_mapping
8
+ @value_mapping = value_mapping
9
+ end
10
+ end
11
+ def self.included(base)
12
+ super
13
+ base.extend(ClassMethods)
14
+ end
15
+
16
+ module ClassMethods
17
+ def depend_on(clazz, options={})
18
+ clazz = Object.const_get(clazz) if clazz.is_a?(String)
19
+ options[:key_mapping] ||= {:id => (clazz.name.underscore.gsub(/^.*\//,"") + "_id").to_sym}
20
+ class << clazz
21
+ attr_accessor :_deps, :_autodeps_after_save_callbacked
22
+ end
23
+ clazz._deps ||= {}
24
+
25
+ # options[:value_mapping].keys.sort?
26
+ (clazz._deps[options[:value_mapping].keys.sort] ||= []) << Mapping.new(self, options[:key_mapping], options[:value_mapping] )
27
+
28
+ Autodeps.autorun do
29
+
30
+ end
31
+ if !clazz._autodeps_after_save_callbacked
32
+ clazz._autodeps_after_save_callbacked = true
33
+ clazz.send(:after_save) do
34
+ clazz._deps.each do |attribute_keys, values|
35
+ if attribute_keys.any? {|attribute_key| self.send("#{attribute_key.to_s}_changed?")}
36
+ values.each do |mapping|
37
+ relation = mapping.dependent
38
+ mapping.key_mapping.each do |source_key, target_key|
39
+ relation = relation.where(target_key => self.send(source_key))
40
+ end
41
+
42
+ relation.each do |tuple|
43
+ mapping.value_mapping.each do |source_key, target_key|
44
+ tuple[target_key] = self.send(source_key)
45
+ end
46
+ tuple.save
47
+ end
48
+ end
49
+ end
50
+ end
51
+
52
+ end
53
+ end
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,22 @@
1
+ module Autodeps
2
+ class ReactiveData
3
+ attr_accessor :value, :dep
4
+ def initialize(value)
5
+ @value = value
6
+ @dep = Autodeps::Dependency.new
7
+ end
8
+ def change_to(value)
9
+ if value != @value
10
+ changed = true
11
+ end
12
+ @value = value
13
+ if changed
14
+ @dep.changed
15
+ end
16
+ end
17
+ def value
18
+ dep.depend
19
+ @value
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,3 @@
1
+ module Autodeps
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,51 @@
1
+ require '../test/test_helper'
2
+ require 'test/unit'
3
+
4
+
5
+
6
+ class AutoDepsTest < Test::Unit::TestCase
7
+ def test_invalidate
8
+ a = 3
9
+ b = nil
10
+ computation = Autodeps.autorun do |computation|
11
+ b = a
12
+ end
13
+ assert_equal b,a
14
+
15
+ a = 5
16
+ computation.invalidate
17
+ assert_equal b,a
18
+
19
+ end
20
+
21
+ def test_reactive_integer
22
+ a = Autodeps::ReactiveData.new(3)
23
+ b = nil
24
+ computation = Autodeps.autorun do |computation|
25
+ b = a.value
26
+ end
27
+ assert_equal b,3
28
+
29
+ a.change_to 5
30
+
31
+ assert_equal b,5
32
+ end
33
+
34
+ def test_reactive_integer_add
35
+ a = Autodeps::ReactiveData.new(3)
36
+ b = Autodeps::ReactiveData.new(5)
37
+ c = nil
38
+ computation = Autodeps.autorun do |computation|
39
+ c = a.value + b.value
40
+ end
41
+ assert_equal c,8
42
+
43
+ a.change_to 5
44
+
45
+ assert_equal c,10
46
+
47
+ b.change_to 15
48
+
49
+ assert_equal c,20
50
+ end
51
+ end
@@ -0,0 +1,2 @@
1
+ $: << File.join(File.expand_path(File.dirname(__FILE__)),"..","lib")
2
+ require 'autodeps'
metadata ADDED
@@ -0,0 +1,90 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: autodeps
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - femto
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-02-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.5'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: autodeps
42
+ email:
43
+ - femtowin@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - .gitignore
49
+ - Gemfile
50
+ - LICENSE.txt
51
+ - README.md
52
+ - Rakefile
53
+ - autodeps.gemspec
54
+ - lib/autodeps.rb
55
+ - lib/autodeps/autodeps.rb
56
+ - lib/autodeps/computation.rb
57
+ - lib/autodeps/dependency.rb
58
+ - lib/autodeps/persistency.rb
59
+ - lib/autodeps/reactive_data.rb
60
+ - lib/autodeps/version.rb
61
+ - test/autodep_test.rb
62
+ - test/test_helper.rb
63
+ homepage: ''
64
+ licenses:
65
+ - MIT
66
+ metadata: {}
67
+ post_install_message:
68
+ rdoc_options: []
69
+ require_paths:
70
+ - lib
71
+ required_ruby_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ required_rubygems_version: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - '>='
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ requirements: []
82
+ rubyforge_project:
83
+ rubygems_version: 2.0.14
84
+ signing_key:
85
+ specification_version: 4
86
+ summary: autodeps
87
+ test_files:
88
+ - test/autodep_test.rb
89
+ - test/test_helper.rb
90
+ has_rdoc: