dirk 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/dirk.rb +136 -0
- data/lib/processes/read_file.rb +19 -0
- data/test/fixtures/data/_attributes.txt +6 -0
- data/test/fixtures/data/news/_2009-04-36.txt +0 -0
- data/test/fixtures/data/portfolio/project_1/_attributes.txt +0 -0
- data/test/fixtures/data/portfolio/project_1/sub-project_1/_attributes.txt +0 -0
- data/test/fixtures/data/portfolio/project_1/sub-project_2/_attributes.txt +0 -0
- data/test/fixtures/data/portfolio/project_1/sub-project_3/_attributes.txt +0 -0
- data/test/fixtures/data/portfolio/project_2/_attributes.txt +3 -0
- data/test/fixtures/data/portfolio/project_2/_file_attributes.txt +6 -0
- data/test/fixtures/data/portfolio/project_2/image_1.jpg +0 -0
- data/test/fixtures/data/portfolio/project_2/image_2.jpg +0 -0
- data/test/fixtures/data/portfolio/project_2/sub-project_1/_attributes.txt +0 -0
- data/test/fixtures/data/portfolio/project_2/sub-project_2/broken_image_1.jpg +0 -0
- data/test/fixtures/data/portfolio/project_3/^attributes.txt +2 -0
- data/test/test_dirk.rb +82 -0
- metadata +70 -0
data/lib/dirk.rb
ADDED
@@ -0,0 +1,136 @@
|
|
1
|
+
require 'yaml'
|
2
|
+
# require 'image_fu'
|
3
|
+
|
4
|
+
class Dirk
|
5
|
+
attr_reader :path, :attributes, :attr_file
|
6
|
+
|
7
|
+
def self.dir
|
8
|
+
""
|
9
|
+
end
|
10
|
+
|
11
|
+
def initialize(p = "", options = {})
|
12
|
+
@prefix = options[:prefix] || "_"
|
13
|
+
@attr_file = @prefix + (options[:attr_file] || "attributes.txt")
|
14
|
+
@root_path = Pathname.new(options[:root] || "public/data")
|
15
|
+
@path = (@root_path + self.class.dir + p).realpath
|
16
|
+
@attributes = get_attributes
|
17
|
+
end
|
18
|
+
|
19
|
+
# used to access the attributes of the current object
|
20
|
+
def method_missing(name, *args, &block)
|
21
|
+
@attributes ? @attributes[name.to_s] : nil
|
22
|
+
end
|
23
|
+
|
24
|
+
def name
|
25
|
+
slug.gsub(/_/, " ").split(".")[0]
|
26
|
+
end
|
27
|
+
|
28
|
+
def slug
|
29
|
+
path.basename.to_s
|
30
|
+
end
|
31
|
+
|
32
|
+
# returns the path relative to the root_path for the application
|
33
|
+
def relative_path(p = path)
|
34
|
+
anchor = p.ftype == "directory" ? @root_path : "public"
|
35
|
+
p.relative_path_from(Pathname.new(anchor).realpath)
|
36
|
+
end
|
37
|
+
|
38
|
+
# returns the url corresponding to the path
|
39
|
+
def url(p = path)
|
40
|
+
"/" + relative_path(p).to_s.downcase.gsub(/\s/, " ")
|
41
|
+
end
|
42
|
+
|
43
|
+
def extension
|
44
|
+
@path.extname[1..-1]
|
45
|
+
end
|
46
|
+
|
47
|
+
def parent
|
48
|
+
Dirk.new(@path.parent)
|
49
|
+
end
|
50
|
+
|
51
|
+
# creates a hash of Dirk objects for any non-system sub-directories of the current directory
|
52
|
+
def children(options = {})
|
53
|
+
children = get_children("directory").collect { |child| Dirk.new(child) }
|
54
|
+
children.reject! { |child| child.published == false || child.draft == true }
|
55
|
+
children.sort! { |a, b| a.send(options[:sort]).to_s <=> b.send(options[:sort]).to_s } if options[:sort]
|
56
|
+
children.reverse if options[:order].to_s.upcase == "DESC"
|
57
|
+
children
|
58
|
+
end
|
59
|
+
|
60
|
+
# creates a hash of Dirk objects for any non-system files in the current directory
|
61
|
+
def files(options = {})
|
62
|
+
resize_images_if_needed
|
63
|
+
files = get_children("file").collect do |file|
|
64
|
+
Dirk.new(file, { :attr_file => file.basename.to_s.gsub(file.extname, '.txt') })
|
65
|
+
end
|
66
|
+
|
67
|
+
files.sort! { |a, b| a.send(options[:sort]).to_s <=> b.send(options[:sort]).to_s } if options[:sort]
|
68
|
+
files
|
69
|
+
end
|
70
|
+
|
71
|
+
def thumb_path
|
72
|
+
thumb_path = Pathname.new(parent.path.realpath.to_s + "/#{@prefix}thumbs/" + path.basename)
|
73
|
+
create_thumbnail(path) unless thumb_path.exist?
|
74
|
+
thumb_path.realpath
|
75
|
+
end
|
76
|
+
|
77
|
+
def thumb_url
|
78
|
+
url(thumb_path)
|
79
|
+
end
|
80
|
+
|
81
|
+
private
|
82
|
+
|
83
|
+
def resize_images_if_needed
|
84
|
+
resize_path = Pathname.new(path.parent.realpath.to_s + "/#{@prefix}resize/")
|
85
|
+
|
86
|
+
if resize_path.exist?
|
87
|
+
back_path = (file.dirname).realpath.to_s + "/#{@prefix}backup"
|
88
|
+
FileUtils.mkdir_p back_dir unless File.exists?(back_dir)
|
89
|
+
FileUtils.mv(file, back_dir)
|
90
|
+
|
91
|
+
resize_path.entries.each do |image_path|
|
92
|
+
convert_size(image_path, path.parent.real_path)
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
def convert_size(file, destination)
|
98
|
+
%x{convert #{destination + "/" + file.basename} -resize 1000x1000 #{file}}
|
99
|
+
end
|
100
|
+
|
101
|
+
def get_children(type = %w{directory file})
|
102
|
+
@path.children.
|
103
|
+
reject { |child| [@prefix, "."].include?(child.basename.to_s[0..0]) }.
|
104
|
+
reject { |child| not [*type].include?(child.ftype) }
|
105
|
+
end
|
106
|
+
|
107
|
+
def get_attributes
|
108
|
+
file_contents = read_file(@attr_file)
|
109
|
+
return YAML::load(file_contents) if file_contents
|
110
|
+
{}
|
111
|
+
end
|
112
|
+
|
113
|
+
#TODO needs re-factoring - path should only be the directory and the file should be separate
|
114
|
+
def read_file(file)
|
115
|
+
text_file = (path.ftype == "directory" ? path : Pathname.new(path.dirname)) + file
|
116
|
+
File.read(text_file) if File.exists?(text_file)
|
117
|
+
end
|
118
|
+
|
119
|
+
end
|
120
|
+
|
121
|
+
class FileAttributes
|
122
|
+
def initialize(file)
|
123
|
+
return false unless file && !File.exists?(file)
|
124
|
+
contents = File.read(file)
|
125
|
+
@attribs = YAML::load(contents) if contents
|
126
|
+
end
|
127
|
+
|
128
|
+
def read(filename)
|
129
|
+
@attribs[filename]["title"]
|
130
|
+
end
|
131
|
+
|
132
|
+
def keys(filename)
|
133
|
+
@attribs[filename].keys
|
134
|
+
end
|
135
|
+
|
136
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'faster_csv'
|
2
|
+
|
3
|
+
module Process
|
4
|
+
def contents(file)
|
5
|
+
case file.extname
|
6
|
+
when "txt":
|
7
|
+
file_contents = read_file(file)
|
8
|
+
return YAML::load_stream(file_contents).documents if file_contents
|
9
|
+
when "csv":
|
10
|
+
return FasterCSV.for_each load(file_contents) if %w{txt yml}.include?(file.extname)
|
11
|
+
end
|
12
|
+
{}
|
13
|
+
end
|
14
|
+
|
15
|
+
def read_file(file)
|
16
|
+
text_file = (path.ftype == "directory" ? path : Pathname.new(path.dirname)) + file
|
17
|
+
File.read(text_file) if File.exists?(text_file)
|
18
|
+
end
|
19
|
+
end
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
Binary file
|
Binary file
|
File without changes
|
data/test/test_dirk.rb
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'test/unit'
|
3
|
+
require File.expand_path(
|
4
|
+
File.join(File.dirname(__FILE__), %w[.. lib dirk]))
|
5
|
+
|
6
|
+
class Projects < Dirk
|
7
|
+
def self.dir
|
8
|
+
"portfolio"
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
class TestDirk < Test::Unit::TestCase
|
13
|
+
def setup
|
14
|
+
test_root = File.join(File.dirname(__FILE__), 'fixtures', 'data')
|
15
|
+
@test_options = { :root => test_root }
|
16
|
+
|
17
|
+
@root = Dirk.new("", @test_options )
|
18
|
+
@projects = Projects.new("", @test_options )
|
19
|
+
@first_project = Dirk.new("portfolio/project_1", @test_options )
|
20
|
+
@second_project = Dirk.new("portfolio/project_2", @test_options )
|
21
|
+
@second_sub_project = Dirk.new("portfolio/project_2/sub-project_2", @test_options )
|
22
|
+
@image_1 = Pathname.new(test_root) + "portfolio/project_2/image_1.jpg"
|
23
|
+
@image_2 = Pathname.new(test_root) + "portfolio/project_2/image_2.jpg"
|
24
|
+
end
|
25
|
+
|
26
|
+
def test_subclass_can_set_starting_directory
|
27
|
+
assert_equal @projects.path, Dirk.new("portfolio", @test_options ).path
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_dirk_correctly_counts_files
|
31
|
+
assert_equal 0, @root.files.size
|
32
|
+
assert_equal 1, @second_sub_project.files.size
|
33
|
+
assert_equal 2, @second_sub_project.parent.files.size
|
34
|
+
end
|
35
|
+
|
36
|
+
def test_dirk_correctly_shows_files
|
37
|
+
assert_equal @image_1, @projects.children[1].files[0].path
|
38
|
+
assert_equal [@image_1, @image_2], @second_sub_project.parent.files.collect { |file| file.path }
|
39
|
+
end
|
40
|
+
|
41
|
+
def test_dirk_correctly_counts_children
|
42
|
+
assert_equal 3, @root.children.size
|
43
|
+
end
|
44
|
+
|
45
|
+
def test_dirk_correctly_reads_parent
|
46
|
+
assert_equal @root.path, @root.children.first.parent.path
|
47
|
+
assert_equal @root.title, @root.children.first.parent.title
|
48
|
+
end
|
49
|
+
|
50
|
+
def test_dirk_correctly_traverses_heirarchy
|
51
|
+
assert_equal "portfolio", @root.children[2].path.basename.to_s
|
52
|
+
end
|
53
|
+
|
54
|
+
def test_dirk_correctly_computes_object_directory
|
55
|
+
assert_equal @second_sub_project.path, @root.children[2].children[1].children[1].path
|
56
|
+
end
|
57
|
+
|
58
|
+
def test_dirk_correctly_reads_attributes
|
59
|
+
assert_equal "Portfolio Site", @root.title
|
60
|
+
assert_equal "This is the portfolio of a madman.", @root.sub_title
|
61
|
+
assert_equal "Welcome to the terrordome.\nWe do things differently inside.\nThere is no place to hide.", @root.body
|
62
|
+
assert_equal "Project 2", @second_sub_project.parent.title
|
63
|
+
assert_equal "This is a really great project", @second_sub_project.parent.sub_title
|
64
|
+
end
|
65
|
+
|
66
|
+
def test_dirk_accepts_root_override
|
67
|
+
assert true
|
68
|
+
end
|
69
|
+
|
70
|
+
def test_dirk_accepts_prefix_override
|
71
|
+
project = Dirk.new("portfolio/project_3", @test_options.merge({:prefix => "^"}) )
|
72
|
+
assert_equal "This is project 3", project.title
|
73
|
+
end
|
74
|
+
|
75
|
+
def test_dirk_returns_slug
|
76
|
+
assert_equal "image_1.jpg", Dirk.new(@image_1).slug
|
77
|
+
end
|
78
|
+
|
79
|
+
def test_dirk_returns_name
|
80
|
+
assert_equal "image 1", Dirk.new(@image_1).name
|
81
|
+
end
|
82
|
+
end
|
metadata
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: dirk
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- James Andrews
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-10-13 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description:
|
17
|
+
email: me@jamesandre.ws
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files: []
|
23
|
+
|
24
|
+
files:
|
25
|
+
- test/fixtures/data/_attributes.txt
|
26
|
+
- test/fixtures/data/news/_2009-04-36.txt
|
27
|
+
- test/fixtures/data/portfolio/project_1/_attributes.txt
|
28
|
+
- test/fixtures/data/portfolio/project_1/sub-project_1/_attributes.txt
|
29
|
+
- test/fixtures/data/portfolio/project_1/sub-project_2/_attributes.txt
|
30
|
+
- test/fixtures/data/portfolio/project_1/sub-project_3/_attributes.txt
|
31
|
+
- test/fixtures/data/portfolio/project_2/_attributes.txt
|
32
|
+
- test/fixtures/data/portfolio/project_2/_file_attributes.txt
|
33
|
+
- test/fixtures/data/portfolio/project_2/image_1.jpg
|
34
|
+
- test/fixtures/data/portfolio/project_2/image_2.jpg
|
35
|
+
- test/fixtures/data/portfolio/project_2/sub-project_1/_attributes.txt
|
36
|
+
- test/fixtures/data/portfolio/project_2/sub-project_2/broken_image_1.jpg
|
37
|
+
- test/fixtures/data/portfolio/project_3/^attributes.txt
|
38
|
+
- test/test_dirk.rb
|
39
|
+
- lib/dirk.rb
|
40
|
+
- lib/processes/read_file.rb
|
41
|
+
has_rdoc: true
|
42
|
+
homepage: http://jamesandre.ws
|
43
|
+
licenses: []
|
44
|
+
|
45
|
+
post_install_message:
|
46
|
+
rdoc_options: []
|
47
|
+
|
48
|
+
require_paths:
|
49
|
+
- lib
|
50
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: "0"
|
55
|
+
version:
|
56
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - ">="
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: "0"
|
61
|
+
version:
|
62
|
+
requirements: []
|
63
|
+
|
64
|
+
rubyforge_project: dirk
|
65
|
+
rubygems_version: 1.3.5
|
66
|
+
signing_key:
|
67
|
+
specification_version: 3
|
68
|
+
summary: Makes your file system act as a database
|
69
|
+
test_files: []
|
70
|
+
|