myexperiment-hackers
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[myexperiment-hackers] [2352] branches/biocat/vendor/plugins: adding str


From: noreply
Subject: [myexperiment-hackers] [2352] branches/biocat/vendor/plugins: adding structured data plugin
Date: Tue, 30 Mar 2010 04:44:53 -0400 (EDT)

Revision
2352
Author
dgc
Date
2010-03-30 04:44:52 -0400 (Tue, 30 Mar 2010)

Log Message

adding structured data plugin

Added Paths

Diff

Added: branches/biocat/vendor/plugins/structured_data/MIT-LICENSE (0 => 2352)


--- branches/biocat/vendor/plugins/structured_data/MIT-LICENSE	                        (rev 0)
+++ branches/biocat/vendor/plugins/structured_data/MIT-LICENSE	2010-03-30 08:44:52 UTC (rev 2352)
@@ -0,0 +1,20 @@
+Copyright (c) 2009 University of Manchester and the University of Southampton
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Added: branches/biocat/vendor/plugins/structured_data/README (0 => 2352)


--- branches/biocat/vendor/plugins/structured_data/README	                        (rev 0)
+++ branches/biocat/vendor/plugins/structured_data/README	2010-03-30 08:44:52 UTC (rev 2352)
@@ -0,0 +1,13 @@
+StructuredData
+==============
+
+Introduction goes here.
+
+
+Example
+=======
+
+Example goes here.
+
+
+Copyright (c) 2009 [name of plugin creator], released under the MIT license

Added: branches/biocat/vendor/plugins/structured_data/Rakefile (0 => 2352)


--- branches/biocat/vendor/plugins/structured_data/Rakefile	                        (rev 0)
+++ branches/biocat/vendor/plugins/structured_data/Rakefile	2010-03-30 08:44:52 UTC (rev 2352)
@@ -0,0 +1,23 @@
+require 'rake'
+require 'rake/testtask'
+require 'rake/rdoctask'
+
+desc 'Default: run unit tests.'
+task :default => :test
+
+desc 'Test the structured_data plugin.'
+Rake::TestTask.new(:test) do |t|
+  t.libs << 'lib'
+  t.libs << 'test'
+  t.pattern = 'test/**/*_test.rb'
+  t.verbose = true
+end
+
+desc 'Generate documentation for the structured_data plugin.'
+Rake::RDocTask.new(:rdoc) do |rdoc|
+  rdoc.rdoc_dir = 'rdoc'
+  rdoc.title    = 'StructuredData'
+  rdoc.options << '--line-numbers' << '--inline-source'
+  rdoc.rdoc_files.include('README')
+  rdoc.rdoc_files.include('lib/**/*.rb')
+end

Added: branches/biocat/vendor/plugins/structured_data/init.rb (0 => 2352)


--- branches/biocat/vendor/plugins/structured_data/init.rb	                        (rev 0)
+++ branches/biocat/vendor/plugins/structured_data/init.rb	2010-03-30 08:44:52 UTC (rev 2352)
@@ -0,0 +1,9 @@
+# myExperiment: vendor/plugins/structured_data/init.rb
+#
+# Copyright (c) 2009 University of Manchester and the University of Southampton.
+# See license.txt for details.
+
+ActiveRecord::Base.extend StructuredData::ActsMethods
+
+AutoMigrate.migrate
+

Added: branches/biocat/vendor/plugins/structured_data/install.rb (0 => 2352)


--- branches/biocat/vendor/plugins/structured_data/install.rb	                        (rev 0)
+++ branches/biocat/vendor/plugins/structured_data/install.rb	2010-03-30 08:44:52 UTC (rev 2352)
@@ -0,0 +1,7 @@
+# myExperiment: vendor/plugins/structured_data/install.rb
+#
+# Copyright (c) 2009 University of Manchester and the University of Southampton.
+# See license.txt for details.
+
+Dir.mkdir("config/schema.d")
+

Added: branches/biocat/vendor/plugins/structured_data/lib/auto_migrate.rb (0 => 2352)


