`
Goldice
  • 浏览: 104597 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

<<Agile DSL Development in Ruby>> 笔记

阅读更多

pdf见:http://obiefernandez.com/presentations/obie_fernandez-agile_dsl_development_in_ruby.pdf

 

1. What is DSL

——designed for a specific domain

——captures jargon in executable form

——can be internal or external

 

2. How to design Ruby DSL (1)

——Don’t try to do an abstract metamodel first (不要一开始就尝试建立抽象的元模型,元模型即模型的模型)

——Capture your DSL concepts in valid Ruby syntax, but don’t worry about implementation(用正确的Ruby语法来标识你的DSL概念,但不需要担心实现)

——Iterate over your Ruby DSL syntax until authors agree that it faithfully represents the domain, then work on the implementation(迭代改进你的Ruby DSL语法,直到业务专家认为语法足够真实代表这个领域,然后再考虑实现)

 

3. 

Let the DSL you devise guide your implementation

——Kind of like TDD, don’t do more than what you need to make your DSL execute correctly

(让你设计的DSL引导你的实现。某种程度上就像TDD,不要做比你需要的更多的事情来让你的DSL正确运行)

 

DSLs that reflect business documents such as contracts are great

——Designing a DSL that’s as close as possible to the document it reflects makes verification of the system much easier!

(反应商业文档比如合同的DSL是最理想的。设计一种尽可能与领域文档接近的语言可以让系统的证明更为容易)


4. How to design Ruby DSL (2) —— Agile DSL Development

——Start with short iterations over the design(一开始,针对设计做短周期的迭代)

——Incorporate end-user feedback, pair with them if possible(吸取终端用户的反馈,如果可能的话与他们结对)

——Do TDD your context cod(利用TDD来写代码)

——Do refactor your context code often, but avoid over-engineering it(经常重构你的代码,但不要过度)

 

5. DSL的一些语法技巧

——Optional parentheses(可选的括号)

——Symbol(符号)

——Block(块)

——Literal arrays and hashes

——Variable-length argument(可变长的参数)

 

6. “the trick to writing DSL’s in Ruby is really knowing what you can and can’t do with Ruby’s metaprogramming features —— Jamis Buck, 37signals

(用Ruby编写DSL的技巧在于真正了解使用Ruby的元编程特征你可以做什么,不可以做什么)

 

7. Different types of Ruby DSL designs

(1) Instantiation——Your DSL is simply methods on an object

      其实这种DSL就是方法调用,只不过方法的参数更加灵活,利用了Ruby的一些tricks

(2) Class Macros——DSL as methods on some ancestor class, and subclasses can then use those methods to tweak the behavior of themselves and their subclasses

      这个可以参见我以前写过的关于Programming Ruby中Metaprogramming的章节,Marcos就是指通过这种方式可以将某些简短的名次扩展成更大的东西。

(3) Top-Level Methods——Your application defines the DSL as top-level methods, and then invokes load with the path to your DSL script.When those methods are called in the configuration file, they  modify some central (typically global) data, which your application uses to determine how it should execute.

      你直接在脚本里定义方法,这个方法就会被注册到Object类中,因为你调用脚本的是Object类的上下文,所以这些方法在任何调用的脚本中都可用。

(4) Sandboxing——Your DSL is defined as methods of some object, but that object is really just a “sandbox”. Interacting with the object’s methods modify some state in the sandbox, which is then queried by the application

 

8. Ruby Features used by DSL implementors

 

 

• Symbols, less noisy than strings

• Blocks, enabling delayed evaluation of code

• Modules, for cleaner separation of code

• Splats, for handling parameter arrays

• eval, instance_eval, and class_eval

• define_method and alias_method

 

9. It’s a different way of thinking about writing code, and as such needs to be learned by doing, not by reading. Experimentation is the key!

1
2
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics