Class: Pliny::Commands::Generator::Schema

Inherits:
Base
  • Object
show all
Defined in:
lib/pliny/commands/generator/schema.rb

Instance Attribute Summary

Attributes inherited from Base

#name, #options, #stream

Instance Method Summary collapse

Methods inherited from Base

#display, #field_name, #plural_class_name, #pluralized_file_name, #render_template, #singular_class_name, #table_name, #write_file, #write_template

Constructor Details

#initializeSchema

Returns a new instance of Schema.



9
10
11
12
# File 'lib/pliny/commands/generator/schema.rb', line 9

def initialize(*)
  super
  @warned_legacy = false
end

Instance Method Details

#createObject



14
15
16
17
18
19
20
21
22
# File 'lib/pliny/commands/generator/schema.rb', line 14

def create
  warn_legacy if legacy?

  schema = schema_yaml_path(field_name)
  write_file(schema) do
    Prmd.init(name.singularize, yaml: true)
  end
  display "created schema file #{schema}"
end

#legacy?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/pliny/commands/generator/schema.rb', line 33

def legacy?
  File.exist?("./docs/schema.json") || File.directory?("./docs/schema/schemata")
end

#meta_pathObject



51
52
53
54
55
56
57
# File 'lib/pliny/commands/generator/schema.rb', line 51

def meta_path
  if legacy?
    "./docs/schema/meta.json"
  else
    "./schema/meta.json"
  end
end

#rebuildObject



24
25
26
27
28
29
30
31
# File 'lib/pliny/commands/generator/schema.rb', line 24

def rebuild
  warn_legacy if legacy?

  write_file(schema_json_path) do
    Prmd.combine(schemata_path, meta: meta_path)
  end
  display "rebuilt #{schema_json_path}"
end

#schema_baseObject



67
68
69
# File 'lib/pliny/commands/generator/schema.rb', line 67

def schema_base
  Pathname.new("./schema")
end

#schema_json_pathObject



43
44
45
46
47
48
49
# File 'lib/pliny/commands/generator/schema.rb', line 43

def schema_json_path
  if legacy?
    "./docs/schema.json"
  else
    "./schema/schema.json"
  end
end

#schema_yaml_path(field_name) ⇒ Object



71
72
73
# File 'lib/pliny/commands/generator/schema.rb', line 71

def schema_yaml_path(field_name)
  File.join(schemata_path, "#{field_name}.yaml")
end

#schemata_pathObject



59
60
61
62
63
64
65
# File 'lib/pliny/commands/generator/schema.rb', line 59

def schemata_path
  if legacy?
    "./docs/schema/schemata"
  else
    "./schema/schemata"
  end
end

#warn_legacyObject



37
38
39
40
41
# File 'lib/pliny/commands/generator/schema.rb', line 37

def warn_legacy
  return if @warned_legacy
  display "WARNING: Using legacy schema layout under docs/. To use new layout under schema/, run `mkdir -p schema && git mv docs/schema.json docs/schema/meta.* docs/schema/schemata schema` then check for remaining schema-related files under docs/."
  @warned_legacy = true
end