我正在使用带引导程序3的simple_form gem.我想要一个提交按钮的包装器
现在它将HTML显示为
<form id="new_order" ...>
...
<input class="btn btn-primary" type="submit" value="Save" name="commit">
</form>
Run Code Online (Sandbox Code Playgroud)
我想写一个包装器,所以HTML将是:
<form id="new_order" ...>
...
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<input class="btn btn-primary" type="submit" value="Save" name="commit">
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
我到目前为止得到了这个:
应用程序/初始化/ simple_form_bootstrap.rb:
options[:wrapper] = :horizontal_form
options[:wrapper_mappings] = {
check_boxes: :horizontal_radio_and_checkboxes,
radio_buttons: :horizontal_radio_and_checkboxes,
file: :horizontal_file_input,
boolean: :horizontal_boolean,
# what to write here??
# submit: :horizontal_submit_button
}
Run Code Online (Sandbox Code Playgroud)
这是我的包装:
config.wrappers :horizontal_submit_button, tag: 'div', class: 'form-group', error_class: 'has-error' do |b|
b.use :html5
b.use :placeholder
b.wrapper tag: 'div', class: 'col-sm-offset-2 col-sm-10' do …Run Code Online (Sandbox Code Playgroud) 我发现自己创建了大量的包装类,纯粹是因为我想模仿它的行为
DirectoryInfo或者WindowsIdentity)然后我发现自己附加了一个用'W'包装的类(表示它是一个包装器),所以我最终得到DirectoryInfoW(相反,DirectoryInfoWrapper它似乎相当冗长).同样,我最终使用被称为包装的本机方法NativeMethods.DuplicateTokenW.
在命名包装类时,遵循什么是一个很好的经验法则?
我有一些类没有实现某个接口,但在结构上符合该接口.
interface IFoo {
void method();
}
class Bar { // does not implement IFoo
public void method() {...}
}
Run Code Online (Sandbox Code Playgroud)
现在,我可以围绕那些简单地委托给包装类的类编写一个包装器
class BarWrapper : IFoo {
Bar bar = new Bar();
public void method()
{
bar.method();
}
}
Run Code Online (Sandbox Code Playgroud)
但那是很多繁琐的工作.那些包装类可以自动生成吗?就像是:
IFoo foo = CreateWrapper<IFoo>(new Bar());
我相信你可以用Reflection.Emit做到这一点,但我从来没有用过它,乍一看它看起来并不容易.
有没有更简单的方法,或者是否有一个库已经实现了这个?
假设我有这个:
var wrap = $("#someId");
Run Code Online (Sandbox Code Playgroud)
我需要访问我会得到的原始对象
var orig = document.getElementById("someId");
Run Code Online (Sandbox Code Playgroud)
但我不想做document.getElementById.
有什么我可以wrap用来获得它吗?就像是:
var orig = wrap.original();
Run Code Online (Sandbox Code Playgroud)
我搜索得很高,但我找不到任何东西; 或许我不是在找正确的事.
我想将Pure Data用作我自己库的原型工具.我发现Pure Data补丁是用C语言编写的,但我的库是用C++编写的.那么如何在纯数据中使用此代码呢?由于我没有使用普通的C,我想知道如何编写C++类的C包装器以及如何实例化我的类呢?或者我必须重写C中的所有内容?
玩弄了这个,我怀疑它不可能,但我想我会问专家.我有以下C++代码:
class IInterface
{
virtual void SomeMethod() = 0;
};
class Object
{
IInterface* GetInterface() { ... }
};
class Container
{
private:
struct Item
{
Object* pObject;
[... other members ...]
};
std::list<Item> m_items;
};
我想将这些方法添加到Container:
MagicIterator<IInterface*> Begin();
MagicIterator<IInterface*> End();
为了让呼叫者可以写:
Container c = [...]
for (MagicIterator<IInterface*> i = c.Begin(); i != c.End(); i++)
{
IInterface* pItf = *i;
[...]
}
所以基本上我想提供一个类似乎迭代某些集合(其中Begin()和End()的调用者不允许看到)的IInterface指针,但它实际上迭代了指向其他指针的指针集合.对象(Container类专用),可以转换为IInterface指针.
几个关键点:
MagicIterator是在外面定义的Container.
Container::Item 必须保持私密.
MagicIterator有遍历IInterface,指针尽管事实Container持有std::list<Container::Item>.Container::Item包含一个 …我怎样才能编写一个可以包装任何函数的包装器,并且可以像函数本身一样调用它?
我需要这个的原因:我想要一个Timer对象,它可以包装一个函数,就像函数本身一样,加上它记录所有调用的累计时间.
场景看起来像这样:
// a function whose runtime should be logged
double foo(int x) {
// do something that takes some time ...
}
Timer timed_foo(&foo); // timed_foo is a wrapping fct obj
double a = timed_foo(3);
double b = timed_foo(2);
double c = timed_foo(5);
std::cout << "Elapsed: " << timed_foo.GetElapsedTime();
Run Code Online (Sandbox Code Playgroud)
我怎么写这Timer堂课?
我正在尝试这样的事情:
#include <tr1/functional>
using std::tr1::function;
template<class Function>
class Timer {
public:
Timer(Function& fct)
: fct_(fct) {}
??? operator()(???){
// call the fct_,
// measure runtime and …Run Code Online (Sandbox Code Playgroud) 我的Web应用程序目前已执行简单的查询:简单的CRUD操作,计数,...
几个月前,有人建议我为此编写一个简单的PDO包装器(以避免每次执行查询时都编写try/catch,prepare(),execute()等).显示了这个示例方法(我做了一些更改,所以我可以在我自己的项目中使用它):
public function execute() {
$args = func_get_args();
$query = array_shift($args);
$result = false;
try {
$res = $this->pdo->prepare($query);
$result = $res->execute($args);
} catch (PDOException $e) { echo $e->getMessage(); }
return $result;
}
Run Code Online (Sandbox Code Playgroud)
因为我需要执行更多操作(执行查询,检索1条记录,检索多条记录,计算结果),我为所有这些创建了一个方法:
public function getMultipleRecords() {
$args = func_get_args();
$query = array_shift($args);
$records = array();
try {
$res = $this->pdo->prepare($query);
$res->execute($args);
$records = $res->fetchAll();
} catch (PDOException $e) { echo $e->getMessage(); }
return $records;
}
public function getSingleRecord() {
$args = func_get_args();
$query = array_shift($args);
$record = …Run Code Online (Sandbox Code Playgroud) 我正在寻找一个剥离(所以不像squeryl,或任何创建类型安全的查询,我必须重新指定表只是为了得到一些数据)jdbc的包装器.
我正在使用scala 2.9和postgres.
有人在这里取得成功吗
考虑下面的代码捕捉.我们equals()用来比较对象是否有意义?这两个值都有意义相等,但为什么会longWrapper.equals(0)返回false?当我将两个值与==运算符进行比较时,它返回true.
Long longWrapper = 0L;
long longPrimitive = 0;
System.out.println(longWrapper == 0L); // true
System.out.println(longWrapper == 0); //true
System.out.println(longWrapper == longPrimitive); //true
System.out.println(longWrapper.equals(0L)); //true
System.out.println(longWrapper.equals(0)); //false
System.out.println(longWrapper.equals(longPrimitive)); //true
Run Code Online (Sandbox Code Playgroud)