Class: EPO::Observer
- Inherits:
-
Welo::Observer
- Object
- Welo::Observer
- EPO::Observer
- Defined in:
- lib/epo/core/observer.rb
Overview
An Observer is a object able to look at the filesystem and observe resources in it.
Defined Under Namespace
Classes: Source
Instance Attribute Summary collapse
-
#db ⇒ Object
The DB able to tell if a path is understandable or not.
-
#models ⇒ Object
readonly
The list of models understood by this observer.
-
#structures ⇒ Object
A cache for the observation structures.
Class Method Summary collapse
-
.for_db(db) ⇒ Object
Creates and return a new observer from a DB.
Instance Method Summary collapse
-
#get_node_for_path(path, root = nil) ⇒ Object
Returns the node for a db path, or nil if doesnt exist.
-
#initialize(models = []) ⇒ Observer
constructor
Creates a new observer for given models.
-
#read_path_as_resource(path, model) ⇒ Object
Reads the file to path as a resource dumped EPO-style.
-
#read_tree(root) ⇒ Object
Recursively reads the files in the filesystem (with Find).
-
#structure(model, persp) ⇒ Object
Handy-cache for mapping (model, persp) pairs to Welo::ObservationStruct.
Constructor Details
#initialize(models = []) ⇒ Observer
Creates a new observer for given models. Will instanciate a new DB.
40 41 42 43 44 45 46 47 |
# File 'lib/epo/core/observer.rb', line 40 def initialize(models=[]) super() @db = DB.new(models) @structures = {} register(:observation) do |o| event(o.class.resource, o) end end |
Instance Attribute Details
#db ⇒ Object
The DB able to tell if a path is understandable or not
25 26 27 |
# File 'lib/epo/core/observer.rb', line 25 def db @db end |
#models ⇒ Object (readonly)
The list of models understood by this observer
22 23 24 |
# File 'lib/epo/core/observer.rb', line 22 def models @models end |
#structures ⇒ Object
A cache for the observation structures
28 29 30 |
# File 'lib/epo/core/observer.rb', line 28 def structures @structures end |
Class Method Details
.for_db(db) ⇒ Object
Creates and return a new observer from a DB. It will take the models from the DB.
32 33 34 35 36 |
# File 'lib/epo/core/observer.rb', line 32 def self.for_db(db) obj = self.new(db.models) obj.db = db obj end |
Instance Method Details
#get_node_for_path(path, root = nil) ⇒ Object
Returns the node for a db path, or nil if doesnt exist
50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/epo/core/observer.rb', line 50 def get_node_for_path(path, root=nil) case root when Regexp path = path.sub(root,'') when String re = %r{^#{root}} path = path.sub(re,'') when nil #ok else raise ArgumentError, "can only understand string or regexps as roots" end db.get_route_silent(path) end |
#read_path_as_resource(path, model) ⇒ Object
Reads the file to path as a resource dumped EPO-style
66 67 68 69 70 |
# File 'lib/epo/core/observer.rb', line 66 def read_path_as_resource(path, model) persp_str = db.persp_and_ext_for_basename(path).first persp = model.perspectives.keys.find{|k| k.to_s == persp_str} observe_source(Source.new(db, path), structure(model, persp)) end |
#read_tree(root) ⇒ Object
Recursively reads the files in the filesystem (with Find). For each path, will try to read_path Currently, there is no pruning, or control possible.
81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/epo/core/observer.rb', line 81 def read_tree(root) Find.find(root) do |path| node = get_node_for_path(path, root) if node if node.content #a model attached to a file in a directory if db.understands_filename?(path) read_path_as_resource(path, node.content) end end else #the db doesn't understand this branch, we'd rather drop now Find.prune end end end |
#structure(model, persp) ⇒ Object
Handy-cache for mapping (model, persp) pairs to Welo::ObservationStruct
73 74 75 76 |
# File 'lib/epo/core/observer.rb', line 73 def structure(model, persp) pair = [model, persp] @structures[pair] ||= Welo::ObservationStruct.new_for_resource_in_perspective(model, persp) end |