货币宝石:从父模型继承货币

发布于 2024-11-24 07:50:39 字数 1323 浏览 1 评论 0原文

有人可以告诉我如何从父模型继承货币吗?

我使用 Money gem (https://github.com/RubyMoney/money) 并有 2 个模型 (市场和贸易限制)。

市场价格会定期更新,如果存在具有相同价格的交易限额,则会通知用户。

因为市场已经存储了货币,所以我不想在 tradelimit 模型中再次存储它(不干):

class Market << AR
  composed_of :price,
    :class_name => "Money",
    :mapping => [%w(price_cents cents), %w(currency currency_as_string)],
    :constructor => Proc.new { |cents, currency| Money.new(cents || 0, currency || Money.default_currency) },
    :converter => Proc.new { |value|  value.respond_to?(:to_money) ? value.to_money : raise(ArgumentError, "Can't convert #{value.class} to Money") }
  ...
end

#tradelimit.rb

class Tradelimit << AR
  composed_of :price,
    :class_name => "Money",
    :mapping => [%w(price_cents cents), %w(currency currency_as_string)],
    :constructor => Proc.new { |cents, currency| Money.new(cents || 0, currency || Money.default_currency) },
    :converter => Proc.new { |value|  value.respond_to?(:to_money) ? value.to_money : raise(ArgumentError, "Can't convert #{value.class} to Money") }
  ...
end

我现在如何从父(市场)模型继承货币? 我尝试覆盖 tradelimit 模型中的货币方法,但这不起作用..

有什么建议吗?谢谢!

编辑:

或者有没有办法(没有composed_of功能)来提供此功能?

Can someone tell me how to inherit the currency from a parent model?

I use the money gem (https://github.com/RubyMoney/money) and have 2 models (market and tradelimit).

The price of the market becomes updated periodical and if there is a tradelimit which has the same price, the user becomes informed.

Because the market has already the currency stored, i dont want to store it again in the tradelimit model (not dry):

class Market << AR
  composed_of :price,
    :class_name => "Money",
    :mapping => [%w(price_cents cents), %w(currency currency_as_string)],
    :constructor => Proc.new { |cents, currency| Money.new(cents || 0, currency || Money.default_currency) },
    :converter => Proc.new { |value|  value.respond_to?(:to_money) ? value.to_money : raise(ArgumentError, "Can't convert #{value.class} to Money") }
  ...
end

#tradelimit.rb

class Tradelimit << AR
  composed_of :price,
    :class_name => "Money",
    :mapping => [%w(price_cents cents), %w(currency currency_as_string)],
    :constructor => Proc.new { |cents, currency| Money.new(cents || 0, currency || Money.default_currency) },
    :converter => Proc.new { |value|  value.respond_to?(:to_money) ? value.to_money : raise(ArgumentError, "Can't convert #{value.class} to Money") }
  ...
end

How can I inherit now the currency from the parent (market) model?
I tried to override the currency method in the tradelimit model, but this doenst work..

Any suggestions? Thanks!

EDIT:

Or is there a way (without the composed_of feature) to provide this functionality?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

剑心龙吟 2024-12-01 07:50:39

你有没有尝试过:

class Market < AR
  #use Money as a mixin
  include Money
...

end

#Now Tradelimit should inherit all from Market
class Tradelimit <  Market

Have you tried:

class Market < AR
  #use Money as a mixin
  include Money
...

end

#Now Tradelimit should inherit all from Market
class Tradelimit <  Market
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文