0

我在用 ruby​​ on rails 开发的 projet 工作

我有这条线:

 _orig_account     = Account.find_ussd_by_login_and_pass_and_type_and_name(_orgin,_pin,Account::Type::Creova.to_sym,_orgin)

老实说,我不明白这段代码

我有另一个代码:

 _resul   = PMClient.new.user_login_ussd(_orgin,_pin)

在这种情况下,user_login_ussd 是模型中的一个函数,专门用于:PMClient

Account的源代码是

class Account < CreovaBase

  attr :login, true
  attr :admin_login, true
  attr :admin_pass, true
  attr :type
  attr :name, true
  attr :status
  attr :balance, true
  attr :currency
  attr :dob, true
  attr :bank_id, true
  attr :routing, true
  attr :legal_name, true
  attr :bank_name, true
  attr :attribute, true
  attr :linker_id, true


  Status = Enum.new(:Enabled, :Suspended,:Deleted, :Legal_Hold, ['Enabled','Suspended','Deleted','Legal Hold'])

  Type = Enum.new(:Creova, :Bank, :Credit,:rt, ['Creova','Bank','Credit','rt'])


  Banks = Enum.new(:None, :Test1,:Test2 , :Test3, :Test4,
    ['','Test1','Test2','Test3','Test4'])

  def initialize(params=nil)
    super
  end

  def type=(val)
    val.blank? ? @type = nil : @type = val.to_sym
  end

  def status=(val)
    val.blank? ? @status = nil : @status = val.to_sym 
  end

  def currency=(val)
    val.blank? ? @currency = nil : @currency = val.to_sym
  end

  # should check that the account number either:
  # a) exists in the system
  # b) is a valid mobile number
  # c) ?? is a valid email address ??
  # returns [valid,message]
  def self.is_valid_account?(name, purpose=:send)
    #STUB
    return true
  end


  def formatted_balance(currency = :tnd)
    curr = Currency.get_currency(currency) rescue Currency.get_currency(:tnd)
    curr.display(balance.to_i)  
  end

  def formal_name(currency=:tnd)
    curr = Currency.get_currency(currency) rescue Currency.get_currency(:tnd)
    curr.formal_name
  end

  def save!

    #pmac = CreovaBase._get_admin()

    if @new_record
      # create_account will raise an exception if it cannot be created
      vars = self.instance_variables.map() { |v| v.sub('@','') unless ['@errors','@new_record'].include?(v) }.compact 
      params = {}
      vars.each() do |v|
        if self.send(v).is_a?(Enum::Member)
          params[v] = self.send(v).to_sym
        else
          params[v] = self.send(v)
        end
      end
      if PMClient.new.create_account(params)
        @new_record = false
      else
        raise 'account save failed'
      end
    else
      PMClient.new.update_account(admin_login,admin_pass,type,name,status)
    end
  end

  def save
    save!
  end

end
4

1 回答 1

0

我假设您的问题是“这段代码有什么作用?” (您可能需要在标题和问题文本中进行精确说明)。

 _orig_account     = Account.find_ussd_by_login_and_pass_and_type_and_name(_orgin,_pin,Account::Type::Creova.to_sym,_orgin)

没有任何其他信息,我想:

  • 这是一个查找器方法,用于从数据库中搜索并返回 Account。将finder 查询作为模型上的类方法在Rails 中是非常标准的,许多都是“开箱即用”的。
  • 参数匹配各种“and”,origin 是登录名和名称(在这种情况下两者可能相同),然后是密码和类型(作为Symbol给出,另一种 Ruby 常见做法)。

第二部分:

_resul   = PMClient.new.user_login_ussd(_orgin,_pin)

只是创建一个 PMClient 对象 (PMClient.new),紧接着对新创建的对象 (".user_login_ussd") 进行方法调用,并提供两个参数。

希望这可以帮助。

于 2013-03-13T09:49:55.487 回答