Class: Feedcellar::GroongaDatabase

Inherits:
Object
  • Object
show all
Defined in:
lib/feedcellar/groonga_database.rb

Instance Method Summary collapse

Constructor Details

#initializeGroongaDatabase

Returns a new instance of GroongaDatabase.



24
25
26
# File 'lib/feedcellar/groonga_database.rb', line 24

def initialize
  @database = nil
end

Instance Method Details

#add(resource_key, title, link, description, date) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/feedcellar/groonga_database.rb', line 51

def add(resource_key, title, link, description, date)
  columns = {
    :resource    => resources[resource_key],
    :title       => title,
    :link        => link,
    :description => description,
    :date        => date,
  }
  if date
    if feeds.have_column?(:year)
      columns[:year] = date.year
    end
    if feeds.have_column?(:month)
      columns[:month] = date.month
    end
    if feeds.have_column?(:day)
      columns[:day] = date.day
    end
    if feeds.have_column?(:wday)
      columns[:wday] = Date.new(date.year, date.month, date.day).wday
    end
  end
  feeds.add(link, columns)
end

#closeObject



111
112
113
114
# File 'lib/feedcellar/groonga_database.rb', line 111

def close
  @database.close
  @database = nil
end

#closed?Boolean

Returns:

  • (Boolean)


116
117
118
# File 'lib/feedcellar/groonga_database.rb', line 116

def closed?
  @database.nil? or @database.closed?
end

#delete(id_or_key_or_conditions) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/feedcellar/groonga_database.rb', line 76

def delete(id_or_key_or_conditions)
  if id_or_key_or_conditions.is_a?(Integer)
    id = id_or_key_or_conditions
    feeds.delete(id, :id => true)
  elsif id_or_key_or_conditions.is_a?(String)
    key = id_or_key_or_conditions
    feeds.delete(key)
  elsif id_or_key_or_conditions.is_a?(Hash)
    conditions = id_or_key_or_conditions
    feeds.delete do |record|
      expression_builder = nil
      conditions.each do |key, value|
        case key
        when :resource_key
          record &= (record.resource._key == value)
        else
          raise ArgumentError,
                "Not supported condition: <#{key}>"
        end
      end
      record
    end
  else
    raise ArgumentError,
          "Not supported type: <#{id_or_conditions.class}>"
  end
end

#dumpObject



128
129
130
# File 'lib/feedcellar/groonga_database.rb', line 128

def dump
  Groonga::DatabaseDumper.dump
end

#feedsObject



124
125
126
# File 'lib/feedcellar/groonga_database.rb', line 124

def feeds
  @feeds ||= Groonga["Feeds"]
end

#open(base_path, encoding = :utf8) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/feedcellar/groonga_database.rb', line 28

def open(base_path, encoding=:utf8)
  reset_context(encoding)
  path = File.join(base_path, "feedcellar.db")
  if File.exist?(path)
    @database = Groonga::Database.open(path)
    populate_schema
  else
    FileUtils.mkdir_p(base_path)
    populate(path)
  end
  if block_given?
    begin
      yield(self)
    ensure
      close unless closed?
    end
  end
end

#register(key, attributes) ⇒ Object



47
48
49
# File 'lib/feedcellar/groonga_database.rb', line 47

def register(key, attributes)
  resources.add(key, attributes)
end

#resourcesObject



120
121
122
# File 'lib/feedcellar/groonga_database.rb', line 120

def resources
  @resources ||= Groonga["Resources"]
end

#unregister(title_or_url) ⇒ Object



104
105
106
107
108
109
# File 'lib/feedcellar/groonga_database.rb', line 104

def unregister(title_or_url)
  resources.delete do |record|
    (record.title == title_or_url) |
    (record.xmlUrl == title_or_url)
  end
end