Class: BlueprintsBoy::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/blueprints_boy/context.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_name, &added_callback) ⇒ Context

Returns a new instance of Context.



5
6
7
8
9
10
11
12
# File 'lib/blueprints_boy/context.rb', line 5

def initialize(file_name, &added_callback)
  @file_name = file_name
  @added_callback = added_callback
  @dependencies = []
  @attrs = {}
  @factory_class = nil
  instance_eval(File.read(file_name), file_name)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missingObject



51
52
53
# File 'lib/blueprints_boy/context.rb', line 51

def dependency(name, *args, &block)
  BlueprintsBoy::Dependency.new(name, *args, &block)
end

Instance Attribute Details

#attrsObject

Returns the value of attribute attrs.



3
4
5
# File 'lib/blueprints_boy/context.rb', line 3

def attrs
  @attrs
end

#dependenciesObject

Returns the value of attribute dependencies.



3
4
5
# File 'lib/blueprints_boy/context.rb', line 3

def dependencies
  @dependencies
end

#factory_classObject

Returns the value of attribute factory_class.



3
4
5
# File 'lib/blueprints_boy/context.rb', line 3

def factory_class
  @factory_class
end

Instance Method Details

#attributes(attributes, &block) ⇒ Object



18
19
20
# File 'lib/blueprints_boy/context.rb', line 18

def attributes(attributes, &block)
  chain(nil, attributes, nil, &block)
end

#blueprint(*args, &block) ⇒ Object



35
36
37
38
39
# File 'lib/blueprints_boy/context.rb', line 35

def blueprint(*args, &block)
  Blueprint.new(self, *args, &block).tap do |blueprint|
    @added_callback.call blueprint if @added_callback
  end
end

#chain(dependencies, attributes, factory, &block) ⇒ Object



26
27
28
29
30
31
32
33
# File 'lib/blueprints_boy/context.rb', line 26

def chain(dependencies, attributes, factory, &block)
  dup.tap do |context|
    context.dependencies |= dependencies if dependencies
    context.attrs = context.attrs.merge(attributes) if attributes
    context.factory_class = factory if factory
    context.instance_eval(&block) if block
  end
end

#dependency(name, *args, &block) ⇒ Object Also known as: method_missing



47
48
49
# File 'lib/blueprints_boy/context.rb', line 47

def dependency(name, *args, &block)
  BlueprintsBoy::Dependency.new(name, *args, &block)
end

#depends_on(*dependencies, &block) ⇒ Object



14
15
16
# File 'lib/blueprints_boy/context.rb', line 14

def depends_on(*dependencies, &block)
  chain(dependencies, nil, nil, &block)
end

#factory(factory_class, &block) ⇒ Object



22
23
24
# File 'lib/blueprints_boy/context.rb', line 22

def factory(factory_class, &block)
  chain(nil, nil, factory_class, &block)
end

#group(groups) ⇒ Object



41
42
43
44
45
# File 'lib/blueprints_boy/context.rb', line 41

def group(groups)
  groups.collect do |name, children|
    blueprint(name) { build(*children) }
  end
end