Class: Ridgepole::DSLParser::Context
- Inherits:
-
Object
- Object
- Ridgepole::DSLParser::Context
- Defined in:
- lib/ridgepole/dsl_parser.rb
Defined Under Namespace
Classes: TableDefinition
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.
137 138 139 140 141 |
# File 'lib/ridgepole/dsl_parser.rb', line 137 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.
134 135 136 |
# File 'lib/ridgepole/dsl_parser.rb', line 134 def __definition @__definition end |
#__execute ⇒ Object (readonly)
Returns the value of attribute __execute.
135 136 137 |
# File 'lib/ridgepole/dsl_parser.rb', line 135 def __execute @__execute end |
Class Method Details
.eval(dsl, opts = {}) ⇒ Object
143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/ridgepole/dsl_parser.rb', line 143 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
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 |
# File 'lib/ridgepole/dsl_parser.rb', line 211 def add_foreign_key(from_table, to_table, = {}) unless [:name] raise "Foreign key name in `#{from_table}` is undefined" end from_table = from_table.to_s to_table = to_table.to_s [:name] = [:name].to_s @__definition[from_table] ||= {} @__definition[from_table][:foreign_keys] ||= {} idx = [:name] 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
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 |
# File 'lib/ridgepole/dsl_parser.rb', line 175 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 # XXX: fix for https://github.com/rails/rails/commit/5025fd3a99c68f95bdd6fd43f382c62e9653236b if ActiveRecord::VERSION::MAJOR >= 6 or (ActiveRecord::VERSION::MAJOR == 5 and (ActiveRecord::VERSION::MINOR >= 1 or ActiveRecord::VERSION::TINY >= 1)) [:length] = [:length].symbolize_keys end end @__definition[table_name][:indices][idx] = { :column_name => column_name, :options => , } end |
#create_table(table_name, options = {}) {|table_definition| ... } ⇒ Object
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/ridgepole/dsl_parser.rb', line 155 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 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
245 246 247 248 249 250 |
# File 'lib/ridgepole/dsl_parser.rb', line 245 def execute(sql, name = nil, &cond) @__execute << { :sql => sql, :condition => cond, } end |
#require(file) ⇒ Object
233 234 235 236 237 238 239 240 241 242 243 |
# File 'lib/ridgepole/dsl_parser.rb', line 233 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 |