Module: JetSet::Environment

Included in:
JetSet
Defined in:
lib/jet_set/environment.rb

Instance Method Summary collapse

Instance Method Details

#init(mapping, container = Hypo::Container.new) ⇒ Object

Initializes JetSet environment. Parameters:

mapping

JetSet mapping definition. Instance of JetSet::Mapping class.

container

(optional) Existing Hypo::Container instance.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/jet_set/environment.rb', line 17

def init(mapping, container = Hypo::Container.new)
  @container = container

  @container.register_instance(mapping, :mapping)

  @container.register(JetSet::EntityBuilder, :entity_builder)
    .using_lifetime(:singleton)

  @container.register(JetSet::Mapper, :mapper)
    .using_lifetime(:singleton)

  @container.register(JetSet::QueryParser, :query_parser)
    .using_lifetime(:singleton)
end

#map(&block) ⇒ Object

Accepts Ruby block with mapping definition. Parameters:

+&block+: Ruby block

Usage:

JetSet::Mapping.new do
  entity Invoice do
    field :amount
    field :created_at
    collection :line_items
    reference :subscription, type: Subscription
  end
  ...
  entity User do
    field :amount
    field :created_at
    collection :line_items
    reference :subscription, type: Subscription
  end
end


83
84
85
86
87
88
89
# File 'lib/jet_set/environment.rb', line 83

def map(&block)
  unless block_given?
    raise MapperError, 'Mapping should be defined as Ruby block.'
  end

  Mapping.new(&block)
end

#open_session(sequel, scope = nil) ⇒ Object

Creates JetSet session and registers it in Hypo container. Parameters:

sequel

Sequel connection.

scope

a name of registered component which manages the session lifetime.

Returns the session object.



56
57
58
59
60
61
# File 'lib/jet_set/environment.rb', line 56

def open_session(sequel, scope = nil)
  @container.register_instance(sequel, :sequel)

  register_session(scope)
  @container.resolve(:jet_set)
end

#register_session(scope = nil) ⇒ Object

Creates JetSet session and registers it in Hypo container. Parameters:

scope

a name of registered component which manages the session lifetime.



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/jet_set/environment.rb', line 35

def register_session(scope = nil)
  session_component = @container.register(JetSet::Session, :jet_set)
  dependency_graph_component = @container.register(JetSet::DependencyGraph, :dependency_graph)

  if scope.nil?
    @container.register_instance(nil, :session_scope)
    session_component.use_lifetime(:transient)
    dependency_graph_component.use_lifetime(:transient)
  else
    @container.register_instance(scope, :session_scope)
    session_component.use_lifetime(:scope).bind_to(scope)
    dependency_graph_component.use_lifetime(:scope).bind_to(scope)
  end

end