Class: FactoryBurgers::Models::Factory

Inherits:
Object
  • Object
show all
Defined in:
lib/factory_burgers/models/factory.rb

Overview

Represent a factory and associated data needed for UI TODO: support transient attributes (f.definition.attributes …?) TODO: support associations

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(factory) ⇒ Factory

Returns a new instance of Factory.



11
12
13
# File 'lib/factory_burgers/models/factory.rb', line 11

def initialize(factory)
  @factory = factory
end

Instance Attribute Details

#factoryObject (readonly)

Returns the value of attribute factory.



9
10
11
# File 'lib/factory_burgers/models/factory.rb', line 9

def factory
  @factory
end

Instance Method Details

#attributesObject



45
46
47
# File 'lib/factory_burgers/models/factory.rb', line 45

def attributes
  settable_columns.map { |col| Attribute.new(col) }
end

#class_nameObject



37
38
39
# File 'lib/factory_burgers/models/factory.rb', line 37

def class_name
  active_record? ? build_class.base_class.name : build_class.name
end

#nameObject



33
34
35
# File 'lib/factory_burgers/models/factory.rb', line 33

def name
  factory.name.to_s
end

#to_hObject



20
21
22
23
24
25
26
27
# File 'lib/factory_burgers/models/factory.rb', line 20

def to_h
  {
    name: name,
    class_name: class_name,
    traits: traits.map(&:to_h),
    attributes: attributes.map(&:to_h),
  }
end

#to_jsonObject



29
30
31
# File 'lib/factory_burgers/models/factory.rb', line 29

def to_json(...)
  to_h.to_json(...)
end

#traitsObject



41
42
43
# File 'lib/factory_burgers/models/factory.rb', line 41

def traits
  defined_traits.map { |trait| Trait.new(trait) }
end

#valid?Boolean

Returns:

  • (Boolean)


15
16
17
18
# File 'lib/factory_burgers/models/factory.rb', line 15

def valid?
  # Non-ActiveRecord factories are not supported; we don't know how to set their columns, persist records, etc.
  active_record?
end