Class: Suma::ExpressSchema

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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



11
12
13
14
15
16
# File 'lib/suma/express_schema.rb', line 11

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.



9
10
11
# File 'lib/suma/express_schema.rb', line 9

def id
  @id
end

#is_standalone_fileObject

Returns the value of attribute is_standalone_file.



9
10
11
# File 'lib/suma/express_schema.rb', line 9

def is_standalone_file
  @is_standalone_file
end

#output_pathObject

Returns the value of attribute output_path.



9
10
11
# File 'lib/suma/express_schema.rb', line 9

def output_path
  @output_path
end

#parsedObject

Returns the value of attribute parsed.



9
10
11
# File 'lib/suma/express_schema.rb', line 9

def parsed
  @parsed
end

#pathObject

Returns the value of attribute path.



9
10
11
# File 'lib/suma/express_schema.rb', line 9

def path
  @path
end

Instance Method Details

#build_output_filenameObject



51
52
53
54
55
56
57
58
59
60
# File 'lib/suma/express_schema.rb', line 51

def build_output_filename
  if @is_standalone_file
    # For standalone files, output directly to output_path
    File.join(@output_path, "#{@id}.exp")
  else
    # For manifest schemas, preserve directory structure
    # Note: @output_path already contains the category (resources/modules)
    File.join(@output_path, @id, File.basename(@path))
  end
end

#ensure_id_loadedObject



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

def ensure_id_loaded
  parsed unless @id
end

#filename_plainObject



42
43
44
45
# File 'lib/suma/express_schema.rb', line 42

def filename_plain
  ensure_id_loaded
  build_output_filename
end

#save_exp(with_annotations: false) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/suma/express_schema.rb', line 62

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}"

  # return if File.exist?(filename_plain)
  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



38
39
40
# File 'lib/suma/express_schema.rb', line 38

def to_plain
  parsed.to_s(no_remarks: true)
end

#typeObject



18
19
20
21
22
23
24
25
26
27
# File 'lib/suma/express_schema.rb', line 18

def type
  path_str = @path.to_s
  if path_str.include?("/resources/")
    "resources"
  elsif path_str.include?("/modules/")
    "modules"
  else
    "unknown_type"
  end
end