jbe-Nest 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.textile +112 -0
- data/lib/nest.rb +136 -0
- metadata +54 -0
data/README.textile
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
|
|
2
|
+
h1. Nest
|
|
3
|
+
|
|
4
|
+
h2. YAML file tree object wrapper in Ruby
|
|
5
|
+
|
|
6
|
+
Nest is an object wrapper for mounting and reading data from trees of multiple YAML files. You may think of it as a mapping between files and objects. While this may reek of meta (shrug), it it does real world® usage.
|
|
7
|
+
|
|
8
|
+
h3. Installation
|
|
9
|
+
|
|
10
|
+
@gem sources -a http://gems.github.com@ if you haven't already.
|
|
11
|
+
@sudo gem install jbe-nest@
|
|
12
|
+
|
|
13
|
+
h2. Explaination and usage
|
|
14
|
+
|
|
15
|
+
The nest data model has:
|
|
16
|
+
|
|
17
|
+
* A mount point in some file system
|
|
18
|
+
* A corresponding root node
|
|
19
|
+
* Zero or more ascending nodes
|
|
20
|
+
|
|
21
|
+
Every node has:
|
|
22
|
+
|
|
23
|
+
* Any number of properties (described in YAML)
|
|
24
|
+
* Zero or more ascending files
|
|
25
|
+
|
|
26
|
+
I wrote nest as a file based data model for a website, where every node corresponded to a viewable page, and aditional contents was kept in files.
|
|
27
|
+
|
|
28
|
+
This is an example of a mountable nest directory structure:
|
|
29
|
+
|
|
30
|
+
<pre>
|
|
31
|
+
<code>
|
|
32
|
+
doc/
|
|
33
|
+
index.yml
|
|
34
|
+
about.yml
|
|
35
|
+
blob.file
|
|
36
|
+
fruits/
|
|
37
|
+
index.yml
|
|
38
|
+
apple.yml
|
|
39
|
+
banana/
|
|
40
|
+
index.yml
|
|
41
|
+
banana.jpg
|
|
42
|
+
</code>
|
|
43
|
+
</pre>
|
|
44
|
+
|
|
45
|
+
This gives the following tree of nodes, excluding files:
|
|
46
|
+
|
|
47
|
+
<pre>
|
|
48
|
+
<code>
|
|
49
|
+
/
|
|
50
|
+
about
|
|
51
|
+
fruits
|
|
52
|
+
apple
|
|
53
|
+
banana
|
|
54
|
+
</code>
|
|
55
|
+
</pre>
|
|
56
|
+
|
|
57
|
+
The @root@ node also contains @blob.file@, and @banana@ also contains @banana.jpg@. All of this is available in the object model. Every node contains it's own YAML.
|
|
58
|
+
|
|
59
|
+
Every node will have a uri corresponding to it's path in the above tree, such as @/fruits/banana@. This is useful when you need urls.
|
|
60
|
+
|
|
61
|
+
h3. Synopsis
|
|
62
|
+
|
|
63
|
+
<pre>
|
|
64
|
+
<code>
|
|
65
|
+
class Library
|
|
66
|
+
include Nest
|
|
67
|
+
mount './doc'
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
l = Library.root
|
|
71
|
+
|
|
72
|
+
l.properties # available methods
|
|
73
|
+
l.children
|
|
74
|
+
l.parent
|
|
75
|
+
l.files
|
|
76
|
+
|
|
77
|
+
</code>
|
|
78
|
+
</pre>
|
|
79
|
+
|
|
80
|
+
h2. Why is this useful?
|
|
81
|
+
|
|
82
|
+
For highly customizeable, file based taxonomies, ie. websites. Let each *.yaml correspond to a page, and contain metainformation, including styles and layout instructions. Keep additional content in files, in accordance with that particular implementation.
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
h3. TODO
|
|
86
|
+
|
|
87
|
+
* Write tests
|
|
88
|
+
|
|
89
|
+
h3. LICENSE
|
|
90
|
+
|
|
91
|
+
(MIT License)
|
|
92
|
+
|
|
93
|
+
Copyright (c) 2009 Jostein Berre Eliassen
|
|
94
|
+
|
|
95
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
|
96
|
+
a copy of this software and associated documentation files (the
|
|
97
|
+
'Software'), to deal in the Software without restriction, including
|
|
98
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
|
99
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
|
100
|
+
permit persons to whom the Software is furnished to do so, subject to
|
|
101
|
+
the following conditions:
|
|
102
|
+
|
|
103
|
+
The above copyright notice and this permission notice shall be
|
|
104
|
+
included in all copies or substantial portions of the Software.
|
|
105
|
+
|
|
106
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
|
107
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
108
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
109
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
|
110
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
|
111
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
|
112
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/lib/nest.rb
ADDED
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
|
|
2
|
+
require 'rubygems'
|
|
3
|
+
require 'yaml'
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
module Nest
|
|
8
|
+
|
|
9
|
+
module ClassMethods
|
|
10
|
+
|
|
11
|
+
attr_reader :mount_point
|
|
12
|
+
|
|
13
|
+
def mount( path )
|
|
14
|
+
@mount_point = path
|
|
15
|
+
raise "invalid path" unless valid?
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def valid?
|
|
19
|
+
root.valid?
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def root
|
|
23
|
+
new '/'
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def self.included(base)
|
|
28
|
+
base.extend(ClassMethods)
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
attr_reader :uri
|
|
32
|
+
|
|
33
|
+
def initialize( uri )
|
|
34
|
+
raise 'nest not mounted' unless self.class.mount_point
|
|
35
|
+
@uri = sanitize( uri )
|
|
36
|
+
raise 'invalid uri' unless valid?
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def base_path
|
|
40
|
+
File.join(self.class.mount_point, @uri)
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def index_path
|
|
44
|
+
File.join(base_path, 'index.yml')
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def file_path
|
|
48
|
+
base_path + '.yml'
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def node_type
|
|
52
|
+
if File.file?(index_path)
|
|
53
|
+
:index
|
|
54
|
+
elsif File.file?(file_path)
|
|
55
|
+
:file
|
|
56
|
+
elsif File.file?(base_path)
|
|
57
|
+
:object
|
|
58
|
+
elsif File.directory?(base_path)
|
|
59
|
+
:object_group
|
|
60
|
+
else
|
|
61
|
+
:error
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
def data_path
|
|
66
|
+
case node_type
|
|
67
|
+
when :index
|
|
68
|
+
index_path
|
|
69
|
+
when :file
|
|
70
|
+
file_path
|
|
71
|
+
else
|
|
72
|
+
raise 'not a data node'
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
def valid?
|
|
77
|
+
node_type != :error
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
def properties
|
|
81
|
+
YAML.load_file data_path
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
def []( name )
|
|
85
|
+
properties[name.to_s]
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
def method_missing( name )
|
|
89
|
+
self[name]
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
def children
|
|
93
|
+
retrieve_sub( :file, :index )
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
def indexes
|
|
97
|
+
retrieve_sub( :index )
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
alias :indices :indexes
|
|
101
|
+
|
|
102
|
+
def endpoints
|
|
103
|
+
retrieve_sub( :file )
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
def files
|
|
107
|
+
retrieve_sub( :object )
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
def file_groups
|
|
111
|
+
retrieve_sub( :file_group )
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
def parent
|
|
115
|
+
raise 'parent not implemented'
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
private
|
|
120
|
+
|
|
121
|
+
def retrieve_sub( *node_types_requested )
|
|
122
|
+
Dir.new(base_path).entries.map do |path|
|
|
123
|
+
unless ['.', '..', 'index.yml'].include? path
|
|
124
|
+
path.gsub! /\.yml$/, ''
|
|
125
|
+
x = self.class.new( File.join( @uri, path ))
|
|
126
|
+
node_types_requested.include?( x.node_type ) ? x : nil
|
|
127
|
+
end
|
|
128
|
+
end.compact.uniq
|
|
129
|
+
end
|
|
130
|
+
|
|
131
|
+
def sanitize( uri )
|
|
132
|
+
File.join( '/',
|
|
133
|
+
*uri.split(/\/|\\/).select{|x| !x.match( /^\.+$/ )} )
|
|
134
|
+
end
|
|
135
|
+
|
|
136
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: jbe-Nest
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 1.2.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Jostein Berre Eliassen
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
|
|
12
|
+
date: 2009-04-12 00:00:00 -07:00
|
|
13
|
+
default_executable:
|
|
14
|
+
dependencies: []
|
|
15
|
+
|
|
16
|
+
description: Ruby YAML file tree object wrapper
|
|
17
|
+
email: jbe@svusj.com
|
|
18
|
+
executables: []
|
|
19
|
+
|
|
20
|
+
extensions: []
|
|
21
|
+
|
|
22
|
+
extra_rdoc_files: []
|
|
23
|
+
|
|
24
|
+
files:
|
|
25
|
+
- README.textile
|
|
26
|
+
- lib/nest.rb
|
|
27
|
+
has_rdoc: false
|
|
28
|
+
homepage: http://github.com/jbe/nest
|
|
29
|
+
post_install_message:
|
|
30
|
+
rdoc_options: []
|
|
31
|
+
|
|
32
|
+
require_paths:
|
|
33
|
+
- lib
|
|
34
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
35
|
+
requirements:
|
|
36
|
+
- - ">="
|
|
37
|
+
- !ruby/object:Gem::Version
|
|
38
|
+
version: "0"
|
|
39
|
+
version:
|
|
40
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
41
|
+
requirements:
|
|
42
|
+
- - ">="
|
|
43
|
+
- !ruby/object:Gem::Version
|
|
44
|
+
version: "0"
|
|
45
|
+
version:
|
|
46
|
+
requirements: []
|
|
47
|
+
|
|
48
|
+
rubyforge_project:
|
|
49
|
+
rubygems_version: 1.2.0
|
|
50
|
+
signing_key:
|
|
51
|
+
specification_version: 2
|
|
52
|
+
summary: Ruby YAML file tree object wrapper
|
|
53
|
+
test_files: []
|
|
54
|
+
|