You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
78 lines
1.6 KiB
78 lines
1.6 KiB
module DsAdmin::Model
|
|
include Enumerable
|
|
|
|
DsAdmin::Model.autoload(:User, 'model/user')
|
|
DsAdmin::Model.autoload(:Group, 'model/group')
|
|
DsAdmin::Model.autoload(:Site, 'model/site')
|
|
DsAdmin::Model.autoload(:MailAliasRole, 'model/mail_alias_role')
|
|
DsAdmin::Model.autoload(:MailAliasPerson, 'model/mail_alias_person')
|
|
DsAdmin::Model.autoload(:MailAccount, 'model/mail_account')
|
|
|
|
def (DsAdmin::Model).storage=(storage)
|
|
@@storage = storage
|
|
end
|
|
|
|
def (DsAdmin::Model).storage
|
|
@@storage
|
|
end
|
|
|
|
attr_reader :id
|
|
|
|
def initialize(args = {})
|
|
@id = args[:id] if args[:id]
|
|
end
|
|
|
|
def all
|
|
@@storage.config.model = self
|
|
|
|
@@storage.map do |data|
|
|
self.class.new(data)
|
|
end
|
|
end
|
|
|
|
def each(&block)
|
|
all.each(&block)
|
|
end
|
|
|
|
def load(id)
|
|
self.class.new(_load(id))
|
|
end
|
|
|
|
def load!(id)
|
|
initialize(_load(id))
|
|
end
|
|
|
|
##
|
|
# used to save either new or changed models
|
|
#
|
|
# @storage is free to change an already existent storage id
|
|
# for organizational, indexing or other reasons thus it
|
|
# always has to be stored back into @id
|
|
#
|
|
# Note: We do not need to set the actual model here, as
|
|
# write gets a reference of self and thus can extract the used
|
|
# model with self.class
|
|
#
|
|
def save
|
|
@id = @@storage.write(self)
|
|
end
|
|
|
|
def config_key
|
|
self.class.to_s.to_sym
|
|
end
|
|
|
|
def to_h
|
|
Hash[instance_variables.map do |var|
|
|
[var[1...var.size].to_sym, instance_variable_get(var)]
|
|
end]
|
|
end
|
|
|
|
protected
|
|
def _load(id)
|
|
@@storage.config.model = self
|
|
|
|
data = @@storage.find {|data| data[:id] == id}
|
|
raise "unknown id (#{id})" unless data
|
|
data
|
|
end
|
|
end
|