activekit 0.2.2 → 0.2.3.dev2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c2a8a1118c555e7d450d530a4d844e43d718acf3c879c5e21616e2607c46bd7e
4
- data.tar.gz: 838dd18ff937255d7f860ad0279f886123de8367ae9182cd3f93d1bf6dcbd829
3
+ metadata.gz: 95241693c8cee6d5b29f2f8172abf1a5b31f70ea29a529595db9d5f71d24b948
4
+ data.tar.gz: e00f4986129a42c3444daee94921081738e763f9edd9de704567b0f9aab7eb3e
5
5
  SHA512:
6
- metadata.gz: 9f5946fd34c134e6b0b11e70a3c5f307dc4c83d77bcfd11003e6d7aeddce7772202ba96d13e3638060d313231ee7390c6dd80a0d9d23de11ed2e10e54ba97b96
7
- data.tar.gz: 171003ab2c3b2c8e4f9c8f098c5b5811cf2e3e6dd9d2315658bbdfaa1beaea0b7ce3551d60bad7bd9425b39783d570236f43cd25cb5e67c0084e9ed5b7ffc5cb
6
+ metadata.gz: 29b391870a0332435dcfdd39aa7995483689e5bb1b1c3a54fe81b7a0ace8165295f953b2afd82bf973e152b63cfb9d396a4a9dedc99bcaeb12e1d93b0682fa07
7
+ data.tar.gz: b9855efff5f9d7de6e0ec73f5dde1f501cab9cba507d2a0d7914fc2920e66443553df7ade87b411e0fbb2fdcbb5141c49a4687e5a0a505d37d4e77ee2ff6844b
@@ -2,9 +2,9 @@ require 'active_support/concern'
2
2
  require "active_kit/sequence/sequenceable"
3
3
 
4
4
  module ActiveKit
5
+ module Base
5
6
  module Activekitable
6
7
  extend ActiveSupport::Concern
7
- include ActiveKit::Loader
8
8
  include ActiveKit::Sequence::Sequenceable
9
9
 
10
10
  included do
@@ -13,4 +13,5 @@ module ActiveKit
13
13
  class_methods do
14
14
  end
15
15
  end
16
+ end
16
17
  end
@@ -0,0 +1,17 @@
1
+ module ActiveKit
2
+ module Base
3
+ class Activekiter
4
+ def initialize(current_class:)
5
+ @current_class = current_class
6
+ end
7
+
8
+ def schedule
9
+ @schedule ||= ActiveKit::Schedule::Schedule.new(current_class: @current_class)
10
+ end
11
+
12
+ def sequence
13
+ @sequence ||= ActiveKit::Sequence::Sequence.new(current_class: @current_class)
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,29 @@
1
+ module ActiveKit
2
+ module Base
3
+ class Ensure
4
+ def self.middleware_for!(request:)
5
+ ActiveKit::Schedule::Middleware.run
6
+ end
7
+
8
+ def self.setup_for!(current_class:)
9
+ current_class.class_eval do
10
+ unless self.reflect_on_association :activekit_association
11
+ has_one :activekit_association, as: :record, dependent: :destroy, class_name: "ActiveKit::Attribute"
12
+
13
+ def activekit
14
+ @activekit ||= Relation.new(current_object: self)
15
+ end
16
+
17
+ def self.activekiter
18
+ @activekiter ||= Activekiter.new(current_class: self)
19
+ end
20
+ end
21
+ end
22
+ end
23
+
24
+ def self.has_one_association_for!(record:)
25
+ record.create_activekit_association unless record.activekit_association
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,26 @@
1
+ module ActiveKit
2
+ module Base
3
+ class Middleware
4
+ def initialize(app)
5
+ @app = app
6
+ end
7
+
8
+ # Middleware that determines which ActiveKit middlewares to run.
9
+ def call(env)
10
+ request = ActionDispatch::Request.new(env)
11
+
12
+ activekit_runner(request) do
13
+ @app.call(env)
14
+ end
15
+ end
16
+
17
+ private
18
+
19
+ def activekit_runner(request, &blk)
20
+ Ensure.middleware_for!(request: request)
21
+
22
+ yield
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,9 @@
1
+ module ActiveKit
2
+ module Base
3
+ class Relation
4
+ def initialize(current_object:)
5
+ @current_object = current_object
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,10 @@
1
+ module ActiveKit
2
+ module Base
3
+ extend ActiveSupport::Autoload
4
+
5
+ autoload :Activekiter
6
+ autoload :Ensure
7
+ autoload :Middleware
8
+ autoload :Relation
9
+ end
10
+ end
@@ -3,11 +3,15 @@ module ActiveKit
3
3
  isolate_namespace ActiveKit
