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.



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

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)


56
57
58
# File 'lib/conject/object_context.rb', line 56

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.


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

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)
    object.instance_variable_set(:@_conject_contextual_name, name.to_s)
    @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



84
85
86
# File 'lib/conject/object_context.rb', line 84

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

#has?(name) ⇒ Boolean

Returns:

  • (Boolean)


46
47
48
49
50
51
# File 'lib/conject/object_context.rb', line 46

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)


60
61
62
# File 'lib/conject/object_context.rb', line 60

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:



65
66
67
# File 'lib/conject/object_context.rb', line 65

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

#inspectObject Also known as: to_s



88
89
90
# File 'lib/conject/object_context.rb', line 88

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
17
# 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)
  object.instance_variable_set(:@_conject_contextual_name, name.to_s)
  @cache[name.to_sym] = object
end

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

Yields:

  • (_self)

Yield Parameters:



41
42
43
44
# File 'lib/conject/object_context.rb', line 41

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