Rails+Mongodb之non browser app之敏捷开发

第一个问题:Rails程序是否需要nonbrowser app?

这个答案是肯定的,当你的Rails程序上线之后,象备份你的数据库这样后台操纵表格等需求一定会提上日程,这也是为什么Rails的经典指南<Agile Web Development With Rails>其最新第四版加了一章介绍如何开发non browser app,可惜的是,它介绍的内容是基于active record,不适用于mongodb,另外还有一个问题,这本书介绍的方法明显比较落伍,并不是 Agile Way!

第二个问题:怎么Agile开发nonbrowser app?

本文介绍的就是如何敏捷开发Rails+Mongodb的nonbrowser app

1、创建一个目录utils

mkdir utils

2、创建一个文件util_helper.rb,内容如下:

ENV["RAILS_ENV"] ||= 'development'

require File.expand_path("../../config/environment", __FILE__)

 

# Requires supporting ruby files with custom matchers and macros, etc,

# in spec/support/ and its subdirectories.

Dir[Rails.root.join("utils/support/**/*.rb")].each {|f| require f}

 

MyApp::Application.configure do |config|

end

其中ENV["RAILS_ENV"] ||= 'development'表示我们对development数据库操作

require File.expand_path("http://www.cnblogs.com/config/environment", __FILE__)这一行特别重要,就是它来让我们的APP具有和Rails Server一样的环境,包括数据库参数

Dir[Rails.root.join("utils/support/**/*.rb")].each {|f| require f}这一行是为将来作准备,如果有自定义的参数设置,可以放在support目录下

3、然后创建一个文件countposts.rb,其中Post是一个model,可以直接引用。

require File.expand_path("../util_helper", __FILE__)

puts Post.find(:all).size

4、OK,运行ruby countposts.rb,得到结果如:10

总结:实现敏捷nonbrowser app的要点:与APP共同环境变量、参数;可直接引用MVC的Class

原文地址:https://www.cnblogs.com/mobileinternet/p/2542948.html