activekit 0.2.3.dev1 → 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: fdfebffac9f65f7b8e393240aebefc53bb56d46a1eaff8070837735adf269e99
4
- data.tar.gz: 331f0bef2a0675a603bf462885b7bf0c47a5acca2efcc8a59db8150555183bdd
3
+ metadata.gz: 95241693c8cee6d5b29f2f8172abf1a5b31f70ea29a529595db9d5f71d24b948
4
+ data.tar.gz: e00f4986129a42c3444daee94921081738e763f9edd9de704567b0f9aab7eb3e
5
5
  SHA512:
6
- metadata.gz: f814ff88427a48347b5272a7cec69997fed42d5910a9fa708fdd45b7e19957ebd6df5b1d0a12d025bcd7bf65d99e630aa2ed879ac7b3e5c50ddbf043c7bcdf55
7
- data.tar.gz: b79dc6fa0fd3d62e7f03fe0e69426969d77380614e4c3b64a1fec5c21162ae7440a4fc23d2af922370474ad8dc4a6b38ed615c0dc2f3425349ef7138a426e03f
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
@@ -4,14 +4,14 @@ module ActiveKit
4
4
  config.eager_load_namespaces << ActiveKit
5
5
 
6
6
  initializer "active_kit.add_middleware" do |app|
7
- app.middleware.use ActiveKit::Middleware
7
+ app.middleware.use ActiveKit::Base::Middleware
8
8
  end
9
9
 
10
10
  initializer "active_kit.activekitable" do
11
- require "active_kit/activekitable"
11
+ require "active_kit/base/activekitable"
12
12
 
13
13
  ActiveSupport.on_load(:active_record) do
14
- include ActiveKit::Activekitable
14
+ include ActiveKit::Base::Activekitable
15
15
  end
16
16
  end
17
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.3.dev1'
2
+ VERSION = '0.2.3.dev2'
3
3
  end
data/lib/active_kit.rb CHANGED
@@ -4,9 +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 :Middleware
10
- autoload :Relation
7
+ autoload :Base
8
+ autoload :Schedule
11
9
  autoload :Sequence
12
10
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activekit
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.3.dev1
4
+ version: 0.2.3.dev2
5
5
  platform: ruby
6
6
  authors:
7
7
  - plainsource
@@ -52,12 +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/middleware.rb
60
- - 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
61
66
  - lib/active_kit/sequence.rb
62
67
  - lib/active_kit/sequence/sequence.rb
63
68
  - lib/active_kit/sequence/sequenceable.rb
@@ -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,26 +0,0 @@
1
- module ActiveKit
2
- module Loader
3
- def self.ensure_middleware_for!(request:)
4
- end
5
-
6
- def self.ensure_setup_for!(current_class:)
7
- current_class.class_eval do
8
- unless self.reflect_on_association :activekit_association
9
- has_one :activekit_association, as: :record, dependent: :destroy, class_name: "ActiveKit::Attribute"
10
-
11
- def activekit
12
- @activekit ||= Relation.new(current_object: self)
13
- end
14
-
15
- def self.activekiter
16
- @activekiter ||= Activekiter.new(current_class: self)
17
- end
18
- end
19
- end
20
- end
21
-
22
- def self.ensure_has_one_association_for!(record:)
23
- record.create_activekit_association unless record.activekit_association
24
- end
25
- end
26
- end
@@ -1,24 +0,0 @@
1
- module ActiveKit
2
- class Middleware
3
- def initialize(app)
4
- @app = app
5
- end
6
-
7
- # Middleware that determines which ActiveKit schedules to run.
8
- def call(env)
9
- request = ActionDispatch::Request.new(env)
10
-
11
- activekit_runner(request) do
12
- @app.call(env)
13
- end
14
- end
15
-
16
- private
17
-
18
- def activekit_runner(request, &blk)
19
- ActiveKit::Loader.ensure_middleware_for!(request: request)
20
-
21
- yield
22
- end
23
- end
24
- 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