--- branches/biocat/vendor/plugins/structured_data/lib/auto_migrate.rb	                        (rev 0)
+++ branches/biocat/vendor/plugins/structured_data/lib/auto_migrate.rb	2010-03-30 08:44:52 UTC (rev 2352)
@@ -0,0 +1,179 @@
+# myExperiment: vendor/plugins/structured_data/lib/auto_migrate.rb
+#
+# Copyright (c) 2009 University of Manchester and the University of Southampton.
+# See license.txt for details.
+
+require 'xml/libxml'
+
+class AutoMigrate
+
+  AUTO_TABLE_NAME     = "auto_tables"
+  SCHEMA              = "config/base_schema.xml"
+  SCHEMA_D            = "config/schema.d"
+  COLUMN_ATTRIBUTES   = ['name', 'type']
+  HAS_MANY_ATTRIBUTES = ['target', 'through', 'foreign_key']
+
+  def self.schema
+
+    tables = {}
+    assocs = []
+
+    # load the base schema
+
+    if File.exists?(SCHEMA)
+      tables, assocs = merge_schema(File.read(SCHEMA), tables, assocs) 
+    end
+
+    # merge files from the schema directory
+
+    if File.exists?(SCHEMA_D)
+
+      Dir.new(SCHEMA_D).each do |entry|
+        if entry.ends_with?(".xml")
+          tables, assocs = merge_schema(File.read("#{SCHEMA_D}/#{entry}"), tables, assocs)
+        end
+      end
+    end
+
+    [tables, assocs]
+  end
+
+  def self.migrate
+
+    conn = ActiveRecord::Base.connection
+
+    # ensure that the auto_tables table exists
+    
+    tables = conn.tables
+
+    if tables.include?(AUTO_TABLE_NAME) == false
+      conn.create_table(AUTO_TABLE_NAME) do |table|
+        table.column :name,   :string
+        table.column :schema, :text
+      end
+    end
+
+    old_tables = AutoTable.find(:all).map do |table| table.name end
+       
+    # get the schema
+
+    new_tables, assocs = schema
+
+    # create and drop tables as appropriate
+
+    (old_tables - new_tables.keys).each do |name|
+      conn.drop_table(name)
+      AutoTable.find_by_name(name).destroy
+    end 
+
+    (new_tables.keys - old_tables).each do |name|
+      conn.create_table(name) do |table| end
+      AutoTable.create(:name => name)
+    end
+
+    # adjust the columns in each table
+    new_tables.keys.each do |table_name|
+
+      # get the list of existing columns
+
+      old_columns = conn.columns(table_name).map do |column| column.name end - ["id"]
+
+      # determine the required columns
+
+      new_columns = new_tables[table_name].map do |column, definition| column end
+
+      # remove columns
+
+      (old_columns - new_columns).each do |column_name|
+        conn.remove_column(table_name, column_name)
+      end
+
+      # add columns
+
+      (new_columns - old_columns).each do |column_name|
+        conn.add_column(table_name, column_name, new_tables[table_name][column_name]["type"].to_sym)
+      end
+    end
+
+    # Now that the schema has changed, update all the models
+
+    reload_models(new_tables.keys)
+  end
+
+  def self.destroy_auto_tables
+
+    conn   = ActiveRecord::Base.connection
+    tables = conn.tables
+    
+    AutoTable.find(:all).map do |table|
+      conn.drop_table(table.name)
+    end
+
+    conn.drop_table(AUTO_TABLE_NAME) if tables.include?(AUTO_TABLE_NAME)
+  end
+
+private
+
+  def self.merge_schema(schema, tables = {}, assocs = [])
+
+    root = LibXML::XML::Parser.string(schema).parse.root
+
+    root.find('/schema/table').each do |table|
+      tables[table['name']] ||= {}
+
+      table.find('column').each do |column|
+        tables[table['name']][column['name']] ||= {}
+
+        COLUMN_ATTRIBUTES.each do |attribute|
+          if column[attribute] and attribute != 'name'
+            tables[table['name']][column['name']][attribute] = column[attribute]
+          end
+        end
+      end
+    end
+
+    root.find('/schema/table').each do |table|
+      table.find('belongs-to').each do |belongs_to|
+        assocs.push(:table => table['name'], :type => 'belongs_to', :target => belongs_to['target'])
+      end
+
+      table.find('has-many').each do |has_many|
+        attributes = {:table => table['name'], :type => 'has_many'}
+
+        HAS_MANY_ATTRIBUTES.each do |attribute|
+          attributes[attribute.to_sym] = has_many[attribute] if has_many[attribute]
+        end
+
+        assocs.push(attributes)
+      end
+    end
+
+    [tables, assocs]
+  end
+
+  def self.reload_models(tables)
+    tables.each do |table|
+
+      file_name  = "app/models/#{table.singularize}.rb"
+      class_name = table.singularize.camelize
+
+      if File.exists?(file_name)
+
+        # force reload the model
+        Kernel::load(file_name)
+
+      else
+
+        # Create a class for it
+        c = Class.new(ActiveRecord::Base)
+        c.class_eval("acts_as_structured_data(:class_name => '#{class_name}')")
+
+        Object.const_set(class_name.to_sym, c)
+      end
+
+      # reload the model schema from the database
+      eval(class_name).reset_column_information
+    end
+  end
+end
+

