Coder Social home page Coder Social logo

turtles's Introduction

Turtles ! (All the way down)

Get rid of the tired old (obj && obj.method) syntax. Don't let the fact that your
objects use semi-structured data like XML or nested hashes clutter your code with
conditionality. Increase performance by pruning garbage-creating intermediate 
evaluations where possible. 

Why the name turtles ? 
See the old-world story here: http://en.wikipedia.org/wiki/Turtles_all_the_way_down

The main use case:

Turtles, copied originally from some site I currently don't have the URL for, 
returns nil instead of raising MethodMissing for objects that are in the (configurable)
turtle scope. In addition it tracks a call chain of turtles, allowing you
to turn obtain the string "m1.m2.m0" from a call chain of obj.m1.m2.m0, none 
of which were methods defined on the original object, and decide to process the 
chain all at once (by creating an xpath if you like) instead of incrementally.

For classes that wrap semi-structured data, like XML-backed classes, the Turtles
paradigm lets you defer the evaluation of a call of methods until the end, at a 
minimum of overhead at each intermediate step.

Thus a query over an object backed by XML of :
<a>
  <b>
    <c d="val"/>
  </b>
</a>

may be represented unconditionally as

the_val = obj.a.b.c.d

even in cases where the intermediate node b may be absent, in which case nil is returned
without error. Your object can recieve the chain [:a, :b, :c, :d] as an argument to one

the_val = obj.a.b.c.d.eval_turtles! # Calls obj.turtle_eval( [:a, :b, :c, :d] )

In which you may resolve to an xpath for an all-at-once query, or do whatever you like.
The name is reconfigurable at the class level.

The scoping options are: for a class

  class Foo
    include Turtles
  end

  f = Foo.new
  f.m0.m1.m2  # nil, does not raise, even in subclasses of Foo

For the entire program (be careful if using this, its like swatting a fly with
a sledgehammer):

  include Turtles # in the global scope eg Object or Kernel

Within a block:

  with_turtles{ 
    obj.m0.m1.m2  # nil, does not raise
  }


To define a different method to be the reciever of the turtle chain when eval_turtles! is called:

class Obj
  include Turtles
  self.turtle_evaluator = :xpath_query

  def xpath_query( chain ); self.query( '//' + chain.map(&:to_s).join('/') ) ; end
end

(o = Obj.new).foo.goo.eval_turtles! # calls o.xpath_query( [:foo, :goo] )

Note: Once a single class in your process has done an include Turtles, the NilClass
will be turtled throughout the process. The way around this is to explicitly use
with_turtles blocks around code where you want turtling behavior to occur. 
(This should move to thread-local turtle settings)

See the spec file for other use cases.

And enjoy turtling ! 

http://www.youtube.com/watch?v=ezAJeaCySV4 - Dana Carvey
http://www.youtube.com/watch?v=CMNry4PE93Y - I like turtles kid

Dean


turtles's People

Stargazers

Dean Radcliffe avatar

Watchers

Dean Radcliffe avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.