4
4
  config.eager_load_namespaces << ActiveKit
5
5
 
6
+ initializer "active_kit.add_middleware" do |app|
7
+ app.middleware.use ActiveKit::Base::Middleware
8
+ end
9
+
6
10
  initializer "active_kit.activekitable" do
7
- require "active_kit/activekitable"
11
+ require "active_kit/base/activekitable"
8
12
 
9
13
  ActiveSupport.on_load(:active_record) do
10
- include ActiveKit::Activekitable
14
+ include ActiveKit::Base::Activekitable
11
15
  end
12
16
  end
13
17
  end
@@ -0,0 +1,12 @@
1
+ module ActiveKit
2
+ module Schedule
3
+ class Middleware
4
+ def self.run(request:)
5
+ logger.info "coming from middleware of activekit schedule"
6
+ # activekit_attribute = ActiveKit::Attribute.where(value: schedule: timestamp < DateTime.now).order(updated_at: :desc)
7
+ # json_where = 'value->"$.schedule.attributes.' + attribute_name.to_s + '" = "' + word_for_position + '"'
8
+ # record_at_position = ActiveKit::Attribute.where(record_type: record.class.name).where(json_where).first&.record
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,24 @@
1
+ module ActiveKit
2
+ module Schedule
3
+ class Schedule
4
+ attr_reader :defined_attributes
5
+
6
+ def initialize(current_class:)
7
+ @current_class = current_class
8
+
9
+ @defined_attributes = {}
10
+ end
11
+
12
+ def add(record:, attribute_name:, datetime:, method:)
13
+ ActiveKit::Base::Ensure.has_one_association_for!(record: record)
14
+
15
+ record.activekit_association.schedule[:attributes][attribute_name.to_sym] = nil
16
+ record.activekit_association.save!
17
+ end
18
+
19
+ def add_attribute(name:, options:)
20
+ @defined_attributes.store(name, options)
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,97 @@
1
+ require 'active_support/concern'
2
+
3
+ module ActiveKit
4
+ module Schedule
5
+ module Scheduleable
6
+ extend ActiveSupport::Concern
7
+
8
+ included do
9
+ end
10
+
11
+ class_methods do
12
+ # Usage Options
13
+ # schedule_attribute :name, :run_method, :begin_at, :end_at
14
+ # schedule_attribute :name, :run_method, :timestamp_method, updater: {}
15
+ # schedule_attribute :name, :run_method, :timestamp_method, updater: { on: {} }
16
+ # schedule_attribute :name, :run_method, :timestamp_method, updater: { via: :assoc, on: {} }
17
+ # schedule_attribute :name, :run_method, :timestamp_method, updater: { via: {}, on: {} }
18
+ # Note: :on and :via in :updater can accept nested associations.
19
+ def schedule_attribute(name, run_method, timestamp_method, interval, **options)
20
+ ActiveKit::Base::Ensure.setup_for!(current_class: self)
21
+
22
+ name = name.to_sym
23
+ options.store(:run_method, run_method&.to_sym)
24
+ options.store(:timestamp_method, datetime_method&.to_sym)
25
+ options.deep_symbolize_keys!
26
+
27
+ set_activekit_schedule_callbacks(name: name, options: options)
28
+ activekiter.schedule.add_attribute(name: name, options: options)
29
+ end
30
+
31
+ def set_activekit_schedule_callbacks(name:, options:)
32
+ run_method = options.dig(:run_method)
33
+ updater = options.dig(:updater) || {}
34
+
35
+ if updater.empty?
36
+ after_save do
37
+ self.class.activekiter.schedule.update(record: self, name: name, run_method: run_method)
38
+ logger.info "ActiveKit | Schedule - Scheduling from #{self.class.name}: Done."
39
+ end
40
+ else
41
+ raise ":updater should be a hash while setting sequence_attribute. " unless updater.is_a?(Hash)
42
+ raise ":on in :updater should be a hash while setting sequence_attribute. " if updater.key?(:on) && !updater[:on].is_a?(Hash)
43
+ raise "Cannot use :via without :on in :updater while setting sequence_attribute. " if updater.key?(:via) && !updater.key?(:on)
44
+
45
+ updater_via = updater.delete(:via)
46
+ updater_on = updater.delete(:on) || updater
47
+
48
+ base_klass = search_base_klass(self.name, updater_via)
49
+ klass = reflected_klass(base_klass, updater_on.keys.first)
50
+ klass.constantize.class_eval do
51
+ after_save :activekit_sequence_sequenceable_callback
52
+ after_destroy :activekit_sequence_sequenceable_callback
53
+
54
+ define_method :activekit_sequence_sequenceable_callback do
55
+ inverse_assoc = self.class.search_inverse_assoc(self, updater_on)
56
+ position = positioning_method ? self.public_send(positioning_method) : nil
57
+ if inverse_assoc.respond_to?(:each)
58
+ inverse_assoc.each { |instance| instance.class.activekiter.sequence.update(record: instance, attribute_name: attribute_name, position: position) }
59
+ else
60
+ inverse_assoc.class.activekiter.sequence.update(record: inverse_assoc, attribute_name: attribute_name, position: position)
61
+ end
62
+ logger.info "ActiveSequence - Sequencing from #{self.class.name}: Done."
63
+ end
64
+ private :activekit_sequence_sequenceable_callback
65
+ end
66
+ end
67
+ end
68
+
69
+ def search_base_klass(classname, updater_via)
70
+ if updater_via.blank?
71
+ classname
72
+ elsif updater_via.is_a? Symbol
73
+ reflected_klass(classname, updater_via)
74
+ elsif updater_via.is_a? Hash
75
+ klass = reflected_klass(classname, updater_via.keys.first)
76
+ updater_via.values.first.is_a?(Hash) ? search_base_klass(klass, updater_via.values.first) : reflected_klass(klass, updater_via.values.first)
77
+ end
78
+ end
79
+
80
+ def reflected_klass(classname, key)
81
+ klass = classname.constantize.reflect_on_all_associations.map { |assoc| [assoc.name, assoc.klass.name] }.to_h[key]
82
+ raise "Could not find reflected klass for classname '#{classname}' and key '#{key}' while setting sequence_attribute" unless klass
83
+ klass
84
+ end
85
+
86
+ def search_inverse_assoc(klass_object, updater_on)
87
+ if updater_on.values.first.is_a?(Hash)
88
+ klass_object = klass_object.public_send(updater_on.values.first.keys.first)
89
+ search_inverse_assoc(klass_object, updater_on.values.first)
90
+ else
91
+ klass_object.public_send(updater_on.values.first)
92
+ end
93
+ end
94
+ end
95
+ end
96
+ end
97
+ end
@@ -0,0 +1,8 @@
1
+ module ActiveKit
2
+ module Schedule
3
+ extend ActiveSupport::Autoload
4
+
5
+ autoload :Middleware
6
+ autoload :Schedule
7
+ end
8
+ end
@@ -11,7 +11,7 @@ module ActiveKit
11
11
  end
