Class: Ridgepole::DSLParser::Context
- Inherits:
-
Object
- Object
- Ridgepole::DSLParser::Context
- Defined in:
- lib/ridgepole/dsl_parser/context.rb
Instance Attribute Summary collapse
-
#__definition ⇒ Object
readonly
Returns the value of attribute __definition.
-
#__execute ⇒ Object
readonly
Returns the value of attribute __execute.
Class Method Summary collapse
Instance Method Summary collapse
- #add_foreign_key(from_table, to_table, options = {}) ⇒ Object
- #add_index(table_name, column_name, options = {}) ⇒ Object
- #create_table(table_name, options = {}) {|table_definition| ... } ⇒ Object
- #execute(sql, name = nil, &cond) ⇒ Object
-
#initialize(opts = {}) ⇒ Context
constructor
A new instance of Context.
- #require(file) ⇒ Object
Constructor Details
#initialize(opts = {}) ⇒ Context
Returns a new instance of Context.
6 7 8 9 10 |
# File 'lib/ridgepole/dsl_parser/context.rb', line 6 def initialize(opts = {}) @__working_dir = File.(opts[:path] ? File.dirname(opts[:path]) : Dir.pwd) @__definition = {} @__execute = [] end |
Instance Attribute Details
#__definition ⇒ Object (readonly)
Returns the value of attribute __definition.
3 4 5 |
# File 'lib/ridgepole/dsl_parser/context.rb', line 3 def __definition @__definition end |
#__execute ⇒ Object (readonly)
Returns the value of attribute __execute.
4 5 6 |
# File 'lib/ridgepole/dsl_parser/context.rb', line 4 def __execute @__execute end |
Class Method Details
.eval(dsl, opts = {}) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/ridgepole/dsl_parser/context.rb', line 12 def self.eval(dsl, opts = {}) ctx = self.new(opts) if opts[:path] ctx.instance_eval(dsl, opts[:path]) else ctx.instance_eval(dsl) end [ctx.__definition, ctx.__execute] end |
Instance Method Details
#add_foreign_key(from_table, to_table, options = {}) ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/ridgepole/dsl_parser/context.rb', line 84 def add_foreign_key(from_table, to_table, = {}) from_table = from_table.to_s to_table = to_table.to_s [:name] = [:name].to_s if [:name] @__definition[from_table] ||= {} @__definition[from_table][:foreign_keys] ||= {} idx = [:name] || [from_table, to_table] if @__definition[from_table][:foreign_keys][idx] raise "Foreign Key `#{from_table}(#{idx})` already defined" end @__definition[from_table][:foreign_keys][idx] = { :to_table => to_table, :options => , } end |
#add_index(table_name, column_name, options = {}) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/ridgepole/dsl_parser/context.rb', line 49 def add_index(table_name, column_name, = {}) table_name = table_name.to_s # Keep column_name for expression index support # https://github.com/rails/rails/pull/23393 unless column_name.is_a?(String) && /\W/ === column_name column_name = [column_name].flatten.map {|i| i.to_s } end [:name] = [:name].to_s if [:name] @__definition[table_name] ||= {} @__definition[table_name][:indices] ||= {} idx = [:name] || column_name if @__definition[table_name][:indices][idx] raise "Index `#{table_name}(#{idx})` already defined" end if [:length].is_a?(Numeric) index_length = [:length] [:length] = {} column_name.each do |col| [:length][col] = index_length end end if [:length] [:length] = [:length].compact.symbolize_keys end @__definition[table_name][:indices][idx] = { :column_name => column_name, :options => , } end |
#create_table(table_name, options = {}) {|table_definition| ... } ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/ridgepole/dsl_parser/context.rb', line 24 def create_table(table_name, = {}) table_name = table_name.to_s table_definition = TableDefinition.new(table_name, self) if [:primary_key] and [:primary_key].is_a?(Symbol) [:primary_key] = [:primary_key].to_s end if [:id] and TableDefinition::ALIAS_TYPES.has_key?([:id]) type, type_default_opts = TableDefinition::ALIAS_TYPES[[:id]] [:id] = type = type_default_opts.merge() end yield(table_definition) @__definition[table_name] ||= {} if @__definition[table_name][:definition] raise "Table `#{table_name}` already defined" end @__definition[table_name][:definition] = table_definition.__definition .delete(:force) @__definition[table_name][:options] = end |
#execute(sql, name = nil, &cond) ⇒ Object
114 115 116 117 118 119 |
# File 'lib/ridgepole/dsl_parser/context.rb', line 114 def execute(sql, name = nil, &cond) @__execute << { :sql => sql, :condition => cond, } end |
#require(file) ⇒ Object
102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/ridgepole/dsl_parser/context.rb', line 102 def require(file) schemafile = (file =~ %r|\A/|) ? file : File.join(@__working_dir, file) if File.exist?(schemafile) instance_eval(File.read(schemafile), schemafile) elsif File.exist?(schemafile + '.rb') instance_eval(File.read(schemafile + '.rb'), schemafile + '.rb') else Kernel.require(file) end end |