Class: Meta::Catalog

Inherits:
Object
  • Object
show all
Defined in:
lib/meta/catalog.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCatalog

Returns a new instance of Catalog.



7
8
9
10
11
# File 'lib/meta/catalog.rb', line 7

def initialize

  self.db = Sequel.sqlite(Meta::DATASTORE)

end

Instance Attribute Details

#dbObject

Returns the value of attribute db.



5
6
7
# File 'lib/meta/catalog.rb', line 5

def db
  @db
end

#resourcesObject

Returns the value of attribute resources.



5
6
7
# File 'lib/meta/catalog.rb', line 5

def resources
  @resources
end

Class Method Details

.upgradeObject



13
14
15
16
17
18
19
20
# File 'lib/meta/catalog.rb', line 13

def self.upgrade

  db = Sequel.sqlite(Meta::DATASTORE)

  Sequel::Migrator.run( db, File.join( File.dirname(__FILE__),
    "../../db/migrate" ) )

end

Instance Method Details

#add_content(file, hash) ⇒ Object



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/meta/catalog.rb', line 139

def add_content( file, hash )

  title   = ask "Please add a Title for #{file}? "

  layout  = select_template("layouts")
  navbar  = select_template("navbars")
  # leave pages out for now, just default to page.haml
  #page    = select_template("pages")
  footer  = select_template("footers")

  self.db[:contents].insert(
    :title => title,
    :hash => hash,
    :path => file,
    :layout_id => layout,
    :navbar_id => navbar,
    :page_id => 1,
    :footer_id => footer,
    :created_at => Time.now )

end

#add_template(file, hash) ⇒ Object



211
212
213
214
215
216
217
218
219
# File 'lib/meta/catalog.rb', line 211

def add_template( file, hash )

  dir = File.dirname(file)

  self.db[dir.to_sym].insert(
    :path => file,
    :hash => hash )

end

#content_exists?(file) ⇒ Boolean

Returns:

  • (Boolean)


33
34
35
36
37
38
39
40
41
42
43
# File 'lib/meta/catalog.rb', line 33

def content_exists?(file)

  rs = self.db[:contents].where(:path => file).all

  if rs.empty?
    return false
  else
    return true
  end

end

#get_content(file) ⇒ Object



45
46
47
48
49
50
51
# File 'lib/meta/catalog.rb', line 45

def get_content(file)

  rs = self.db[:contents].where(:path => file).first

  return rs

end

#get_recent(count) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/meta/catalog.rb', line 53

def get_recent(count)

  if count < 0
    rs = self.db[:contents].order(Sequel.desc(:created_at)).all
  else
    rs = self.db[:contents].order(Sequel.desc(:created_at)).limit(
      count).all
  end

  rs.each do |r|

    content = Tilt.new(r[:path]).render

    content = "abc" if content.empty?
    
    r[:summary] = content
    r[:link] = File.basename( r[:path], File.extname(r[:path]) ) +
      HTMLEXT
    r[:picture] = false

  end

  return rs

end

#get_resource(template) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/meta/catalog.rb', line 91

def get_resource(template)

  dir = File.dirname(template)

  r = self.db[:resources].where(:folder => dir).first()

  if r.nil?
    return nil
  else
    return r[:id]
  end

end

#get_statisticsObject



22
23
24
25
26
27
28
29
30
31
# File 'lib/meta/catalog.rb', line 22

def get_statistics

  stats = Hash.new

  stats[:posts]       = self.db[:contents].count
  stats[:pictures]    = self.db[:contents].where(:picture => true).count

  return stats

end

#get_template(template, id) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
# File 'lib/meta/catalog.rb', line 79

def get_template( template, id )

  rs = self.db[template.to_sym].where(:id => id).first

  if rs.nil?
    return nil
  else
    return rs[:path]
  end

end

#revise_content(file, hash) ⇒ Object



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/meta/catalog.rb', line 161

def revise_content( file, hash )
  
  content = self.db[:contents].where(:path => file).first
  
  puts "Changes detected for #{file}".yellow if hash != content[:hash]

  if content[:layout_id].nil?
    # for legacy schema purposes
    puts "Select layout for #{file}:"
    lid = select_template("layouts")
  else
    lid = content[:layout_id]
  end

  self.db[:contents].where(:path => file).update(
    :hash => hash,
    :layout_id => lid,
    :page_id => 1,
    #:title => title,
    :updated_at => Time.now )

end

#revise_template(file, hash) ⇒ Object



221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/meta/catalog.rb', line 221

def revise_template( file, hash )

  dir = File.dirname(file)

  rs = self.db[dir.to_sym].where( :hash => hash, :path => file ).first

  if rs.empty?

    self.db[dir.to_sym].insert(
      :path => file,
      :hash => hash )

  end

end

#select_template(template) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/meta/catalog.rb', line 105

def select_template(template)

  rs = self.db[template.to_sym].all

  return rs[0][:id] if rs.count == 1
  return nil if rs.empty?

  choose do |menu|
   
    menu.prompt = "Choose #{template}: "
    rs.each do |r|
      menu.choice File.basename(r[:path]) do return r[:id] end
    end
   
  end
  
end

#sync_content(content) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/meta/catalog.rb', line 123

def sync_content(content)

  hash = Digest::MD5.hexdigest(File.read(content))

  if content_exists?(content)
    revise_content( content, hash )
  else
    add_content( content, hash )
  end

  rs = self.db[:contents].where(:hash => hash).first

  return rs

end

#sync_templates(templates) ⇒ Object



191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/meta/catalog.rb', line 191

def sync_templates(templates)

  templates.each do |t|

    hash = Digest::MD5.hexdigest(t)
 
    if template_exists?(t)

      revise_template( t, hash )

    else

      add_template( t, hash )

    end

  end

end

#template_exists?(file) ⇒ Boolean

Returns:

  • (Boolean)


237
238
239
240
241
242
243
244
245
246
247
248
249
# File 'lib/meta/catalog.rb', line 237

def template_exists?(file)

  dir = File.dirname(file)

  rs = self.db[dir.to_sym].where(:path => file).all

  if rs.empty?
    return false
  else
    return true
  end

end

#template_revised?(path, hash) ⇒ Boolean

Returns:

  • (Boolean)


251
252
253
254
255
256
257
258
259
260
261
262
263
# File 'lib/meta/catalog.rb', line 251

def template_revised?( path, hash )

  dir = File.dirname(path)

  rs = self.db[dir.to_sym].where( :hash => hash, :path => path )

  if rs.nil?
    return true
  else
    return false
  end

end

#update_content_title(file, title) ⇒ Object



184
185
186
187
188
189
# File 'lib/meta/catalog.rb', line 184

def update_content_title( file, title )

  self.db[:contents].where(:path => file).update(
    :title => title, :updated_at => Time.now )

end