Class: Conject::ObjectContext

Inherits:
Object
  • Object
show all
Defined in:
lib/conject/object_context.rb

Instance Method Summary collapse

Constructor Details

#initializeObjectContext

Returns a new instance of ObjectContext.



6
7
8
9
# File 'lib/conject/object_context.rb', line 6

def initialize
  @cache = { :this_object_context => self }
  @object_configs = Hash.new do |h,k| h[k] = {} end
end

Instance Method Details

#configure_objects(confs = {}) ⇒ Object

Allow configuration options to be set for named objects.



71
72
73
74
75
76
77
# File 'lib/conject/object_context.rb', line 71

def configure_objects(confs={})
  confs.each do |key,opts|
    key = key.to_sym
    @object_configs[key] ={} unless has_config?(key)
    @object_configs[key].merge!(opts)
  end
end

#directly_has?(name) ⇒ Boolean

Indicates if this context has the requested object in its own personal cache. (Does not consult any parent contexts.)

Returns:

  • (Boolean)


54
55
56
# File 'lib/conject/object_context.rb', line 54

def directly_has?(name)
  @cache.keys.include?(name.to_sym) or has_config?(name.to_sym)
end

#get(name) ⇒ Object Also known as: []

Retrieve a named object from this context.

If the object is already existant in this context, return it.
If we have a parent context and it contains the requested object, get and return object from parent context. (Recursive upward search)
If the object exists nowhere in this or a super context: construct, cache and return a new instance of the requested object using the object factory.


24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/conject/object_context.rb', line 24

def get(name)
  name = name.to_sym
  return @cache[name] if @cache.keys.include?(name)
  
  if !has_config?(name) and parent_context and parent_context.has?(name)
    return parent_context.get(name)
  else
    object = object_factory.construct_new(name,self)
    @cache[name] = object unless no_cache?(name)
    return object
  end
end

#get_object_config(name) ⇒ Object

Get the object configuration options for the given name



82
83
84
# File 'lib/conject/object_context.rb', line 82

def get_object_config(name)
  @object_configs[name.to_sym] || {}
end

#has?(name) ⇒ Boolean

Returns:

  • (Boolean)


44
45
46
47
48
49
# File 'lib/conject/object_context.rb', line 44

def has?(name)
  walk_up_contexts do |context|
    return true if context.directly_has?(name)
  end
  return false
end

#has_config?(name) ⇒ Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/conject/object_context.rb', line 58

def has_config?(name)
  @object_configs.keys.include?(name.to_sym)
end

#in_subcontext {|Conject.create_object_context(self)| ... } ⇒ Object

Create and yield a new ObjectContext with this ObjectContext as its parent

Yields:



63
64
65
# File 'lib/conject/object_context.rb', line 63

def in_subcontext
  yield Conject.create_object_context(self) if block_given?
end

#inspectObject Also known as: to_s



86
87
88
# File 'lib/conject/object_context.rb', line 86

def inspect
  "<ObjectContext 0x#{object_id.to_s(16)}>"
end

#put(name, object) ⇒ Object Also known as: []=

Inject a named object into this context



12
13
14
15
16
# File 'lib/conject/object_context.rb', line 12

def put(name, object)
  raise "This ObjectContext already has an instance or configuration for '#{name.to_s}'" if directly_has?(name)
  Conject.install_object_context(object, self)
  @cache[name.to_sym] = object
end

#walk_up_contexts {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



39
40
41
42
# File 'lib/conject/object_context.rb', line 39

def walk_up_contexts(&block)
  yield self
  parent_context.walk_up_contexts(&block) unless parent_context.nil?
end