2016-06-16 45 views
0

我有一個員工類,我存儲員工詳細信息,如年齡,生日和獲得的積分。如何訪問所有創建的對象和更新屬性

class Employee 
    attr_accessor :id, :dob, :points 

    def initialize 
    @id = id 
    @dob = dob 
    @points = points 
    end 

    def tranfer_points 
    # Access both employee objects and add points to one and remove points from other 
    end 
end 


emp_one = Employee.new(1,"2 Jan 1990",400) 
emp_two = Employee.new(2,"12 Jan 1986",700) 

emp_two.transfer() 

我想知道維護所有創建對象的最佳做法是什麼,以便我可以將點從一個員工轉移到其他員工。

我應該在這裏使用類變量,這是一個很好的解決方案,這是一個標準的做法嗎?

像下面

class Employee 
    @@all_employee = Hash.new 
    attr_accessor :id, :dob, :points 

    def initialize 
    @id = id 
    @dob = dob 
    @points = points 
    add_all_employee 
    end 

    def add_all_employee 
    @@all_employee[id] = self 
    end 

    def tranfer_points(i,points) 
    b = @@all_employee[i] 
    self.balance -= points 
    b.points += points 
    end 
end 

任何幫助這裏將不勝感激。謝謝。

+0

沒有數據庫? – scorix

+0

嗨scorix,是的沒有數據庫 –

+0

爲什麼你需要一個'員工'類?不會簡單的'Hash'實例足夠嗎? – mudasobwa

回答

1

下面是一個如何做到這一點的例子。

隨着數據庫:

class Employee 
    def tranfer_points_to(recipient, num_points) 
    Employee.transaction do 
     self.update!(points: self.points - num_points) 
     recipient.update!(points: recipient.points + num_points) 
    end 
    end 
end 

使用Rails Transaction將是一個很好的方式,以確保您不會意外地更新一個,而不是其他,如果有這樣的塊中產生的異常。

沒有數據庫:

class Employee 
    def tranfer_points_to(recipient, num_points) 
    self.points -= num_points 
    recipient.points += num_points 
    end 
end 

UPDATE: 爲了回答您的評論:

如果你考慮使用分貝,那麼它是沒有道理的,只是保存模型。 沒有數據庫,通常使用一些父類來「保留」員工實例。舉個例子:

class Company 
    attr_reader :employees 

    def initialize 
    @employees = [] 
    end 

    def add_employee(id, dob, points) 
    employees.push(Employee.new(id, dob, points)) 
    end 

    def tranfer_points(from_employee_id, to_employee_id, num_points) 
    find_employee_by_id(from_employee_id).points -= num_points 
    find_employee_by_id(to_employee_id).points += num_points 
    end 

    def find_employee_by_id(id) 
    employees.find { |e| e.id == id } 
    end 
end 

company = Company.new 
company.add_employee(1, "2 Jan 1990", 400) 
company.add_employee(2, "2 Jan 1991", 400) 
company.transfer_points(1, 2, 200) 

company.find_employee_by_id(1).points => 200 
company.find_employee_by_id(2).points => 600 
+0

嗨,彼得,我如何找到創建的許多收件人對象。我應該使用類變量來存儲創建的所有員工對象,並使用ID訪問對象嗎?這是一個很好的解決方案 –

+0

嗨,彼得,我已經更新了我的問題,請你看看更新的一個,謝謝 –

+0

@ opensource-ios好吧,我想我知道你想要什麼。請參閱編輯^^ –

0

下面是一個例子:

require 'singleton' 
require 'forwardable' 
require 'thread' 

class EmployeeStore 
    include Singleton 

    class << self 
    extend Forwardable 
    def_delegator :instance, :employees, :employees 
    def_delegator :instance, :find, :find 
    def_delegator :instance, :add, :add 
    def_delegator :instance, :transfer_points, :transfer_points 
    def_delegator :instance, :remove, :remove 
    end 

    def employees 
    @employees.dup 
    end 

    def find(id) 
    @employees[id].dup 
    end 

    def add(employee) 
    @employees[employee.id] = employee 
    end 

    def remove(employee) 
    case employee 
     when Employee 
     @employees.delete(employee.id) 
     when Fixnum 
     @employees.delete(employee) 
     else 
     raise "Don't know how to delete #{employee}" 
    end 
    end 

    def transfer_points(from, to, points) 
    # EmployeeStore is the only way to modify @employees 
    real_from, real_to = find(from.id), find(to.id) 

    # synchronize for thread-safe 
    @mutex.synchronize do 
     f_points, t_points = from.points, to.points 

     begin 
     real_from.instance_variable_set(:@points, f_points - points) 
     real_to.instance_variable_set(:@points, t_points + points) 
      # do something else 
     rescue 
     real_from.instance_variable_set(:@points, f_points) 
     real_to.instance_variable_set(:@points, t_points) 
     ensure 
     from.instance_variable_set(:@points, real_from.points) 
     to.instance_variable_set(:@points, real_to.points) 
     end 
    end 
    end 

    private 
    def initialize 
    @employees = {} 
    @mutex = Mutex.new 
    super 
    end 
end 

class Employee 
    attr_reader :id, :dob, :points 

    def initialize(id, dob, points) 
    @id, @dob, @points = id, dob, points 
    EmployeeStore.add(self) 
    end 

    def transfer_points_to(employee, points) 
    EmployeeStore.transfer_points(self, employee, points) 
    end 
end 


Employee.new(1, "2 Jan 1990", 400) 
Employee.new(2, "12 Jan 1986", 700) 

emp_one = EmployeeStore.find(1) 
emp_two = EmployeeStore.find(2) 

1.upto(10).map do 
    Thread.new do 
    emp_one.transfer_points_to(emp_two, 30) 
    end 
end.map(&:join) 

p emp_one.points # => 100 
p emp_two.points # => 1000 

類變量不是線程安全的