Class: BlueprintsBoy::Blueprint

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

Defined Under Namespace

Classes: Data

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context, name, attrs = {}, &block) ⇒ Blueprint

Returns a new instance of Blueprint.



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

def initialize(context, name, attrs = {}, &block)
  @context = context
  @name = name.to_sym
  @strategies = {
      create: block,
      attributes: proc { |data| data.attributes }
  }
  attributes(attrs)
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



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

def context
  @context
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

Instance Method Details

#attributes(attributes) ⇒ Object



30
31
32
# File 'lib/blueprints_boy/blueprint.rb', line 30

def attributes(attributes)
  update_context nil, attributes, nil
end

#blueprint(strategy, &block) ⇒ Object



38
39
40
# File 'lib/blueprints_boy/blueprint.rb', line 38

def blueprint(strategy, &block)
  @strategies[strategy] = block
end

#build(environment, strategy, options = {}) ⇒ Object



15
16
17
18
19
20
21
22
23
24
# File 'lib/blueprints_boy/blueprint.rb', line 15

def build(environment, strategy, options = {})
  data = Data.new(@name, options, normalized_attributes(environment).merge(options), @context.factory_class)
  block = @strategies[strategy]
  block ||= BlueprintsBoy.factories[@context.factory_class, strategy] if @context.factory_class
  if block
    environment.autoset(@name, environment.instance_exec(data, &block))
  else
    raise BlueprintsBoy::StrategyNotFound, "Blueprint #{@name.inspect} does not define strategy #{strategy.inspect}"
  end
end

#depends_on(*dependencies) ⇒ Object



26
27
28
# File 'lib/blueprints_boy/blueprint.rb', line 26

def depends_on(*dependencies)
  update_context dependencies, nil, nil
end

#factory(factory_class) ⇒ Object



34
35
36
# File 'lib/blueprints_boy/blueprint.rb', line 34

def factory(factory_class)
  update_context nil, nil, factory_class
end