使用独立的代码扩展Ruby类 [英] Extending a Ruby class with a standalone piece of code
问题描述
我有一个Rails应用程序与几个模型具有相同的结构:
class Item1< ActiveRecord :: Base
WIDTH = 100
HEIGHT = 100
has_attached_file:image,styles:{original:#{WIDTH} x#{HEIGHT}}
validates_attachment:image, :presence => true
end
class Item2< ActiveRecord :: Base
WIDTH = 200
HEIGHT = 200
has_attached_file:image,styles:{original:#{WIDTH} x#{HEIGHT}}
validates_attachment:image, :presence => true
end
实际代码比较复杂, >
我想我可以将代码的公共部分放在一个地方,然后在所有模型中使用。
是我想到的:
class Item1< ActiveRecord :: Base
WIDTH = 100
HEIGHT = 100
extend CommonItem
end
模块CommonItem
has_attached_file:image,styles: original:#{WIDTH} x#{HEIGHT}}
validates_attachment:image,:presence => true
end
显然它不工作有两个原因:
-
CommonItem
不知道我调用的类方法。 - CommonItem 中查找>
WIDTH
和HEIGHT
我尝试使用 看来我缺少一些明显的东西。请告诉我。 以下是我的操作方式: 管理创建一个类似的结构,你的问题有。该模块正在调用 输出: I have a Rails app with several models with the same structure: The actual code is more complicated but that's enough for simplicity. I think I can put the common part of the code in one place and then use it in all models. Here is what comes to my mind: Obviously it doesn't work for two reasons: I tried to use It seems I am missing something obvious. Please tell me what. Here's how I would do it: The I think I've managed to create a similar structure that your problem has. The module is calling the method inherited by the items classes from Output:
这篇关于使用独立的代码扩展Ruby类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
而不是
extend
, class_eval
和类继承的一些方法, / p>
class Model
def self.model_method
putsmodel_method
end
end
模块Item
def self.included(base)
base.class_eval do
p base :: WIDTH,base :: HEIGHT
model_method
end
end
end
class Item1<模型
WIDTH = 100
HEIGHT = 100
include item
end
class Item2<模型
WIDTH = 200
HEIGHT = 200
包括项目
end
包含
方法在包含模块时被调用。
Model
类中的items类继承的方法。
100
100
model_method
200
200
model_method
class Item1 < ActiveRecord::Base
WIDTH = 100
HEIGHT = 100
has_attached_file :image, styles: {original: "#{WIDTH}x#{HEIGHT}"}
validates_attachment :image, :presence => true
end
class Item2 < ActiveRecord::Base
WIDTH = 200
HEIGHT = 200
has_attached_file :image, styles: {original: "#{WIDTH}x#{HEIGHT}"}
validates_attachment :image, :presence => true
end
class Item1 < ActiveRecord::Base
WIDTH = 100
HEIGHT = 100
extend CommonItem
end
module CommonItem
has_attached_file :image, styles: {original: "#{WIDTH}x#{HEIGHT}"}
validates_attachment :image, :presence => true
end
CommonItem
has no idea about class methods I invoke.WIDTH
and HEIGHT
constants are looked up in CommonItem
instead of Item1
.include
instead of extend
, some ways of class_eval
and class inheritance, but none work.class Model
def self.model_method
puts "model_method"
end
end
module Item
def self.included(base)
base.class_eval do
p base::WIDTH, base::HEIGHT
model_method
end
end
end
class Item1 < Model
WIDTH = 100
HEIGHT = 100
include Item
end
class Item2 < Model
WIDTH = 200
HEIGHT = 200
include Item
end
included
method is called on a module when it's included.Model
class.100
100
model_method
200
200
model_method