Class: Meta::Catalog

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

Instance Attribute 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

Instance Method Details

#add_content(file, title, hash) ⇒ Object



83
84
85
86
87
88
89
90
91
# File 'lib/meta/catalog.rb', line 83

def add_content( file, title, hash )

  self.db[:contents].insert(
    :title => title,
    :hash => hash,
    :path => file,
    :created_at => Time.now )

end

#add_template(file, hash) ⇒ Object



130
131
132
133
134
135
136
# File 'lib/meta/catalog.rb', line 130

def add_template( file, hash )

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

end

#check_content(content) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/meta/catalog.rb', line 68

def check_content(content)

  hash    = Digest::MD5.hexdigest(content)

  if content_exists?(content)
    revise_content( content, hash )
  else
    title   = ask "Please add a Title for #{content}? "
    add_content( content, title, hash )
  end

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

end

#check_templates(templates) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/meta/catalog.rb', line 110

def check_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

#content_exists?(file) ⇒ Boolean

Returns:

  • (Boolean)


24
25
26
27
28
29
30
31
32
33
34
# File 'lib/meta/catalog.rb', line 24

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



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

def get_content(file)

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

  return rs

end

#get_recent(count) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/meta/catalog.rb', line 44

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_statisticsObject



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

def get_statistics

  stats = Hash.new

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

  return stats

end

#revise_content(file, hash) ⇒ Object



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

def revise_content( file, hash )

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

end

#revise_template(file, hash) ⇒ Object



138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/meta/catalog.rb', line 138

def revise_template( file, hash )

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

  if rs.empty?

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

  end

end

#template_exists?(file) ⇒ Boolean

Returns:

  • (Boolean)


152
153
154
155
156
157
158
159
160
161
162
# File 'lib/meta/catalog.rb', line 152

def template_exists?(file)

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

  if rs.empty?
    return false
  else
    return true
  end

end

#template_revised?(path, hash) ⇒ Boolean

Returns:

  • (Boolean)


164
165
166
167
168
169
170
171
172
173
174
# File 'lib/meta/catalog.rb', line 164

def template_revised?( path, hash )

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

  if rs.nil?
    return true
  else
    return false
  end

end

#update_content_title(file, title) ⇒ Object



103
104
105
106
107
108
# File 'lib/meta/catalog.rb', line 103

def update_content_title( file, title )

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

end