12
12
 
13
13
  def update(record:, attribute_name:, position:)
14
- ActiveKit::Loader.ensure_has_one_association_for!(record: record)
14
+ ActiveKit::Base::Ensure.has_one_association_for!(record: record)
15
15
 
16
16
  if position
17
17
  raise "position '#{position}' is not a valid unsigned integer value greater than 0." unless position.is_a?(Integer) && position > 0
@@ -18,7 +18,7 @@ module ActiveKit
18
18
  # sequence_attribute :name, :positioning_method, updater: { via: {}, on: {} }
19
19
  # Note: :on and :via in :updater can accept nested associations.
20
20
  def sequence_attribute(name, positioning_method = nil, **options)
21
- ActiveKit::Loader.ensure_setup_for!(current_class: self)
21
+ ActiveKit::Base::Ensure.setup_for!(current_class: self)
22
22
 
23
23
  name = name.to_sym
24
24
  options.store(:positioning_method, positioning_method&.to_sym)
@@ -1,3 +1,3 @@
1
1
  module ActiveKit
2
- VERSION = '0.2.2'
2
+ VERSION = '0.2.3.dev2'
3
3
  end
data/lib/active_kit.rb CHANGED
@@ -4,8 +4,7 @@ require "active_kit/engine"
4
4
  module ActiveKit
