我正在使用mongoid和rails 3,并且最近遇到了一个非常棘手的问题,我需要一个建议.
我正在研究CMS,其中一个想法是CMS将提供一些基本的模型定义,如果需要,最终用户可以使用自己的定义和控件扩展基本类,并将它们保存在不同的集合(表)中.
class DcPage
include Mongoid::Document
field a ....
belongs_to b ....
validates a ....
end
class MyPage < DcPage
field c ....
validates c ....
end
Run Code Online (Sandbox Code Playgroud)
直到最后一个版本的mongoid这个工作(很少黑客),数据将保存到my_pages集合.由于某些问题,mongoid不再支持此行为,数据始终保存到dc_pages集合.
在解释我的问题时,mongoid团队建议我使用ActiveSupport :: Concern并为我提供了一个示例.如果扩展类在同一源文件中定义,那么它的工作正常.顺便说一句.从未发生在实践中.
module CommonBehaviour
extend ActiveSupport::Concern
included do
field :subject, type: String, default: ''
# ...
end
end
class DcPage
include Mongoid::Document
include CommonBehaviour
end
class MyPage
include Mongoid::Document
include CommonBehaviour
end
Run Code Online (Sandbox Code Playgroud)
到目前为止,我发现如果我在第二个文件中需要基本源文件,它可以工作.看起来像这样:require'/some/path/to/my/gem/app/models/dc_page.rb
你现在能看到我的痛苦吗?基本源文件当然会被备份到gem中,因此成为一个移动目标.
请帮我更好的解决方案.
由TheR
我有一个 ID REST API,我需要对其进行扩展以支持多个(最多 10K)ID。基本上是在所有相关 ID 上运行更新,而不是在网络中发送 10Ks 请求。
当前端点:
@POST
@Path("{id}/update")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public ResponseVO updateBlockReason(@PathParam("id") int id, List<RequestVo> requestVo) {
Run Code Online (Sandbox Code Playgroud)
建议的一种选择是以逗号分隔的值作为stackexchange 的 answer-by-ids
/answers/{ids} GET 的使用
{ids} 最多可以包含 100 个分号分隔的 ID。要以编程方式查找 id,请在答案对象上查找 answer_id。
这是类似答案的情况
http://our.api.com/Product/<id1>,<id2>:as James 建议可以作为一个选项,因为 Product 标签之后是一个参数
但这对我来说似乎很尴尬,并且RequestVo对于所有 ID 都是一样的(目前很好,但以后添加此类支持会更难)
看来我需要将 Path 变量更改为将其添加到 RequestVO 中
这意味着 Id 将是一个 JSON 密钥,例如
[{
"id" : "1",
"name": "myAttribute"
"toggle": true
},
{
"id" : "2",
"name": "mySecondAttribute"
"toggle": false …Run Code Online (Sandbox Code Playgroud) 我正在尝试为GDB编写一个简单的python扩展,只要遇到断点就会输出到文件.根据文档,"gdb.Breakpoint类可以被分类"(参见http://sourceware.org/gdb/onlinedocs/gdb/Breakpoints-In-Python.html)
但是,当我尝试以下代码时,我得到错误"TypeError:调用元类库时出错.类型'gdb.Breakpoint'不是可接受的基类型"
class MyBreakpoint(gdb.Breakpoint):
def stop (self):
print "break"
return False
Run Code Online (Sandbox Code Playgroud)
我正在运行Ubuntu 11.04和gdb 7.2.任何帮助或链接到更好的文档将不胜感激.谢谢!
我的确切步骤:
$ gdb
GNU gdb (Ubuntu/Linaro 7.2-1ubuntu11) 7.2
Copyright (C) 2010 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "i686-linux-gnu".
For bug reporting instructions, …Run Code Online (Sandbox Code Playgroud) 我想向 puppeteer.Page 对象添加自定义方法,这样我就可以像这样调用它们:
let page = await browser.newPage();
page.myNewCustomMethod();
Run Code Online (Sandbox Code Playgroud)
这是我创建的众多自定义方法之一。它使用表达式数组通过 XPath 表达式找到第一个可用元素:
const findAnyByXPath = async function (page: puppeteer.Page, expressions: string[]) {
for (const exp of expressions) {
const elements = await page.$x(exp);
if (elements.length) {
return elements[0];
}
}
return null;
}
Run Code Online (Sandbox Code Playgroud)
我必须像这样调用它......
let element = await findAnyByXPath(page, arrayOfExpressions);
Run Code Online (Sandbox Code Playgroud)
对我来说,这在编辑器中看起来很奇怪,尤其是在调用许多自定义方法的区域中。在我看来,有点“断章取义”。所以我宁愿这样调用它:
page.findAnyByXPath(arrayOfExpressions);
Run Code Online (Sandbox Code Playgroud)
我知道有一个page.exposeFunction方法,但这不是我要找的。
有什么方法可以实现这一目标?
我正在尝试在抽象类中测试私有方法.
我有三个抽象类:
abstract class AbstractClass1 extends AbstractClass2
{
private function _privateFunction()
{
//method's body
}
}
abstract class AbstractClass2 extends AbstractClass3
{
public function __construct($param)
{
parent::__construct($param)
}
}
abstract class AbstractClass3
{
public function __construct($param = array())
{
//something
}
}
Run Code Online (Sandbox Code Playgroud)
测试类:
class AbstractClass1Test extends PHPUnit_Framework_TestCase
{
public function test_privateFunction()
{
$stub = $this->getMockForAbstractClass("AbstractClass1");
$class = new ReflectionClass($stub);
$method = $class->getMethod("_privateFunction");
$method->setAccessible(true);
//some assertings with $method->invoke($stub)
}
}
Run Code Online (Sandbox Code Playgroud)
测试失败,因为错误:
缺少AbstractClass2 :: __ construct()的参数1,在第190行的/usr/share/php/PHPUnit/Framework/MockObject/Generator.php中调用并定义
AbstractClass2.php
public function __construct($param)
Run Code Online (Sandbox Code Playgroud)
AbstractClass1.php
$classMock …Run Code Online (Sandbox Code Playgroud) 我试图创建一个新的运算符:?在列表上,其操作与::相同,但如果值为null,则返回原始列表.我写了以下内容,但很快就知道我不知道自己在做什么......
object ImplicitList {
implicit def extendIterator[T](i : List[T]) = new ListExtension(i)
}
class ListExtension[T <: Any](i : List[T]) {
def :?[B >: T] (x: B): List[B] = if (x != null) x :: i else i
}
final case class :?[B](private val hd: B, private val tl: ListExtension[B]) extends ListExtension[B](tl.:?(hd))
Run Code Online (Sandbox Code Playgroud) 我正在尝试扩展一个Abstract对象.
var Abstract = function() { code = 'Abstract'; };
Abstract.prototype.getCode = function() { return code; };
Abstract.prototype.getC = function() { return c; };
var ItemA = function() { code = 'ItemA'; c = 'a'; };
ItemA.prototype = Object.create(Abstract.prototype);
ItemA.prototype.constructor = ItemA;
var ItemB = function() { code = 'ItemB'; };
ItemB.prototype = Object.create(Abstract.prototype);
ItemB.prototype.constructor = ItemB;
var b = new ItemB();
console.log(b.getCode());
var a = new ItemA();
console.log(b.getCode());
console.log(b.getC());
Run Code Online (Sandbox Code Playgroud)
结果:
ItemB
ItemA
a
Run Code Online (Sandbox Code Playgroud)
我在ItemB实例中获得ItemA的范围有什么特别的原因吗?我该如何解决?
我想添加一个静态字符串属性,该属性将跟踪当前正在运行的测试的名称。我认为解决此问题的最佳方法是使用WebDriver,因为它是唯一承载在我所有页面对象中的对象。
有没有一种方法可以扩展WebDriver类以添加可以设置的字符串属性?
编辑:由于WebDriver使用IWebDriver接口而不是我可能扩展该接口?
编辑#2:添加当前我必须加载WebDriver的示例:
protected static NLog.Logger _logger = LogManager.GetCurrentClassLogger();
protected static IWebDriver _driver;
/// <summary>
/// Spins up an instance of FireFox webdriver which controls the browser using a
/// FireFox plugin using a stripped down FireFox Profile.
/// </summary>
protected static void LoadDriver()
{
ChromeOptions options = new ChromeOptions();
try
{
var profile = new FirefoxProfile();
profile.SetPreference("browser.helperApps.neverAsk.saveToDisk", "application/octet-stream doc xls pdf txt");
_driver = new FirefoxDriver(profile);
_driver.Navigate().GoToUrl("http://portal.test-web01.lbmx.com/login?redirect=%2f");
}
catch(Exception e)
{
Console.WriteLine(e.Message);
throw;
}
}
Run Code Online (Sandbox Code Playgroud) 我有自己的onShow()函数对象,在显示对象时调用(由我).我可以通过赋值覆盖此函数
o.onShow = function() { // something };
Run Code Online (Sandbox Code Playgroud)
但它删除了以前版本的函数.所以,如果我希望保留现有的处理程序,我应该缓存它并调用新的处理程序:
o.oldOnShow = o.onShow;
o.onShow = function() { this.oldOnShow(); // something };
Run Code Online (Sandbox Code Playgroud)
但是这样我只能缓存一个先前的处理程序.怎么会有很多呢?
有没有方便的方法来完成这项任务?如何在文献中命名这项任务?在jQuery中有这样的方法吗?
有谁知道如何扩展FOSUserBundle中的Mailer类?
我正在实施一个非常基本的父母电子邮件检查(所有验证都在表单上完成,以强制输入父母的电子邮件),如果在用户实体上填充了父电子邮件字段,那么它应该将电子邮件发送到该地址而不是用户的电子邮件.
到目前为止,我已尝试过以下内容:
namespace SSERugby\UserBundle\Mailer;
use FOS\UserBundle\Mailer\Mailer as BaseMailer;
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
use Symfony\Component\Routing\RouterInterface;
class Mailer extends BaseMailer
{
public function sendConfirmationEmailMessage(UserInterface $user)
{
$email = $user->getEmail();
$parentEmail = $user->getParentEmail();
if(isset($parentEmail)&&(trim($parentEmail)!='')){
$email = $parentEmail;
}
$template = $this->parameters['confirmation.template'];
$url = $this->router->generate('fos_user_registration_confirm', array('token' => $user->getConfirmationToken()), true);
$rendered = $this->templating->render($template, array(
'user' => $user,
'confirmationUrl' => $url
));
$this->sendEmailMessage($rendered, $this->parameters['from_email']['confirmation'], $email);
}
}
Run Code Online (Sandbox Code Playgroud)
它似乎只是忽略被覆盖的类并使用默认值,我已经在测试之前清除了缓存.
谢谢
extending ×10
javascript ×3
c# ×1
class ×1
coalesce ×1
function ×1
gdb ×1
global ×1
java ×1
jquery ×1
json ×1
list ×1
methods ×1
model ×1
mongoid ×1
php ×1
phpunit ×1
prototype ×1
puppeteer ×1
python ×1
rest ×1
scala ×1
scripting ×1
selenium ×1
symfony ×1
typescript ×1
unit-testing ×1
variables ×1
webdriver ×1