Skip to content

Commit

Permalink
Merge pull request #79 from smileart/master
Browse files Browse the repository at this point in the history
Fixed file path detection bug in build method polymorphism
  • Loading branch information
louismullie committed May 20, 2014
2 parents c813e72 + f8e5f83 commit eb0ec83
Showing 1 changed file with 22 additions and 20 deletions.
42 changes: 22 additions & 20 deletions lib/treat/entities/entity/buildable.rb
Expand Up @@ -17,14 +17,17 @@ module Treat::Entities::Entity::Buildable
EmailRegexp = /.+\@.+\..+/
Enclitics = %w['ll 'm 're 's 't 've 'nt]

# Accepted formats of serialized files
AcceptedFormats = ['.xml', '.yml', '.yaml', '.mongo']

# Reserved folder names
Reserved = ['.index']

# Build an entity from anything (can be
# a string, numeric,folder, or file name
# representing a raw or serialized file).
def build(*args)

# This probably needs some doc.
if args.size == 0
file_or_value = ''
Expand All @@ -46,9 +49,7 @@ def build(*args)
from_array(file_or_value)
elsif file_or_value.is_a?(Hash)
from_db(file_or_value)
elsif self == Treat::Entities::Document ||
(fv.index('.yml') || fv.index('.yaml') ||
fv.index('.xml'))
elsif self == Treat::Entities::Document || (is_serialized_file?(fv))
if fv =~ UriRegexp
from_url(fv)
else
Expand Down Expand Up @@ -94,7 +95,7 @@ def from_string(string, enforce_type = false)
end
e
end

# Build a document from an array
# of builders.
def from_array(array)
Expand All @@ -118,7 +119,7 @@ def from_url(url)
begin
folder = Treat.paths.files
if folder[-1] == '/'
folder = folder[0..-2]
folder = folder[0..-2]
end
f = Schiphol.download(url,
download_folder: folder,
Expand Down Expand Up @@ -178,15 +179,15 @@ def from_folder(folder)

c = Treat::Entities::Collection.new(folder)
folder += '/' unless folder[-1] == '/'

if !FileTest.directory?(folder)
FileUtils.mkdir(folder)
end

c.set :folder, folder
i = folder + '/.index'
c.set :index, i if FileTest.directory?(i)

Dir[folder + '*'].each do |f|
if FileTest.directory?(f)
c2 = Treat::Entities::Collection.
Expand All @@ -197,17 +198,15 @@ def from_folder(folder)
from_file(f), false)
end
end

return c

end

# Build a document from a raw or serialized file.
def from_file(file,def_fmt=nil)

if file.index('.yml') ||
file.index('.yaml') ||
file.index('.xml')
if is_serialized_file?(file)
from_serialized_file(file)
else
fmt = Treat::Workers::Formatters::Readers::Autoselect.detect_format(file,def_fmt)
Expand Down Expand Up @@ -240,7 +239,7 @@ def from_raw_file(file, def_fmt='txt')

# Build an entity from a serialized file.
def from_serialized_file(file)

unless File.readable?(file)
raise Treat::Exception,
"Path '#{file}' does not "+
Expand All @@ -249,19 +248,22 @@ def from_serialized_file(file)
doc = Treat::Entities::Document.new
doc.set :file, file
format = nil
if file.index('yml') ||
file.index('yaml')
if File.extname(file) == '.yml' ||
File.extname(file) == '.yaml'
format = :yaml
elsif file.index('xml')
f = :xml
elsif File.extname(file) == '.xml'
format = :xml
else
raise Treat::Exception,
"Unreadable serialized format for #{file}."
end
doc.unserialize(format)
doc.children[0].set_as_root! # Fix this
doc.children[0]
end

def is_serialized_file?(path_to_check)
(AcceptedFormats.include? File.extname(path_to_check)) && (File.file?(path_to_check))
end

def from_db(hash)
Expand All @@ -285,7 +287,7 @@ def anything_from_string(string)
if folder[-1] == '/'
folder = folder[0..-2]
end

now = Time.now.to_f
doc_file = folder+ "/#{now}.txt"
string.force_encoding('UTF-8')
Expand Down Expand Up @@ -383,7 +385,7 @@ def zone_from_string(string)
end

end

def create_collection(fv)
FileUtils.mkdir(fv)
Treat::Entities::Collection.new(fv)
Expand Down

0 comments on commit eb0ec83

Please sign in to comment.