5
5
  extend ActiveSupport::Autoload
6
6
 
7
- autoload :Activekiter
8
- autoload :Loader
9
- autoload :Relation
7
+ autoload :Base
8
+ autoload :Schedule
10
9
  autoload :Sequence
11
10
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activekit
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.2.3.dev2
5
5
  platform: ruby
6
6
  authors:
7
7
  - plainsource
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-10-17 00:00:00.000000000 Z
11
+ date: 2023-10-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -52,11 +52,17 @@ files:
52
52
  - config/routes.rb
53
53
  - db/migrate/20231016050208_create_active_kit_attributes.rb
54
54
  - lib/active_kit.rb
55
- - lib/active_kit/activekitable.rb
56
- - lib/active_kit/activekiter.rb
55
+ - lib/active_kit/base.rb
56
+ - lib/active_kit/base/activekitable.rb
57
+ - lib/active_kit/base/activekiter.rb
58
+ - lib/active_kit/base/ensure.rb
59
+ - lib/active_kit/base/middleware.rb
60
+ - lib/active_kit/base/relation.rb
57
61
  - lib/active_kit/engine.rb
58
- - lib/active_kit/loader.rb
59
- - lib/active_kit/relation.rb
62
+ - lib/active_kit/schedule.rb
63
+ - lib/active_kit/schedule/middleware.rb
64
+ - lib/active_kit/schedule/schedule.rb
65
+ - lib/active_kit/schedule/scheduleable.rb
60
66
  - lib/active_kit/sequence.rb
61
67
  - lib/active_kit/sequence/sequence.rb
62
68
  - lib/active_kit/sequence/sequenceable.rb
@@ -80,9 +86,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
80
86
  version: '0'
81
87
  required_rubygems_version: !ruby/object:Gem::Requirement
82
88
  requirements:
83
- - - ">="
89
+ - - ">"
84
90
  - !ruby/object:Gem::Version
85
- version: '0'
91
+ version: 1.3.1
86
92
  requirements: []
87
93
  rubygems_version: 3.1.2
88
94
  signing_key:
@@ -1,11 +0,0 @@
1
- module ActiveKit
2
- class Activekiter
3
- def initialize(current_class:)
4
- @current_class = current_class
5
- end
6
-
7
- def sequence
8
- @sequence ||= ActiveKit::Sequence::Sequence.new(current_class: @current_class)
9
- end
10
- end
11
- end
@@ -1,23 +0,0 @@
1
- module ActiveKit
2
- module Loader
3
- def self.ensure_setup_for!(current_class:)
4
- current_class.class_eval do
5
- unless self.reflect_on_association :activekit_association
6
- has_one :activekit_association, as: :record, dependent: :destroy, class_name: "ActiveKit::Attribute"
7
-
8
- def activekit
9
- @activekit ||= Relation.new(current_object: self)
10
- end
11
-
12
- def self.activekiter
13
- @activekiter ||= Activekiter.new(current_class: self)
14
- end
15
- end
16
- end
17
- end
18
-
19
- def self.ensure_has_one_association_for!(record:)
20
- record.create_activekit_association unless record.activekit_association
21
- end
22
- end
23
- end
@@ -1,7 +0,0 @@
1
- module ActiveKit
2
- class Relation
3
- def initialize(current_object:)
4
- @current_object = current_object
5
- end
6
- end
7
- end