Class: Metc::Catalog

Inherits:
Object
  • Object
show all
Defined in:
lib/metc/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/metc/catalog.rb', line 7

def initialize

  self.db = Sequel.sqlite(Metc::DATASTORE)
  
end

Instance Attribute Details

#dbObject

Returns the value of attribute db.



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

def db
  @db
end

Instance Method Details

#add_content(file, title, hash) ⇒ Object



86
87
88
89
90
91
92
93
94
# File 'lib/metc/catalog.rb', line 86

def add_content( file, title, hash )

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

end

#add_template(file, hash) ⇒ Object



125
126
127
128
129
130
131
# File 'lib/metc/catalog.rb', line 125

def add_template( file, hash )

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

end

#check_content(content) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/metc/catalog.rb', line 72

def check_content(content)

  title   = ask "Title? "
  hash    = Digest::MD5.hexdigest(content)

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


end

#check_templates(templates) ⇒ Object



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

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_countObject



147
148
149
150
151
# File 'lib/metc/catalog.rb', line 147

def content_count()

  return self.db[:contents].count

end

#content_exists?(file) ⇒ Boolean

Returns:

  • (Boolean)


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

def content_exists?(file)

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

  if rs.empty?
    return false
  else
    return true
  end

end

#get_recent(count) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/metc/catalog.rb', line 49

def get_recent(count)

  if count < 0
    rs = self.db[:contents].order(:created_at).all
  else
    rs = self.db[:contents].order(: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

#revise_content(file, title, hash) ⇒ Object



96
97
98
99
100
101
102
103
# File 'lib/metc/catalog.rb', line 96

def revise_content( file, title, hash )

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

end

#revise_template(file, hash) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/metc/catalog.rb', line 133

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)


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

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)


37
38
39
40
41
42
43
44
45
46
47
# File 'lib/metc/catalog.rb', line 37

def template_revised?( path, hash )

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

  if rs.nil?
    return true
  else
    return false
  end

end