Permalink
Browse files

Work in progress - starting to move towards a more XSD friendly approach

  • Loading branch information...
zilkey committed Jul 4, 2009
1 parent 6e90ea8 commit 56a0a92f1bbff893e7316de87bcf699f980cf39c
View
@@ -3,4 +3,5 @@
coverage
rdoc
pkg
-.idea
+.idea
+junk*
View
@@ -1,9 +1,18 @@
require 'rubygems'
require 'activesupport'
require 'nokogiri'
-require 'active_api/elements/base'
-require 'active_api/elements/collection'
-require 'active_api/elements/date'
+
+require 'active_api/xs/boolean'
+require 'active_api/xs/date'
+require 'active_api/xs/decimal'
+require 'active_api/xs/integer'
+require 'active_api/xs/string'
+require 'active_api/xs/time'
+
+require 'active_api/element/collection'
+require 'active_api/element/complex'
+require 'active_api/element/simple'
+
require 'active_api/courier'
require 'active_api/field'
require 'active_api/schema'
@@ -7,5 +7,13 @@ def initialize(options = {})
@node_name = options[:node_name]
end
+ def singular_node_name
+
+ end
+
+ def plural_node_name
+
+ end
+
end
end
@@ -1,16 +1,30 @@
module ActiveApi
class Definition
- attr_reader :class_symbol, :fields
- def initialize(class_symbol)
- @class_symbol = class_symbol
- @fields = []
+ attr_reader :name, :fields
+
+ def initialize(options)
+ @name = options[:name]
+ @fields = options[:fields] || []
+ end
+
+ def singular_name
+ name.to_s.singularize
+ end
+
+ def plural_name
+ name.to_s.pluralize
+ end
+
+ def string(name, options = {})
+ field options.merge(:name => name, :type => :string, :klass => Element::Simple)
+ end
+
+ def belongs_to(name, options = {})
+ field options.merge(:name => name, :type => :parent, :klass => Element::Complex)
end
- [:string, :date, :datetime, :reference, :collection].each do |method_name|
- define_method method_name do |name, *args|
- options = args.first || Hash.new
- field options.merge(:name => name, :type => method_name)
- end
+ def has_many(name, options = {})
+ field options.merge(:name => name, :type => :children, :klass => Element::Collection)
end
def field(options)
@@ -0,0 +1,24 @@
+module ActiveApi
+ module Element
+ class Collection
+
+ attr_reader :objects, :node
+
+ def initialize(objects, options)
+ @objects = objects
+ @node = options[:node]
+ end
+
+ def build_xml(builder = Nokogiri::XML::Builder.new)
+ builder.send node.to_s.pluralize do |xml|
+ objects.each do |object|
+ element = Complex.new object, :node => node
+ element.build_xml(xml)
+ end
+ end
+ builder
+ end
+
+ end
+ end
+end
@@ -0,0 +1,27 @@
+module ActiveApi
+ module Element
+ class Complex
+
+ attr_reader :object, :node, :name
+
+ def initialize(object, options = {})
+ @object = object
+ @node = options[:node]
+ @name = options[:name]
+ end
+
+ def build_xml(builder = Nokogiri::XML::Builder.new)
+ builder.send node.to_s.singularize do |xml|
+ definition = Schema.definitions.detect{|definition| definition.name == node}
+ definition.fields.each do |field|
+ puts field.inspect
+ element = field.klass.new object.send(field.name), :node => node, :name => name
+ element.build_xml(xml)
+ end
+ end
+ builder
+ end
+
+ end
+ end
+end
@@ -0,0 +1,17 @@
+module ActiveApi
+ module Element
+ class Simple
+
+ attr_reader :text, :node
+ def initialize(text, options)
+ @text = text
+ @node = options[:node]
+ end
+
+ def build_xml(builder)
+ builder.send node, text
+ end
+
+ end
+ end
+end
@@ -1,14 +0,0 @@
-module ActiveApi
- module Elements
- class Date < Base
-
- protected
-
- def object_value
- value = object.send(options[:field])
- value.present? ? value.xmlschema : nil
- end
-
- end
- end
-end
View
@@ -1,9 +1,10 @@
module ActiveApi
class Field
- attr_reader :type, :name
+ attr_reader :type, :name, :klass
def initialize(options = {})
- @type = options[:type]
- @name = options[:name]
+ @type = options[:type]
+ @name = options[:name]
+ @klass = options[:klass]
end
end
end
View
@@ -1,10 +1,26 @@
+# define :article do |t|
+# t.parent :author
+# t.string :title
+# t.children :comments
+# end
+#
+# define :author do |t|
+# t.string :name
+# t.children :articles
+# end
+#
+# define :comment do |t|
+# t.parent :article
+# t.parent :user
+# t.string :text
+# end
module ActiveApi
class Schema
class_inheritable_array :definitions
class << self
- def define(class_symbol)
- definition = Definition.new(class_symbol)
+ def define(name)
+ definition = Definition.new(:name => name)
yield definition
write_inheritable_array :definitions, [definition]
end
@@ -0,0 +1 @@
+#uri escape with rack?
@@ -0,0 +1,11 @@
+module ActiveApi
+ module Xs
+ class Boolean
+ class << self
+ def format(value)
+ value.to_s
+ end
+ end
+ end
+ end
+end
View
@@ -0,0 +1,15 @@
+module ActiveApi
+ module Xs
+ class Date
+ class << self
+ #YYYY-MM-DD
+ #2002-09-24Z
+ #2002-09-24-06:00
+ #2002-09-24+06:00
+ def format(value)
+ value.strftime("%Y")
+ end
+ end
+ end
+ end
+end
@@ -0,0 +1,22 @@
+#date Defines a date value
+#dateTime Defines a date and time value
+#duration Defines a time interval
+#gDay Defines a part of a date - the day (DD)
+#gMonth Defines a part of a date - the month (MM)
+#gMonthDay Defines a part of a date - the month and day (MM-DD)
+#gYear Defines a part of a date - the year (YYYY)
+#gYearMonth Defines a part of a date - the year and month (YYYY-MM)
+#time Defines a time value
+
+module ActiveApi
+ module Xs
+ class Time
+#YYYY-MM-DDThh:mm:ss
+ class << self
+ def format(value)
+ value.strftime("%Y")
+ end
+ end
+ end
+ end
+end
@@ -0,0 +1,25 @@
+#byte A signed 8-bit integer
+#decimal A decimal value
+#int A signed 32-bit integer
+#integer An integer value
+#long A signed 64-bit integer
+#negativeInteger An integer containing only negative values (..,-2,-1)
+#nonNegativeInteger An integer containing only non-negative values (0,1,2,..)
+#nonPositiveInteger An integer containing only non-positive values (..,-2,-1,0)
+#positiveInteger An integer containing only positive values (1,2,..)
+#short A signed 16-bit integer
+#unsignedLong An unsigned 64-bit integer
+#unsignedInt An unsigned 32-bit integer
+#unsignedShort An unsigned 16-bit integer
+#unsignedByte An unsigned 8-bit integer
+module ActiveApi
+ module Xs
+ class Decimal
+ class << self
+ def format(value)
+ value.to_s
+ end
+ end
+ end
+ end
+end
@@ -0,0 +1,11 @@
+module ActiveApi
+ module Xs
+ class Integer
+ class << self
+ def format(value)
+ value.to_s
+ end
+ end
+ end
+ end
+end
@@ -0,0 +1,26 @@
+#ENTITIES
+#ENTITY
+#ID A string that represents the ID attribute in XML (only used with schema attributes)
+#IDREF A string that represents the IDREF attribute in XML (only used with schema attributes)
+#IDREFS
+#language A string that contains a valid language id
+#Name A string that contains a valid XML name
+#NCName
+#NMTOKEN A string that represents the NMTOKEN attribute in XML (only used with schema attributes)
+#NMTOKENS
+#normalizedString A string that does not contain line feeds, carriage returns, or tabs
+#QName
+#string A string
+#token A string that does not contain line feeds, carriage returns, tabs, leading or trailing spaces, or multiple spaces
+
+module ActiveApi
+ module Xs
+ class String
+ class << self
+ def format(value)
+ value
+ end
+ end
+ end
+ end
+end
View
@@ -0,0 +1,13 @@
+module ActiveApi
+ module Xs
+ class Time
+ #hh:mm:ss
+ #09:30:10.5
+ class << self
+ def format(value)
+ value.strftime("%Y")
+ end
+ end
+ end
+ end
+end
@@ -25,7 +25,7 @@ module ActiveApi
end
end
- [:string, :date, :datetime, :reference, :collection].each do |method_name|
+ [:string, :date, :datetime, :parent, :children].each do |method_name|
describe "##{method_name}" do
it "adds a #{method_name} field with the options" do
definition = Definition.new(:article)
Oops, something went wrong.

0 comments on commit 56a0a92

Please sign in to comment.