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.



7
8
9
10
# File 'lib/pliny/commands/generator/schema.rb', line 7

def initialize(*)
  super
  @warned_legacy = false
end

Instance Method Details

#createObject



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

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)


31
32
33
# File 'lib/pliny/commands/generator/schema.rb', line 31

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

#meta_pathObject



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

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

#rebuildObject



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

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



65
66
67
# File 'lib/pliny/commands/generator/schema.rb', line 65

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

#schema_json_pathObject



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

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

#schema_yaml_path(field_name) ⇒ Object



69
70
71
# File 'lib/pliny/commands/generator/schema.rb', line 69

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

#schemata_pathObject



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

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

#warn_legacyObject



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

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