Simply Relaxed Rails plugin

Well, I swear I don’t sit around all day writing plugins. I just happened to finalize a couple of these things today. :)

What is Simply Relaxed?? Simply Relaxed converts your models into a RELAX NG schema. Not much more to say.

Installation

  script/plugin discover

  # Say yes to http://svn.superruby.com/svn/plugins

  script/plugin install simply_relaxed

Examples

1
2
3
4
5
6
7
8
9
10
class OrdersController < ApplicationController
  def index
        @orders = Order.find(:all)

    respond_to do |wants|
      wants.xml { render :xml => @orders.to_xml }
      wants.rng { render :xml => Order::to_rng }
    end
  end
end
URL: http://localhost:3000/orders.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?xml version="1.0" encoding="UTF-8"?>
<orders>
  <order>
    <id type="integer">1</id>
    <placed-at type="datetime">2006-09-13T09:35:40-07:00</placed-at>
    <status>ACTIVE</status>
    <total-amount type="decimal">150.25</total-amount>
    <total-double type="float">150.25</total-double>
    <total-float type="float">150.25</total-float>
    <user-id type="integer"></user-id>
  </order>
  <order>
    <id type="integer">2</id>
    <placed-at type="datetime"></placed-at>
    <status>PENDING</status>
    <total-amount type="decimal">0.0</total-amount>
    <total-double type="float"></total-double>
    <total-float type="float"></total-float>
    <user-id type="integer"></user-id>
  </order>
</orders>
URL: http://localhost:3000/orders.rng
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<?xml version="1.0" encoding="UTF-8"?>
<grammar datatypeLibrary="http://www.w3.org/2001/XMLSchema-datatypes" 
         xmlns="http://relaxng.org/ns/structure/1.0">
  <start>
    <choice>
      <element name="orders">
        <zeroOrMore>
          <ref name="order"/>
        </zeroOrMore>
      </element>
      <ref name="order"/>
    </choice>
  </start>
  <define name="order">
    <element name="order">
      <zeroOrMore>
        <choice>
          <element name="placed-at">
            <attribute name="type"/>
            <optional>
              <data type="dateTime"/>
            </optional>
          </element>
          <element name="status">
            <optional>
              <text/>
            </optional>
          </element>
          <element name="total-amount">
            <attribute name="type"/>
            <optional>
              <data type="decimal"/>
            </optional>
          </element>
          <element name="total-double">
            <attribute name="type"/>
            <optional>
              <data type="float"/>
            </optional>
          </element>
          <element name="total-float">
            <attribute name="type"/>
            <optional>
              <data type="float"/>
            </optional>
          </element>
          <element name="user-id">
            <attribute name="type"/>
            <optional>
              <data type="integer"/>
            </optional>
          </element>
        </choice>
      </zeroOrMore>
    </element>
  </define>
</grammar>

2 Responses to “Simply Relaxed Rails plugin”

  1. Mark Lussier Says:
    If you dont mind, I wouldnt mind doing an XSD generating variant. I need XSD for use with the Cast Iron appliance which doesnt support RNG
  2. brasten Says:
    Mark- Go for it! I think XSD support would be great! It might be easiest to just start that from scratch, but you're welcome to grab the code from http://svn.superruby.com/svn/plugins/simply_relaxed. When you're done, either package your own plugin, or send me a patch if you want to roll it in to Simply Relaxed.

Leave a Reply