Class: PlutoniumGenerators::Concerns::ConfiguresSqlite::DatabaseYAML

Inherits:
Object
  • Object
show all
Defined in:
lib/generators/pu/lib/plutonium_generators/concerns/configures_sqlite.rb

Constant Summary collapse

COMMENTED_PROD_DATABASE =
"# database: path/to/persistent/storage/production.sqlite3"
UNCOMMENTED_PROD_DATABASE =
"database: path/to/persistent/storage/production.sqlite3"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path:) ⇒ DatabaseYAML

Returns a new instance of DatabaseYAML.



14
15
16
17
18
19
20
21
22
23
# File 'lib/generators/pu/lib/plutonium_generators/concerns/configures_sqlite.rb', line 14

def initialize(path:)
  @content = File.read(path)
  # if the production environment has the default commented database value,
  # uncomment it so that the value can be parsed
  @content.gsub!(COMMENTED_PROD_DATABASE, UNCOMMENTED_PROD_DATABASE)
  @stream = Psych.parse_stream(@content)
  @emission_stream = Psych::Nodes::Stream.new
  @emission_document = Psych::Nodes::Document.new
  @emission_mapping = Psych::Nodes::Mapping.new
end

Instance Attribute Details

#contentObject (readonly)

Returns the value of attribute content.



12
13
14
# File 'lib/generators/pu/lib/plutonium_generators/concerns/configures_sqlite.rb', line 12

def content
  @content
end

Instance Method Details

#add_database(name) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/generators/pu/lib/plutonium_generators/concerns/configures_sqlite.rb', line 25

def add_database(name)
  root = @stream.children.first.root
  root.children.each_slice(2).map do |scalar, mapping|
    next unless scalar.is_a?(Psych::Nodes::Scalar)
    next unless mapping.is_a?(Psych::Nodes::Mapping)
    next unless mapping.anchor.nil? || mapping.anchor.empty?
    next if mapping.children.each_slice(2).any? do |key, value|
      key.is_a?(Psych::Nodes::Scalar) && key.value == name && value.is_a?(Psych::Nodes::Alias) && value.anchor == name
    end

    new_mapping = Psych::Nodes::Mapping.new
    if mapping.children.first.value == "<<" # 2-tiered environment
      new_mapping.children.concat [
        Psych::Nodes::Scalar.new("primary"),
        mapping,
        Psych::Nodes::Scalar.new(name),
        Psych::Nodes::Alias.new(name)
      ]
    else # 3-tiered environment
      new_mapping.children.concat mapping.children
      new_mapping.children.concat [
        Psych::Nodes::Scalar.new(name),
        Psych::Nodes::Alias.new(name)
      ]
    end

    old_environment_entry = emit_pair(scalar, mapping)
    new_environment_entry = emit_pair(scalar, new_mapping)

    [scalar.value, old_environment_entry, new_environment_entry]
  end.compact!
end

#database_def_regex(name) ⇒ Object



72
73
74
# File 'lib/generators/pu/lib/plutonium_generators/concerns/configures_sqlite.rb', line 72

def database_def_regex(name)
  /#{name}: &#{name}\n(?:[ \t]+.*\n)+/
end

#new_database(name, migrations_paths: nil) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/generators/pu/lib/plutonium_generators/concerns/configures_sqlite.rb', line 58

def new_database(name, migrations_paths: nil)
  migrations_paths ||= "db/#{name}_migrate"
  db = Psych::Nodes::Mapping.new(name)
  db.children.concat [
    Psych::Nodes::Scalar.new("<<"),
    Psych::Nodes::Alias.new("default"),
    Psych::Nodes::Scalar.new("migrations_paths"),
    Psych::Nodes::Scalar.new(migrations_paths),
    Psych::Nodes::Scalar.new("database"),
    Psych::Nodes::Scalar.new("storage/<%= Rails.env %>-#{name}.sqlite3")
  ]
  "\n" + emit_pair(Psych::Nodes::Scalar.new(name), db)
end