Class: Suma::ExpressSchema

Inherits:
Object
  • Object
show all
Defined in:
lib/suma/express_schema.rb

Defined Under Namespace

Modules: Type

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id:, path:, output_path:, is_standalone_file: false) ⇒ ExpressSchema

Returns a new instance of ExpressSchema.



47
48
49
50
51
52
# File 'lib/suma/express_schema.rb', line 47

def initialize(id:, path:, output_path:, is_standalone_file: false)
  @path = Pathname.new(path).expand_path
  @id = id
  @output_path = output_path
  @is_standalone_file = is_standalone_file
end

Instance Attribute Details

#idObject

Returns the value of attribute id.



45
46
47
# File 'lib/suma/express_schema.rb', line 45

def id
  @id
end

#is_standalone_fileObject

Returns the value of attribute is_standalone_file.



45
46
47
# File 'lib/suma/express_schema.rb', line 45

def is_standalone_file
  @is_standalone_file
end

#output_pathObject

Returns the value of attribute output_path.



45
46
47
# File 'lib/suma/express_schema.rb', line 45

def output_path
  @output_path
end

#parsedObject

Returns the value of attribute parsed.



45
46
47
# File 'lib/suma/express_schema.rb', line 45

def parsed
  @parsed
end

#pathObject

Returns the value of attribute path.



45
46
47
# File 'lib/suma/express_schema.rb', line 45

def path
  @path
end

Instance Method Details

#build_output_filenameObject



80
81
82
83
84
85
86
87
# File 'lib/suma/express_schema.rb', line 80

def build_output_filename
  if @is_standalone_file
    File.join(@output_path, "#{@id}.exp")
  else
    parent_dir = File.basename(File.dirname(@path))
    File.join(@output_path, parent_dir, File.basename(@path))
  end
end

#ensure_id_loadedObject



76
77
78
# File 'lib/suma/express_schema.rb', line 76

def ensure_id_loaded
  parsed unless @id
end

#filename_plainObject



71
72
73
74
# File 'lib/suma/express_schema.rb', line 71

def filename_plain
  ensure_id_loaded
  build_output_filename
end

#save_exp(with_annotations: false) ⇒ Object



89
90
91
92
93
94
95
96
97
98
# File 'lib/suma/express_schema.rb', line 89

def save_exp(with_annotations: false)
  relative_path = Pathname.new(filename_plain).relative_path_from(Dir.pwd)
  schema_type = with_annotations ? "annotated" : "plain"
  Utils.log "Save #{schema_type} schema: #{relative_path}"

  FileUtils.mkdir_p(File.dirname(filename_plain))

  content = with_annotations ? parsed.to_s(no_remarks: false) : to_plain
  File.write(filename_plain, content)
end

#to_plainObject



67
68
69
# File 'lib/suma/express_schema.rb', line 67

def to_plain
  parsed.to_s(no_remarks: true)
end

#typeObject



54
55
56
# File 'lib/suma/express_schema.rb', line 54

def type
  @type ||= classify
end