Class: Collection

Inherits:
Object
  • Object
show all
Includes:
FileBase
Defined in:
lib/collection.rb

Constant Summary

Constants included from FileBase

FileBase::Home

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from FileBase

#copy, #destroy, #exists?, #file_path, #has_location?, #home, #items_under, #location, #make_dir, #mkdir, #move, #path, #reset_location, #set_home, #set_location, #setup_location, #write, #write_template

Constructor Details

#initialize(base, where, name) ⇒ Collection

Returns a new instance of Collection.



4
5
6
7
8
9
10
11
12
13
# File 'lib/collection.rb', line 4

def initialize base, where, name
  set_home where
  @base = base
  @name = name
  @attrs = []
  @listing = []
  @entries = []
  setup_location 'collections'
  set_home path+name
end

Instance Attribute Details

#attrsObject

Returns the value of attribute attrs.



2
3
4
# File 'lib/collection.rb', line 2

def attrs
  @attrs
end

#baseObject

Returns the value of attribute base.



2
3
4
# File 'lib/collection.rb', line 2

def base
  @base
end

#contentsObject

Returns the value of attribute contents.



2
3
4
# File 'lib/collection.rb', line 2

def contents
  @contents
end

#entriesObject

Returns the value of attribute entries.



2
3
4
# File 'lib/collection.rb', line 2

def entries
  @entries
end

#listingObject

Returns the value of attribute listing.



2
3
4
# File 'lib/collection.rb', line 2

def listing
  @listing
end

#nameObject

Returns the value of attribute name.



2
3
4
# File 'lib/collection.rb', line 2

def name
  @name
end

Class Method Details

.collection_exists?(base, where, name) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/collection.rb', line 38

def self.collection_exists?(base, where, name)
  exists?(:folder, base) && exists?(:folder, where+'collections/'+name) && exists?(:file, where+"collections/#{name}/attributes")
end

.load(base, where, name) ⇒ Object



41
42
43
44
45
46
47
48
49
# File 'lib/collection.rb', line 41

def self.load base, where, name
  return nil unless self.collection_exists?(base, where, name)
  c = self.new base, where, name
  c.load_attributes
  c.load_listing
  c.sanitize_listing
  c.load_entries
  c
end

.start(base, where, name, attrs) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/collection.rb', line 19

def self.start base, where, name, attrs
  return nil if self.collection_exists?(base, where, name)
  c = self.new base, where, name
  c.attrs << attrs
  c.attrs.flatten!
  c.write_attribute_file 
  c.load_attributes
  c.write_display_file rescue puts $!
  c.write_feed_file
  copy base+'.templates/disco/link_list', c.home
  c.write_paginate_file
  c.write '', c.home+'listing'
  c.mkdir c.home+'entries'
  c
end

Instance Method Details

#add_entry(title = nil) ⇒ Object



105
106
107
108
109
110
111
112
# File 'lib/collection.rb', line 105

def add_entry title=nil
  timestamp = Time.now.to_i.to_s 
  title ||=  timestamp
  load_listing
  listing.unshift({:id => timestamp, :publish => true})
  write_template home+'attributes', home+'entries/'+timestamp
  return home+'entries/'+timestamp
end

#display_fileObject



35
36
37
# File 'lib/collection.rb', line 35

def display_file
  home+'display'
end

#file_nameObject



15
16
17
# File 'lib/collection.rb', line 15

def file_name
  name.as_file
end

#load_attributesObject



88
89
90
# File 'lib/collection.rb', line 88

def load_attributes
  File.open( home+'attributes' ) { |yf| @attrs = YAML::load( yf )[name] }
end

#load_entriesObject



96
97
98
99
# File 'lib/collection.rb', line 96

def load_entries
  listing.each {|entry| entries <<  load_entry(entry) }
  entries
end

#load_entry(file) ⇒ Object



100
101
102
103
104
# File 'lib/collection.rb', line 100

def load_entry(file)
  tmp = nil
  File.open( home+'entries/'+file[:id].to_s ) {|yf| tmp = YAML::load( yf )[name].merge(file)} rescue puts "loading entry -- #{$!}"
  tmp
end

#load_listingObject



91
92
93
94
95
# File 'lib/collection.rb', line 91

def load_listing
  File.open( home+'listing' ) { |yf| @listing = YAML::load( yf ) }
  @listing ||= []
  @listing = @listing.sort{|x, y| x[:id] <=> y[:id]}.reverse
end

#publishObject



113
114
115
116
117
118
119
120
121
# File 'lib/collection.rb', line 113

def publish
  tmp = {}
  listing.each {|e|
    if e[:publish] and not e[:publish].kind_of? String
      e[:publish] = Time.now.to_s
    end
  }
  save_listing
end

#sanitize_listingObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/collection.rb', line 56

def sanitize_listing
  tmp = []
  @listing.each { |item|
    entry = load_entry(item)
    add = false
    attrs = File.open( home+'attributes' ) { |yf| @attrs = YAML::load( yf )[name] }
    entry.each { |key, value|
      if attrs.include? key
        add = true if value and not attrs[key]
      end
    }
    tmp << item if add
  }
  @listing = tmp
  save_listing
end

#save_listingObject



51
52
53
54
# File 'lib/collection.rb', line 51

def save_listing
  File.open(home+'listing',  'w') { |yf| YAML.dump(@listing,  yf) }
  @listing
end

#write_attribute_fileObject



73
74
75
# File 'lib/collection.rb', line 73

def write_attribute_file
  write_template base+'.templates/disco/attributes', home+'attributes'
end

#write_display_fileObject



76
77
78
# File 'lib/collection.rb', line 76

def write_display_file
  write_template base+'.templates/disco/display', home+'display'
end

#write_feed_fileObject



79
80
81
# File 'lib/collection.rb', line 79

def write_feed_file
  write_template base+'.templates/disco/feed', home+'feed'
end


85
86
87
# File 'lib/collection.rb', line 85

def write_link_list_file
  write_template base+'.templates/disco/link_list', home+'link_list'
end

#write_paginate_fileObject



82
83
84
# File 'lib/collection.rb', line 82

def write_paginate_file
  write_template base+'.templates/disco/paginate', home+'paginate'
end