Skip to content

Instantly share code, notes, and snippets.

@psndcsrv
Created June 24, 2009 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save psndcsrv/135289 to your computer and use it in GitHub Desktop.
Save psndcsrv/135289 to your computer and use it in GitHub Desktop.
a jruby script for generating useful strings to be included when creating new rigse models and views
require 'rubygems'
require 'java'
require 'jnlp'
require 'open-uri'
require 'hpricot'
require 'activesupport'
@@java_type_to_activerecord_type = {
"java.lang.String" => "string",
"java.lang.Boolean" => "boolean",
"java.lang.Double" => "float",
"java.lang.Integer" => "integer",
"java.lang.Float" => "float",
"org.concord.framework.otrunk.OTObjectList" => ""
}
@@active_record_type_to_haml_form_str = {
"string" => '" = f.text_field :#{prop.name.underscore}, :id => dom_id_for(#{class_str.underscore}, :item, :#{prop.name.underscore}_field)"',
"boolean" => '" = f.check_box :#{prop.name.underscore}, :id => dom_id_for(#{class_str.underscore}, :item, :#{prop.name.underscore}_checkbox)"',
"float" => '" = f.text_field :#{prop.name.underscore}, :id => dom_id_for(#{class_str.underscore}, :item, :#{prop.name.underscore}_field)"',
"integer" => '" = f.text_field :#{prop.name.underscore}, :id => dom_id_for(#{class_str.underscore}, :item, :#{prop.name.underscore}_field)"',
"" => '" = \"<select name=\'#{class_str.underscore}[#{prop.name.underscore.singularize}_ids][]\' multiple=\'true\' size=\'5\'>\"\n = #{prop.name.camelize}.by_scope(@scope).collect {|o| selected = false; selected = true if #{class_str.underscore}.#{prop.name.tableize}.include?(o); \"<option value=\'\#{o.id}\' \#{selected ? \"selected=\'true\'\" : \"\"}>\#{o.name}</option>\" }.join(\"\\\\n\")\n = \"</select>\""',
nil => '" = f.select :#{prop.name.underscore}_id, #{prop.name.classify}.by_scope(@scope).collect {|w| [\"\#{w.name}: \#{w.id}\", w.id]}, { :prompt => true }, {:id => dom_id_for(#{prop.name.underscore}, :item, :#{prop.name.underscore}_select)}"',
}
@@props_to_skip = ["name", "local_id", "id"]
o = Jnlp::Otrunk.new("http://jnlp.concord.org/dev/org/concord/maven-jnlp/all-otrunk-snapshot/all-otrunk-snapshot-0.1.0-20090327.222627.jnlp", 'cache')
o.require_resources
import org.concord.otrunk.otcore.impl.ReflectiveOTClassFactory
import org.concord.otrunk.biologica.OTChromosomeZoom
def get_ar_type(type)
@@java_type_to_activerecord_type[type.instanceClass.name]
end
def generator_property_string(otclass, class_str)
out_str = "script/generate element #{class_str} "
otclass.oTAllClassProperties.each do |prop|
prop_string = prop.name.underscore
next if @@props_to_skip.include?(prop_string)
type = prop.type
if type
ar_type = get_ar_type(type)
if ar_type == "" # OTObjectList
next
elsif ar_type == nil # OTObject
prop_string << "_id:integer"
else
prop_string << ":#{ar_type}"
end
else
next
end
prop_string << " "
out_str << prop_string
end
out_str.strip
end
def model_defaults(otclass)
out_str = ""
otclass.oTAllClassProperties.each do |prop|
prop_string = "default_value_for :#{prop.name.underscore}, "
next if @@props_to_skip.include?(prop.name.underscore)
default = prop.default
if default
prop_string << default.to_s
else
next
end
prop_string << "\n"
out_str << prop_string
end
out_str
end
def haml_otml(otclass, class_str)
out_str = ""
other_str = ""
otclass.oTAllClassProperties.each do |prop|
next if @@props_to_skip.include?(prop.name.underscore)
next if ! prop.type
if prop.is_primitive
out_str << ":#{prop.name} => "
out_str << class_str.underscore
out_str << ".#{prop.name.underscore}#{get_ar_type(prop.type) == "boolean" ? ".to_s" : ""}, "
else
other_str << " %#{prop.name}\n %object{ :refid => ot_refid_for(#{class_str.underscore}.#{prop.name.underscore}) }\n"
end
end
out_str.sub!(/, $/, "")
puts <<EOF
%#{otclass.name.split(".").last}{ :local_id => ot_local_id_for( #{class_str.underscore} ), #{out_str}}
#{other_str}
EOF
end
def haml_edit_form(otclass, class_str)
out_str = " - field_set_tag 'options' do\n"
otclass.oTAllClassProperties.each do |prop|
next if @@props_to_skip.include?(prop.name.underscore)
next if ! prop.type
ar_type = get_ar_type(prop.type)
haml_str = @@active_record_type_to_haml_form_str[ar_type]
out_str << " = \"#{prop.name.underscore.humanize}: \"\n"
out_str << eval(haml_str)
out_str << "\n = \"<br/>\"\n"
end
out_str
end
def generate_strings(otclass_str, rites_class_str)
begin
otclass_ruby = eval(otclass_str)
otclass = ReflectiveOTClassFactory.singleton.registerClass(otclass_ruby.java_class)
ReflectiveOTClassFactory.singleton.processAllNewlyRegisteredClasses()
puts generator_property_string(otclass, rites_class_str)
puts
puts model_defaults(otclass)
puts
puts haml_otml(otclass, rites_class_str)
puts
puts haml_edit_form(otclass, rites_class_str)
rescue NameError => e
puts "#{otclass_str} not found\n#{e}\n#{e.backtrace.join("\n")}"
end
puts
end
generate_strings("org.concord.otrunk.biologica.OTChromosomeZoom", "BiologicaChromosomeZoom")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment