Class: ActionFactory::Base

Inherits:
Object
  • Object
show all
Extended by:
ActiveModel::Callbacks
Defined in:
lib/action_factory/base.rb

Constant Summary collapse

ClassNotFound =
Class.new(StandardError)

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*traits, **attributes) ⇒ Base

Returns a new instance of Base.



80
81
82
83
84
85
# File 'lib/action_factory/base.rb', line 80

def initialize(*traits, **attributes)
  raise "cannot initialize base class" if self.class == ActionFactory::Base

  @traits = traits
  @attributes = attributes
end

Class Attribute Details

.creatorObject (readonly)

Returns the value of attribute creator.



23
24
25
# File 'lib/action_factory/base.rb', line 23

def creator
  @creator
end

.initializerObject (readonly)

Returns the value of attribute initializer.



23
24
25
# File 'lib/action_factory/base.rb', line 23

def initializer
  @initializer
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



78
79
80
# File 'lib/action_factory/base.rb', line 78

def attributes
  @attributes
end

#traitsObject (readonly)

Returns the value of attribute traits.



78
79
80
# File 'lib/action_factory/base.rb', line 78

def traits
  @traits
end

Class Method Details

.assignmentsObject



65
66
67
68
69
70
71
72
73
# File 'lib/action_factory/base.rb', line 65

def assignments
  @assignments ||= begin
    if self.superclass.respond_to?(:assignments)
      self.superclass.assignments.deep_dup
    else
      ActiveSupport::HashWithIndifferentAccess.new { |hash, key| hash[key] = {}.with_indifferent_access }
    end
  end
end

.attribute(name, &block) ⇒ Object



53
54
55
# File 'lib/action_factory/base.rb', line 53

def attribute(name, &block)
  assignments[:attributes][name] = ActionFactory::Assignments::Attribute.new(block)
end

.class_nameObject



31
32
33
# File 'lib/action_factory/base.rb', line 31

def class_name
  Registry.class_name_for(self.name)
end

.klassObject



35
36
37
38
39
40
41
# File 'lib/action_factory/base.rb', line 35

def klass
  @klass ||= class_name.constantize
rescue NameError
  message = "Class with name #{class_name.inspect} not found, " \
            "please use `register` if you need to configure a custom class name"
  raise ClassNotFound, message
end

.register(factory_name: nil, class_name: nil) ⇒ Object

Raises:

  • (ArgumentError)


25
26
27
28
29
# File 'lib/action_factory/base.rb', line 25

def register(factory_name: nil, class_name: nil)
  raise ArgumentError, 'factory_name or class_name required' unless factory_name || class_name

  Registry.register(factory_class_name: self.name, factory_name: factory_name, class_name: class_name)
end

.sequence(name, &block) ⇒ Object



57
58
59
# File 'lib/action_factory/base.rb', line 57

def sequence(name, &block)
  assignments[:attributes][name] = ActionFactory::Assignments::Sequence.new(block)
end

.to_create(&block) ⇒ Object

Raises:

  • (ArgumentError)


48
49
50
51
# File 'lib/action_factory/base.rb', line 48

def to_create(&block)
  raise ArgumentError, 'Block required' unless block_given?
  @creator = block
end

.to_initialize(&block) ⇒ Object

Raises:

  • (ArgumentError)


43
44
45
46
# File 'lib/action_factory/base.rb', line 43

def to_initialize(&block)
  raise ArgumentError, 'Block required' unless block_given?
  @initializer = block
end

.trait(name, &block) ⇒ Object



61
62
63
# File 'lib/action_factory/base.rb', line 61

def trait(name, &block)
  assignments[:traits][name] = ActionFactory::Assignments::Trait.new(block)
end

Instance Method Details

#buildObject



92
93
94
95
96
97
98
# File 'lib/action_factory/base.rb', line 92

def build
  instance
  run_callbacks :assign_attributes do
    assign_attributes
  end
  instance
end

#createObject



100
101
102
103
104
105
106
# File 'lib/action_factory/base.rb', line 100

def create
  build
  run_callbacks :create do
    persist_instance
  end
  instance
end

#factory_attributesObject



108
109
110
# File 'lib/action_factory/base.rb', line 108

def factory_attributes
  @factory_attributes ||= assignment_compiler.compile(assignments[:attributes], except: @attributes.keys)
end

#instanceObject



112
113
114
# File 'lib/action_factory/base.rb', line 112

def instance
  @instance ||= build_instance_with_callbacks
end

#run(strategy) ⇒ Object



87
88
89
90
# File 'lib/action_factory/base.rb', line 87

def run(strategy)
  @strategy = strategy
  public_send(@strategy)
end