Module: Virsandra::Model::ClassMethods

Defined in:
lib/virsandra/model.rb

Instance Method Summary collapse

Instance Method Details

#allObject


76
77
78
# File 'lib/virsandra/model.rb', line 76

def all
  where({})
end

#attribute_namesObject Also known as: column_names


70
71
72
# File 'lib/virsandra/model.rb', line 70

def attribute_names
  attribute_set.map(&:name)
end

#find(columns) ⇒ Object

Raises:

  • (ArgumentError)

52
53
54
55
# File 'lib/virsandra/model.rb', line 52

def find(columns)
  raise ArgumentError.new("Invalid key") unless valid_key?(columns)
  load(columns)
end

#key(*columns) ⇒ Object


47
48
49
50
# File 'lib/virsandra/model.rb', line 47

def key(*columns)
  @key = columns unless columns.empty?
  @key
end

#load(columns) ⇒ Object


57
58
59
60
61
62
63
# File 'lib/virsandra/model.rb', line 57

def load(columns)
  record = new(columns)

  row = ModelQuery.new(record).find_by_key
  record.attributes = row.merge(columns)
  record
end

#table(name = nil) ⇒ Object


42
43
44
45
# File 'lib/virsandra/model.rb', line 42

def table(name = nil)
  @table = name if name
  @table
end

#valid_key?(columns) ⇒ Boolean

Returns:

  • (Boolean)

65
66
67
68
# File 'lib/virsandra/model.rb', line 65

def valid_key?(columns)
  return false if columns.length != key.length
  key.all? {|k| !columns[k].nil? }
end

#where(params) ⇒ Object


80
81
82
# File 'lib/virsandra/model.rb', line 80

def where(params)
  ModelQuery.new(self).where(params)
end