class Soryo::FileInstance

Attributes

file_path[R]

Public Class Methods

new(file_path) click to toggle source
# File lib/classes/fileinstance.rb, line 10
def initialize(file_path)
    @file_path = Pathname.new(file_path)
end

Public Instance Methods

_symbolize(obj) click to toggle source
# File lib/classes/fileinstance.rb, line 49
def _symbolize(obj)
    return obj.inject({}){|memo,(k,v)| memo[k.to_sym] =  _symbolize(v); memo} if obj.is_a? Hash
    return obj.inject([]){|memo,v    | memo           << _symbolize(v); memo} if obj.is_a? Array
    return obj
end
existance?() click to toggle source
# File lib/classes/fileinstance.rb, line 25
def existance?
    File.exists?(file_path)
end
shortname() click to toggle source
# File lib/classes/fileinstance.rb, line 55
def shortname
    File.basename(@file_path, '.*')
end
to_hash() click to toggle source
# File lib/classes/fileinstance.rb, line 35
def to_hash
    if self.existance?
        if File.extname(@file_path) == '.json'
            json_hash = JSON.parse(self.to_s)
        elsif ['.yaml', '.yml'].include? File.extname(@file_path)
            yaml_hash = YAML.load(self.to_s)
        else
            raise 'Must be a JSON or YAML file'
        end
     else
         raise 'NoFileFound'
     end
end
to_s() click to toggle source
# File lib/classes/fileinstance.rb, line 14
def to_s
    if self.existance?
        file = File.open(@file_path, 'r')
        file_text = file.read
        file.close
        file_text
    else
        raise 'NoFileFound'
    end
end
write(file_contents) click to toggle source
# File lib/classes/fileinstance.rb, line 29
def write(file_contents)
    file = File.open(@file_path, 'w')
    file.write(file_contents)
    file.close
end