Class: Oaken::Convert::Fixture

Inherits:
Object
  • Object
show all
Defined in:
lib/generators/oaken/convert/fixtures_generator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model_name, name, attributes) ⇒ Fixture

Returns a new instance of Fixture.



10
11
12
13
14
# File 'lib/generators/oaken/convert/fixtures_generator.rb', line 10

def initialize(model_name, name, attributes)
  @model_name, @name, @attributes = model_name.tr("/", "_"), name, attributes
  @plural = @model_name
  @singular = @model_name.singularize
end

Instance Attribute Details

#model_nameObject (readonly)

Returns the value of attribute model_name.



8
9
10
# File 'lib/generators/oaken/convert/fixtures_generator.rb', line 8

def model_name
  @model_name
end

#nameObject (readonly)

Returns the value of attribute name.



8
9
10
# File 'lib/generators/oaken/convert/fixtures_generator.rb', line 8

def name
  @name
end

Instance Method Details

#extract_dependents(fixtures) ⇒ Object



16
17
18
19
20
21
# File 'lib/generators/oaken/convert/fixtures_generator.rb', line 16

def extract_dependents(fixtures)
  @dependents = fixtures.select { _1.reference(plural, singular) == name }
  fixtures.replace fixtures - dependents

  dependents.each { _1.extract_dependents fixtures }
end

#reference(plural, singular) ⇒ Object



23
24
25
26
27
# File 'lib/generators/oaken/convert/fixtures_generator.rb', line 23

def reference(plural, singular)
  @referenced = [plural, :plural]     if attributes[plural]
  @referenced = [singular, :singular] if attributes[singular]
  attributes[@referenced&.first]
end

#render(delimiter: "\n") ⇒ Object



29
30
31
# File 'lib/generators/oaken/convert/fixtures_generator.rb', line 29

def render(delimiter: "\n")
  [render_self, dependents&.map { _1.render delimiter: nil }].join(delimiter)
end