class DataFile

Attributes

fmt[RW]

Public Class Methods

new(dir, name, fmt: :json) click to toggle source
Calls superclass method
# File src/data.rb, line 12
def initialize dir, name, fmt: :json
  super File.join(dir, "data"), name,
        (fmt == :json ? "json" : (fmt == :jsonlines ? "jsonlines" : "yaml"))
  self.fmt = fmt
end

Public Instance Methods

output(*models, index: nil, select: nil) click to toggle source

@param [object] index - if included, and using :jsonlines, will call index on each item (as a hash) and prepend the line. @param [string] select - if given this will select out the type, eg agreements, and discard the rest

# File src/data.rb, line 20
def output *models, index: nil, select: nil

  map = models_to_data(models)


  file do |file|
    if fmt == :jsonlines
      select ? selection = [select] : selection = map.keys
      for type in selection
        for decl in map[type]
          if (index)
            file.print (JSON.generate(index.call(decl)))
            file.print("\n")
          end
          file.print(JSON.generate(decl))
          file.print("\n")
        end
      end
    elsif fmt == :json
      select ? map = map[select] : map
      file.print(JSON.generate(map))
    elsif fmt == :yaml
      select ? map = map[select] : map
      file.print(map.to_yaml)
    else
      raise "unknown data file format"
    end
  end
end