Class: Activr::Registry
- Inherits:
-
Object
- Object
- Activr::Registry
- Defined in:
- lib/activr/registry.rb
Overview
The registry holds all activities, entities, timelines and timeline entries classes defined in the application
The registry singleton is accessible with registry
Instance Attribute Summary collapse
-
#activity_entities ⇒ Hash{Class=>Array<Symbol>}
readonly
Entity names for activity class.
-
#entity_classes ⇒ Hash{Symbol=>Class}
readonly
Model class associated to entity name.
Instance Method Summary collapse
-
#activities ⇒ Hash{String=>Class}
Get all registered activities.
-
#activity_entities_for_model(model_class) ⇒ Object
Get all entities names for given model class.
-
#add_entity(entity_name, entity_options, activity_klass) ⇒ Object
Register an entity.
-
#add_model(model_class) ⇒ Object
Register a model.
-
#class_for_activity(activity_kind) ⇒ Class
Get class for given activity.
-
#class_for_timeline(timeline_kind) ⇒ Class
Get class for given timeline kind.
-
#class_for_timeline_entry(timeline_kind, route_kind) ⇒ Class
Get class for timeline entry corresponding to given route in given timeline.
-
#classes_from_path(dir_path) ⇒ Hash{String=>Class}
private
Find all classes in given directory.
-
#entities ⇒ Hash{Symbol=>Array<Class>}
Get all registered entities.
-
#entities_names ⇒ Array<Symbol>
Get all registered entities names.
-
#initialize ⇒ Registry
constructor
Init.
-
#models ⇒ Array<Class>
Get all models that included mixin Entity::ModelMixin.
-
#setup ⇒ Object
Setup registry.
-
#timeline_entities_for_model(model_class) ⇒ Hash{Class=>Array<Symbol>}
Get all entities names by timelines that can have a reference to given model class.
-
#timeline_entries ⇒ Hash{String=>Hash{String=>Class}}
Get all registered timeline entries.
-
#timelines ⇒ Hash{String=>Class}
Get all registered timelines.
Constructor Details
#initialize ⇒ Registry
Init
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/activr/registry.rb', line 17 def initialize @setup = false @timelines = nil @timeline_entries = nil @activities = nil @entities = nil @models = nil @entity_classes = { } @activity_entities = { } @timeline_entities_for_model = { } @activity_entities_for_model = { } end |
Instance Attribute Details
#activity_entities ⇒ Hash{Class=>Array<Symbol>} (readonly)
Returns entity names for activity class.
14 15 16 |
# File 'lib/activr/registry.rb', line 14 def activity_entities @activity_entities end |
#entity_classes ⇒ Hash{Symbol=>Class} (readonly)
Returns model class associated to entity name.
11 12 13 |
# File 'lib/activr/registry.rb', line 11 def entity_classes @entity_classes end |
Instance Method Details
#activities ⇒ Hash{String=>Class}
Get all registered activities
128 129 130 |
# File 'lib/activr/registry.rb', line 128 def activities @activities ||= self.classes_from_path(Activr.activities_path) end |
#activity_entities_for_model(model_class) ⇒ Object
Get all entities names for given model class
209 210 211 212 213 214 215 216 217 218 219 |
# File 'lib/activr/registry.rb', line 209 def activity_entities_for_model(model_class) @activity_entities_for_model[model_class] ||= begin result = [ ] @entity_classes.each do |entity_name, entity_class| result << entity_name if (entity_class == model_class) end result end end |
#add_entity(entity_name, entity_options, activity_klass) ⇒ Object
Register an entity
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/activr/registry.rb', line 164 def add_entity(entity_name, , activity_klass) entity_name = entity_name.to_sym if @entity_classes[entity_name] && (@entity_classes[entity_name].name != [:class].name) # otherwise this would break timeline entries deletion mecanism raise "Entity name #{entity_name} already used with class #{@entity_classes[entity_name]}, can't redefine it with class #{entity_options[:class]}" end # class for entity @entity_classes[entity_name] = [:class] # entities for activity @activity_entities[activity_klass] ||= [ ] @activity_entities[activity_klass] << entity_name # entities @entities ||= { } @entities[entity_name] ||= { } if !@entities[entity_name][activity_klass].blank? raise "Entity name #{entity_name} already used for activity: #{activity_klass}" end @entities[entity_name][activity_klass] = end |
#add_model(model_class) ⇒ Object
Register a model
203 204 205 206 |
# File 'lib/activr/registry.rb', line 203 def add_model(model_class) @models ||= [ ] @models << model_class end |
#class_for_activity(activity_kind) ⇒ Class
Get class for given activity
136 137 138 139 140 |
# File 'lib/activr/registry.rb', line 136 def class_for_activity(activity_kind) result = self.activities[activity_kind] raise "No class defined for activity kind: #{activity_kind}" if result.blank? result end |
#class_for_timeline(timeline_kind) ⇒ Class
Get class for given timeline kind
61 62 63 64 65 |
# File 'lib/activr/registry.rb', line 61 def class_for_timeline(timeline_kind) result = self.timelines[timeline_kind] raise "No class defined for timeline kind: #{timeline_kind}" if result.blank? result end |
#class_for_timeline_entry(timeline_kind, route_kind) ⇒ Class
Get class for timeline entry corresponding to given route in given timeline
121 122 123 |
# File 'lib/activr/registry.rb', line 121 def class_for_timeline_entry(timeline_kind, route_kind) (self.timeline_entries[timeline_kind] && self.timeline_entries[timeline_kind][route_kind]) || Activr::Timeline::Entry end |
#classes_from_path(dir_path) ⇒ Hash{String=>Class}
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Find all classes in given directory
252 253 254 255 256 257 258 259 260 261 262 263 264 |
# File 'lib/activr/registry.rb', line 252 def classes_from_path(dir_path) Dir["#{dir_path}/*.rb"].sort.inject({ }) do |memo, file_path| klass = File.basename(file_path, '.rb').camelize.constantize if !memo[klass.kind].nil? raise "Kind #{klass.kind} already used by class #{memo[klass.kind]} so can't use it for class #{klass}" end memo[klass.kind] = klass memo end end |
#entities ⇒ Hash{Symbol=>Array<Class>}
Get all registered entities
145 146 147 148 149 150 |
# File 'lib/activr/registry.rb', line 145 def entities # loading activities triggers calls to #add_entity method self.activities if @entities.blank? @entities || { } end |
#entities_names ⇒ Array<Symbol>
Get all registered entities names
155 156 157 |
# File 'lib/activr/registry.rb', line 155 def entities_names @entities_names ||= self.entities.keys end |
#models ⇒ Array<Class>
Get all models that included mixin Entity::ModelMixin
193 194 195 196 197 198 |
# File 'lib/activr/registry.rb', line 193 def models # loading activities triggers models loading self.activities if @models.blank? @models || [ ] end |
#setup ⇒ Object
Setup registry
34 35 36 37 38 39 40 41 42 43 |
# File 'lib/activr/registry.rb', line 34 def setup return if @setup # eagger load all classes self.activities self.timelines self.timeline_entries @setup = true end |
#timeline_entities_for_model(model_class) ⇒ Hash{Class=>Array<Symbol>}
Get all entities names by timelines that can have a reference to given model class
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 |
# File 'lib/activr/registry.rb', line 225 def timeline_entities_for_model(model_class) @timeline_entities_for_model[model_class] ||= begin result = { } self.timelines.each do |timeline_kind, timeline_class| result[timeline_class] = [ ] timeline_class.routes.each do |route| entities_ary = @activity_entities[route.activity_class] (entities_ary || [ ]).each do |entity_name| result[timeline_class] << entity_name if (@entity_classes[entity_name] == model_class) end end result[timeline_class].uniq! end result end end |
#timeline_entries ⇒ Hash{String=>Hash{String=>Class}}
Get all registered timeline entries
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/activr/registry.rb', line 70 def timeline_entries @timeline_entries ||= begin result = { } self.timelines.each do |(timeline_kind, timeline_class)| dir_name = Activr::Utils.kind_for_class(timeline_class) dir_path = File.join(Activr.timelines_path, dir_name) if !File.directory?(dir_path) dir_name = Activr::Utils.kind_for_class(timeline_class, 'timeline') dir_path = File.join(Activr.timelines_path, dir_name) end if File.directory?(dir_path) result[timeline_kind] = { } Dir["#{dir_path}/*.rb"].sort.inject(result[timeline_kind]) do |memo, file_path| base_name = File.basename(file_path, '.rb') # skip base class if (base_name != "base_timeline_entry") klass = "#{timeline_class.name}::#{base_name.camelize}".constantize route_kind = if (match_data = base_name.match(/(.+)_timeline_entry$/)) match_data[1] else base_name end route = timeline_class.routes.find do |timeline_route| timeline_route.kind == route_kind end raise "Timeline entry class found for an unspecified timeline route: #{file_path} / routes: #{timeline_class.routes.inspect}" unless route memo[route_kind] = klass end memo end end end result end end |
#timelines ⇒ Hash{String=>Class}
Get all registered timelines
53 54 55 |
# File 'lib/activr/registry.rb', line 53 def timelines @timelines ||= self.classes_from_path(Activr.timelines_path) end |