-
Notifications
You must be signed in to change notification settings - Fork 121
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Lots of progress refactoring observing.
- Added class called `ObserveeSet` to hold all the models observed by a given observer. - Created a DeferredModelLoader which is a sort of proxy a model classes (it whines unless you instantiate it with the stringy name of a model) that also checks what `Object.const_defined?` has to say about it, but it never actually tries to load the model unless explicitly asked. - Still need to set up inheritance and enablement and generally fix ObserverArray which hasn't been ported for the updated code yet.
- Loading branch information
Showing
13 changed files
with
224 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
require 'active_model' | ||
module ActiveModel | ||
class ObserveeSet # aka Classes Observed by an Observer | ||
|
||
attr_reader :observer | ||
attr_reader :observees | ||
attr_predicate :observees | ||
def initialize(observer) #:nodoc: | ||
@observer = observer | ||
@observees ||= Set.new | ||
end | ||
|
||
def add(*new_models) | ||
new_models = new_models.flatten.map do |model| | ||
Observing::DeferredModelLoader.new(model) | ||
end.compact | ||
observees.merge(new_models) | ||
end | ||
alias_method :<<, :add | ||
alias_method :merge, :add | ||
|
||
def replace(*models) | ||
observees.clear | ||
add(*models) | ||
end | ||
alias_method :observees=, :replace | ||
|
||
def all | ||
observees.to_a.freeze | ||
end | ||
|
||
def loaded | ||
all.select(&:model_defined?).freeze | ||
end | ||
|
||
def pending | ||
all.reject(&:model_defined?).freeze | ||
end | ||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
module ActiveModel | ||
module Observing | ||
class DeferredModelLoader | ||
include Comparable | ||
extend ActiveSupport::DescendantsTracker | ||
class << self | ||
def load_all | ||
descendants.reject(&:loaded?).each(&:load!) | ||
end | ||
|
||
def load_if(&block) | ||
descendants.reject(&:loaded?).select(&block).each(&:load!) | ||
end | ||
end | ||
|
||
attr_reader :model_name | ||
def initialize(model_name) | ||
if [ Class, Module ].any? { |mod| model_name.is_a?(mod) } | ||
warn ArgumentError, "Specifying models to observe as classes " + | ||
"(as opposed to the stringy names of classes) is highly deprecated and " + | ||
"very likely to result in circular dependencies. (From observation of #{model} " + | ||
"by #{observer}.)" | ||
model_name = model_name.name | ||
elsif model_name.is_a?(self.class) | ||
model_name = model_name.name | ||
end | ||
|
||
if [ String, Symbol ].none? { |mod| model_name.is_a?(mod) } | ||
__raise__ ArgumentError, "Argument #{model_name} must be a String or Symbol." | ||
elsif !model_name.respond_to?(:to_s) | ||
__raise__ ArgumentError, "Argument #{model_name} must respond to #to_s." | ||
end | ||
@model_name = model_name.to_s.camelize.freeze | ||
end | ||
|
||
def <=>(other) | ||
self.model_name <=> other.model_name | ||
end | ||
|
||
def model_defined? | ||
Object.const_defined?(model_name) | ||
end | ||
|
||
def model_assigned? | ||
defined?(@model) | ||
end | ||
|
||
def model(load_model = false) | ||
return @model if model_assigned? | ||
return nil unless load_model || model_defined? | ||
@model = Object.const_get(model_name) | ||
end | ||
|
||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
__FILE__.sub(/\.rb\z/, '').tap do |path| | ||
[ 'string', 'module' ].each do |file| | ||
require File.join(path, file) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
__FILE__.sub(/\.rb\z/, '').tap do |path| | ||
[ 'attr_predicate' ].each do |file| | ||
require File.join(path, file) | ||
end | ||
end |
Oops, something went wrong.