Added: branches/biocat/vendor/plugins/structured_data/lib/auto_table.rb (0 => 2352)


--- branches/biocat/vendor/plugins/structured_data/lib/auto_table.rb	                        (rev 0)
+++ branches/biocat/vendor/plugins/structured_data/lib/auto_table.rb	2010-03-30 08:44:52 UTC (rev 2352)
@@ -0,0 +1,8 @@
+# myExperiment: vendor/plugins/structured_data/lib/auto_table.rb
+#
+# Copyright (c) 2009 University of Manchester and the University of Southampton.
+# See license.txt for details.
+
+class AutoTable < ActiveRecord::Base
+end
+

Added: branches/biocat/vendor/plugins/structured_data/lib/structured_data.rb (0 => 2352)


--- branches/biocat/vendor/plugins/structured_data/lib/structured_data.rb	                        (rev 0)
+++ branches/biocat/vendor/plugins/structured_data/lib/structured_data.rb	2010-03-30 08:44:52 UTC (rev 2352)
@@ -0,0 +1,40 @@
+# myExperiment: vendor/plugins/structured_data/lib/structured_data.rb
+#
+# Copyright (c) 2009 University of Manchester and the University of Southampton.
+# See license.txt for details.
+
+module StructuredData
+
+  module ActsMethods
+    def acts_as_structured_data(opts = {})
+
+      class_name = self.name
+
+      class_name = opts[:class_name] if opts[:class_name]
+
+      tables, associations = AutoMigrate.schema
+
+      associations.each do |association|
+
+        next unless association[:table].singularize.camelize == class_name
+
+        case association[:type]
+        when 'has_many'
+          bits = [":#{association[:target]}"]
+
+          bits.push(":through => :#{association[:through]}") if association[:through]
+          bits.push(":foreign_key => :#{association[:foreign_key]}") if association[:foreign_key]
+
+          line = "has_many #{bits.join(', ')}"
+          self.class_eval(line)
+
+        when 'belongs_to'
+
+          line = "belongs_to :#{association[:target].singularize}"
+          self.class_eval(line)
+        end
+      end
+    end
+  end
+end
+

Added: branches/biocat/vendor/plugins/structured_data/tasks/structured_data_tasks.rake (0 => 2352)


--- branches/biocat/vendor/plugins/structured_data/tasks/structured_data_tasks.rake	                        (rev 0)
+++ branches/biocat/vendor/plugins/structured_data/tasks/structured_data_tasks.rake	2010-03-30 08:44:52 UTC (rev 2352)
@@ -0,0 +1,4 @@
+# desc "Explaining what the task does"
+# task :structured_data do
+#   # Task goes here
+# end

Added: branches/biocat/vendor/plugins/structured_data/test/structured_data_test.rb (0 => 2352)


--- branches/biocat/vendor/plugins/structured_data/test/structured_data_test.rb	                        (rev 0)
+++ branches/biocat/vendor/plugins/structured_data/test/structured_data_test.rb	2010-03-30 08:44:52 UTC (rev 2352)
@@ -0,0 +1,8 @@
+require 'test_helper'
+
+class StructuredDataTest < ActiveSupport::TestCase
+  # Replace this with your real tests.
+  test "the truth" do
+    assert true
+  end
+end

Added: branches/biocat/vendor/plugins/structured_data/test/test_helper.rb (0 => 2352)


--- branches/biocat/vendor/plugins/structured_data/test/test_helper.rb	                        (rev 0)
+++ branches/biocat/vendor/plugins/structured_data/test/test_helper.rb	2010-03-30 08:44:52 UTC (rev 2352)
@@ -0,0 +1,3 @@
+require 'rubygems'
+require 'active_support'
+require 'active_support/test_case'
\ No newline at end of file

Added: branches/biocat/vendor/plugins/structured_data/uninstall.rb (0 => 2352)


--- branches/biocat/vendor/plugins/structured_data/uninstall.rb	                        (rev 0)
+++ branches/biocat/vendor/plugins/structured_data/uninstall.rb	2010-03-30 08:44:52 UTC (rev 2352)
@@ -0,0 +1 @@
+# Uninstall hook code here

reply via email to

[Prev in Thread] Current Thread [Next in Thread]