Class: ActionFactory::Base
- Inherits:
-
Object
- Object
- ActionFactory::Base
- Extended by:
- ActiveModel::Callbacks
- Defined in:
- lib/action_factory/base.rb
Constant Summary collapse
- ClassNotFound =
Class.new(StandardError)
Class Attribute Summary collapse
-
.creator ⇒ Object
readonly
Returns the value of attribute creator.
-
.initializer ⇒ Object
readonly
Returns the value of attribute initializer.
Instance Attribute Summary collapse
-
#attributes ⇒ Object
readonly
Returns the value of attribute attributes.
-
#traits ⇒ Object
readonly
Returns the value of attribute traits.
Class Method Summary collapse
- .assignments ⇒ Object
- .attribute(name, &block) ⇒ Object
- .class_name ⇒ Object
- .klass ⇒ Object
- .register(factory_name: nil, class_name: nil) ⇒ Object
- .sequence(name, &block) ⇒ Object
- .to_create(&block) ⇒ Object
- .to_initialize(&block) ⇒ Object
- .trait(name, &block) ⇒ Object
Instance Method Summary collapse
- #build ⇒ Object
- #create ⇒ Object
- #factory_attributes ⇒ Object
-
#initialize(*traits, **attributes) ⇒ Base
constructor
A new instance of Base.
- #instance ⇒ Object
- #run(strategy) ⇒ Object
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
.creator ⇒ Object (readonly)
Returns the value of attribute creator.
23 24 25 |
# File 'lib/action_factory/base.rb', line 23 def creator @creator end |
.initializer ⇒ Object (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
#attributes ⇒ Object (readonly)
Returns the value of attribute attributes.
78 79 80 |
# File 'lib/action_factory/base.rb', line 78 def attributes @attributes end |
#traits ⇒ Object (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
.assignments ⇒ Object
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_name ⇒ Object
31 32 33 |
# File 'lib/action_factory/base.rb', line 31 def class_name Registry.class_name_for(self.name) end |
.klass ⇒ Object
35 36 37 38 39 40 41 |
# File 'lib/action_factory/base.rb', line 35 def klass @klass ||= class_name.constantize rescue NameError = "Class with name #{class_name.inspect} not found, " \ "please use `register` if you need to configure a custom class name" raise ClassNotFound, end |
.register(factory_name: nil, class_name: nil) ⇒ Object
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
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
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
#build ⇒ Object
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 |
#create ⇒ Object
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_attributes ⇒ Object
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 |
#instance ⇒ Object
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 |