Skip to content

Commit

Permalink
Create real pseudo column
Browse files Browse the repository at this point in the history
  • Loading branch information
kou committed Dec 9, 2016
1 parent 3272dfc commit aa208ee
Showing 1 changed file with 30 additions and 17 deletions.
47 changes: 30 additions & 17 deletions lib/groonga_client_model/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,57 +29,70 @@ def initialize
end

def tables
Tables.new(@raw.tables)
Tables.new(@raw, @raw.tables)
end

class Tables
def initialize(raw)
@raw = raw
def initialize(raw_schema, raw_tables)
@raw_schema = raw_schema
@raw_tables = raw_tables
end

def [](name)
name = name.to_s if name.is_a?(Symbol)
raw_table = @raw[name]
raw_table = @raw_tables[name]
raise Error, "table doesn't exist: <#{name.inspect}>" if raw_table.nil?
Table.new(raw_table)
Table.new(@raw_schema, raw_table)
end

def exist?(name)
@raw.key?(name)
@raw_tables.key?(name)
end
end

class Table
def initialize(raw)
@raw = raw
def initialize(raw_schema, raw_table)
@raw_schema = raw_schema
@raw_table = raw_table
end

def name
@raw.name
@raw_table.name
end

def columns
raw_columns = @raw.columns.merge("_id" => {"name" => "_id"})
if @raw.key_type
raw_columns = raw_columns.merge("_key" => {"name" => "_key"})
raw_columns = {}
raw_columns["_id"] = create_pseudo_column("_id")
if @raw_table.key_type
raw_columns["_key"] = create_pseudo_column("_key")
end
Columns.new(raw_columns)
Columns.new(@raw_schema, @raw_table.columns.merge(raw_columns))
end

private
def create_pseudo_column(name)
raw_column = {
"name" => name,
"indexes" => [],
}
Groonga::Client::Response::Schema::Column.new(@raw_schema, raw_column)
end
end

class Columns
include Enumerable

def initialize(raw)
@raw = raw
def initialize(raw_schema, raw_columns)
@raw_schema = raw_schema
@raw_columns = raw_columns
end

def names
@raw.keys
@raw_columns.keys
end

def each
@raw.each do |name, column|
@raw_columns.each do |name, column|
yield(name, column)
end
end
Expand Down

0 comments on commit aa208ee

Please sign in to comment.