使用独立的代码扩展Ruby类 [英] Extending a Ruby class with a standalone piece of code

查看:85
本文介绍了使用独立的代码扩展Ruby类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个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

显然它不工作有两个原因:


  1. CommonItem 不知道我调用的类方法。

  2. CommonItem 中查找> WIDTH HEIGHT

我尝试使用 而不是 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


I have a Rails app with several models with the same structure:

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

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:

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

Obviously it doesn't work for two reasons:

  1. CommonItem has no idea about class methods I invoke.
  2. WIDTH and HEIGHT constants are looked up in CommonItem instead of Item1.

I tried to use include instead of extend, some ways of class_eval and class inheritance, but none work.

It seems I am missing something obvious. Please tell me what.

解决方案

Here's how I would do it:

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

The included method is called on a module when it's included.

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 Model class.

Output:

100
100
model_method
200
200
model_method

这篇关于使用独立的代码扩展Ruby类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