lispy 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +83 -0
- data/lib/lispy.rb +2 -2
- metadata +4 -3
data/README.md
ADDED
@@ -0,0 +1,83 @@
|
|
1
|
+
# LISPY
|
2
|
+
|
3
|
+
Yeah, right. Get a room, you two.
|
4
|
+
|
5
|
+
## The backstory.
|
6
|
+
|
7
|
+
I've written a number of Ruby libraries in my time. I've used many, many more. Wait, let's back up a bit.
|
8
|
+
|
9
|
+
## Assumptions.
|
10
|
+
|
11
|
+
* People love libraries like Sinatra and ActiveRecord and Babushka and AASM and (name your favourite Ruby library).
|
12
|
+
* What they love about these is the small amount of Ruby syntax required to get a whole lot done.
|
13
|
+
* They love that they can contextualise their definitions (that make the libraries do things) with natural Ruby syntax such as blocks.
|
14
|
+
* To enable these kinds of libraries the use of so-called 'meta-programming' constructs are required (instance_eval, instance_exec).
|
15
|
+
* Clever but inexperienced Ruby programmers often make very useful libraries with wonderful APIs but horrible internals (my hand is up, see: Workflow).
|
16
|
+
|
17
|
+
## Goals.
|
18
|
+
|
19
|
+
* Decouple the 'pretty Ruby junk' from the (hopefully) clean and explicit implementation that makes the library do it's job.
|
20
|
+
* Allow library authors to dream up APIs and implement them without entering Ruby metaprogramming hell (it is a real place).
|
21
|
+
|
22
|
+
## Examples.
|
23
|
+
|
24
|
+
Take the following example:
|
25
|
+
|
26
|
+
Lispy.new.to_data do
|
27
|
+
fart 1
|
28
|
+
fart 2
|
29
|
+
fart 3, 4
|
30
|
+
fart
|
31
|
+
fart :where => :in_bed
|
32
|
+
end
|
33
|
+
|
34
|
+
This will return `[[:fart, 1], [:fart, 2], [:fart, [3, 4]], [:fart, []], [:fart, {:where => :in_bed}]]`. It looks somewhat like an abstract syntax tree (I think). Let's take an example with nesting:
|
35
|
+
|
36
|
+
Lispy.new.to_data do
|
37
|
+
todo_list do
|
38
|
+
item :priority => :high do
|
39
|
+
desc 'Take out the trash, it stinks.'
|
40
|
+
end
|
41
|
+
item :priority => :normal do
|
42
|
+
desc 'Walk the dog.'
|
43
|
+
end
|
44
|
+
item :priority => :normal do
|
45
|
+
desc 'Feed the cat.'
|
46
|
+
end
|
47
|
+
end
|
48
|
+
interested_parties do
|
49
|
+
person 'Me.'
|
50
|
+
person 'You.'
|
51
|
+
person '...'
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
The pretty printed output is this, but before you continue, I ask you to take a deep breath, and remember that you are a programmer and that you have been writing Ruby for too long to be able to look at anything else than ActiveRecord declarations and say something along the lines of "WTF IS THIS, IT IS BAD CODE OMFG I AM TELLING!!!1". Yes, once upon a time you were able to think as a programmer and once you had patience! Now, that output:
|
56
|
+
|
57
|
+
[[:todo_list, []],
|
58
|
+
[[:item, {:priority=>:high}],
|
59
|
+
[[:desc, "Take out the trash, it stinks."]],
|
60
|
+
[:item, {:priority=>:normal}],
|
61
|
+
[[:desc, "Walk the dog."]],
|
62
|
+
[:item, {:priority=>:normal}],
|
63
|
+
[[:desc, "Feed the cat."]]],
|
64
|
+
[:interested_parties, []],
|
65
|
+
[[:person, "Me."], [:person, "You."], [:person, "..."]]]
|
66
|
+
|
67
|
+
That last example, I just wrote off the top of my head. You see how you can start with an API design and worry about implementation later? If you haven't tried to make a 'nice' Ruby library before, this wont be self evident, but for those that have, and have struggled through all the madness, you'll see what I mean. Once you've made up your API, all you have to do is write the translation library that does whatever that needs to be done. If that is piping out to a shell, writing a configuration file or constructing an object graph, then well, you're covered aren't you?
|
68
|
+
|
69
|
+
Your crazy so-called 'DSL' is now decoupled from your wonderfully explicitly programmed library, I hope. Because I might have to modify it one day and man, if I see a rats nest of instance_execs and auto-generated classes I might just... write you a nasty letter. With angry faces like this: >:|
|
70
|
+
|
71
|
+
## Now what?
|
72
|
+
|
73
|
+
If you have any questions about this, or how I'm using it for real-world problems (I am!), please get in touch. Pete won't talk to me about this, and I bought his car and everything!!!
|
74
|
+
|
75
|
+
## Why Lispy?
|
76
|
+
|
77
|
+
To my mind, this whole API fandangle we've gotten ourselves into is due to Ruby having a nice enough syntax to describe APIs in a way that is easy to understand, but Ruby itself wasn't designed with this usage in mind. When I see lines like `has_one :brain` and `dep 'nginx' do` it reminds me of a feature of LISP, which is code as data. Ruby doesn't have this code as data facility but I reckon if it did, we'd have cleaner library implementations with it's idomatic syntax.
|
78
|
+
|
79
|
+
Saying that, I've written about 10 lines of LISP in my life. Pete keeps telling me to go read SICP and I keep meaning to but then I end up at the local bar listening to some rock band and I'm like "crap, it's 3am". And even then, I wont get to use LISP at work because I don't think anyone in our company except for Pete has actually read SICP.
|
80
|
+
|
81
|
+
## License.
|
82
|
+
|
83
|
+
Released under the MIT license (see MIT-LICENSE). GPL can go fart itself, seriously. I like open source, but I also like money.
|
data/lib/lispy.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
class Lispy
|
2
|
-
VERSION = '0.0.
|
2
|
+
VERSION = '0.0.4'
|
3
3
|
|
4
4
|
@@methods_to_keep = /^__/, /class/, /instance_/, /method_missing/, /object_id/
|
5
5
|
|
@@ -12,7 +12,7 @@ class Lispy
|
|
12
12
|
@scope.last << [sym, args]
|
13
13
|
if block
|
14
14
|
# there is some simpler recursive way of doing this, will fix it shortly
|
15
|
-
|
15
|
+
scope.last.last << []
|
16
16
|
@scope.push(@scope.last.last.last)
|
17
17
|
instance_exec(&block)
|
18
18
|
@scope.pop
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
version: 0.0.
|
8
|
+
- 4
|
9
|
+
version: 0.0.4
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Ryan Allen
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2011-04-
|
17
|
+
date: 2011-04-16 00:00:00 +10:00
|
18
18
|
default_executable:
|
19
19
|
dependencies: []
|
20
20
|
|
@@ -31,6 +31,7 @@ files:
|
|
31
31
|
- .gitignore
|
32
32
|
- Gemfile
|
33
33
|
- MIT-LICENSE
|
34
|
+
- README.md
|
34
35
|
- Rakefile
|
35
36
|
- lib/lispy.rb
|
36
37
|
- lispy.gemspec
|