Class: Oinky::Model::Ruby
- Inherits:
-
Object
- Object
- Oinky::Model::Ruby
- Defined in:
- lib/oinky/rb_emitter.rb
Instance Method Summary collapse
-
#default_value_expression(col) ⇒ Object
def handle_from_tablename(str) ; “#cppify(str)_handle” ; end def accessor_from_tablename(str) ; cppify(str) ; end.
-
#emit ⇒ Object
Generate a string, containing the Ruby code for the model classes.
-
#initialize(schema) ⇒ Ruby
constructor
A new instance of Ruby.
- #rbify_chars(str) ⇒ Object
- #rbify_method(str) ⇒ Object
- #schema_classname ⇒ Object
- #tbl_classname(str) ⇒ Object
- #to_ruby(h) ⇒ Object
Constructor Details
#initialize(schema) ⇒ Ruby
Returns a new instance of Ruby.
11 12 13 |
# File 'lib/oinky/rb_emitter.rb', line 11 def initialize(schema) @schema = Oinky::Model.normalize_schema(schema) end |
Instance Method Details
#default_value_expression(col) ⇒ Object
def handle_from_tablename(str) ; “#cppify(str)_handle” ; end def accessor_from_tablename(str) ; cppify(str) ; end
37 38 39 40 41 42 |
# File 'lib/oinky/rb_emitter.rb', line 37 def default_value_expression(col) value = col value = col[:default] if col.is_a? Hash return "DateTime.parse(#{value.to_s.inspect})" if value.is_a? DateTime return value.inspect end |
#emit ⇒ Object
Generate a string, containing the Ruby code for the model classes. this can be eval’d directly in ruby, or saved and required more normally. Supporting both lends itself to static analysis and transformation moreso than if we solved this with ruby metaprogramming.
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/oinky/rb_emitter.rb', line 67 def emit p = Oinky::Detail::Builder.new schemacls = self.schema_classname p.next("class #{schemacls}", "end ##{schemacls}") { tables = @schema[:tables] p.next("def self.schema", "end") { p << to_ruby(@schema.merge(:classname=>schemacls)) } p.next("def schema", "end") { p << 'self.class.schema' } p << '' p.next("def inspect", "end") { # Hide all the internal state, which gets really obnoxious p << '"#<#{self.class}:0x#{self.__id__.to_s(16)}>"' # This is just a bit long to include. #schema=#{self.schema.to_s}>"' } p << '' tables.each{|nm,t| clsname = tbl_classname(nm) p.next("class #{clsname}","end ##{clsname}") { p << "include Oinky::Model::Table" p << '' p.next("def initialize(tbl)","end") { p << "@tbl = tbl" p << "self.class.migrate(tbl)" p << '' t[:indices].each {|k,v| accessor = rbify_method(v[:accessor]) p << "@#{accessor} = @tbl.indices[#{k.inspect}]" } } p << '' p.next("def self.migrate(tbl)","end") { p.next("tbl.db.atomic {", "}") { p << "cols = tbl.columns" t[:columns].each{|k,v| p.next("if c = cols[#{k.inspect}]","end") { p << "raise OinkyException.new('Mismatched column type for column [#{k.to_s}]') unless c[:type] == :#{v[:type]}" p.write("else",-1) p << "tbl.add_column(#{k.inspect},#{v[:type].inspect},#{default_value_expression(v)})" } } p << "ixs = tbl.indices" t[:indices].each{|k,v| p.next("unless ixs[#{k.inspect}]","end") { p << "tbl.add_index(:name=>#{k.inspect},:unique=>#{v[:unique]},:columns=>#{v[:columns]})" } } } } p << '' t[:indices].each {|k,v| accessor = rbify_method(v[:accessor]) p << "attr_accessor :#{accessor}" } p << "def table ; @tbl ; end" p << "alias :t :table" } #tablecls p << '' } #tables.each p.next("def initialize(db, prefix = '')","end") { tables.each{|nm,t| accessor = rbify_method(t[:accessor]) p << "tn = prefix + #{nm.inspect}" p << "db[tn] or db.create_table(tn)" p << "@#{accessor} = #{tbl_classname(nm)}.new(db[tn])" } } tables.each{|nm,t| accessor = rbify_method(t[:accessor]) p << "attr_accessor :#{accessor}" } } #schemacls return p.format end |
#rbify_chars(str) ⇒ Object
21 22 23 |
# File 'lib/oinky/rb_emitter.rb', line 21 def rbify_chars(str) str = str.gsub(/[^a-z0-9A-Z_]/,'_') end |
#rbify_method(str) ⇒ Object
15 16 17 18 19 |
# File 'lib/oinky/rb_emitter.rb', line 15 def rbify_method(str) str = str.gsub(/[^a-z0-9A-Z_]/,'_') return str unless /$[^a-z]/ =~ str return "_" + str end |
#schema_classname ⇒ Object
25 26 27 28 29 30 31 |
# File 'lib/oinky/rb_emitter.rb', line 25 def schema_classname cn = @schema[:classname] return "S#{@schema[:name].to_s}Adapter" unless cn return cn[:ruby] if cn.is_a? Hash return cn.call(:ruby) if cn.is_a? Proc return cn.to_s end |
#tbl_classname(str) ⇒ Object
33 |
# File 'lib/oinky/rb_emitter.rb', line 33 def tbl_classname(str) ; "Table_#{rbify_chars(str)}" ; end |
#to_ruby(h) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/oinky/rb_emitter.rb', line 44 def to_ruby(h) str = ['{'] h.each {|k,v| str << k.inspect + "=>" if v.is_a? Hash str << to_ruby(v) elsif v.is_a? DateTime str << default_value_expression(v) else str << v.inspect end str << ',' } str << '}' return str * '' end |