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



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

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

#class_nameObject



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

def class_name
  build_class.base_class.name
end

#nameObject



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

def name
  factory.name.to_s
end

#to_hObject



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

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

#to_jsonObject



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

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

#traitsObject



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

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

#valid?Boolean

Returns:

  • (Boolean)


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

def valid?
  build_class.present?
end