class DataSrc

Public Class Methods

new(sources) click to toggle source

initialization means establishing a proper hash for the 'data' param

# File lib/liquidoc.rb, line 616
def initialize sources
  @datasrc = {}
  @datasrc['file'] = sources
  @datasrc['ext'] = ''
  @datasrc['pattern'] = nil
  if sources.is_a? Hash # data var is a hash, so add 'ext' to it by extracting it from filename
    @datasrc['file'] = sources['file']
    @datasrc['ext'] = File.extname(sources['file'])
    if (defined?(sources['pattern']))
      @datasrc['pattern'] = sources['pattern']
    end
    if (defined?(sources['type']))
      @datasrc['type'] = sources['type']
    end
  elsif sources.is_a? String
    @datasrc['ext'] = File.extname(sources)
  elsif sources.is_a? Array
    sources.each do |src|
      @datasrc['name'] = File.basename(@datasrc['file'])
    end
  else
    raise "InvalidDataSource"
  end
end

Public Instance Methods

ext() click to toggle source
# File lib/liquidoc.rb, line 645
def ext
  @datasrc['ext']
end
file() click to toggle source
# File lib/liquidoc.rb, line 641
def file
  @datasrc['file']
end
name() click to toggle source
# File lib/liquidoc.rb, line 649
def name
  File.basename(self.file,File.extname(self.file))
end
pattern() click to toggle source
# File lib/liquidoc.rb, line 674
def pattern
  @datasrc['pattern']
end
type() click to toggle source
# File lib/liquidoc.rb, line 653
def type
  if @datasrc['type'] # if we're carrying a 'type' setting for data, pass it along
    datatype = @datasrc['type']
    if datatype.downcase == "yaml" # This is an expected common error, so let's do the user a solid
      datatype = "yml"
    end
  else # If there's no 'type' defined, extract it from the filename and validate it
    unless @datasrc['ext'].downcase.match(/\.yml|\.json|\.xml|\.csv/)
      # @logger.error "Data file extension must be one of: .yml, .json, .xml, or .csv or else declared in config file."
      raise "FileExtensionUnknown"
    end
    datatype = self.ext
    datatype = datatype[1..-1] # removes leading dot char
  end
  unless datatype.downcase.match(/yml|json|xml|csv|regex/) # 'type' must be one of these permitted vals
    # @logger.error "Declared data type must be one of: yaml, json, xml, csv, or regex."
    raise "DataTypeUnrecognized"
  end
  datatype
end