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_check_constraint(table_name, expression, options = {}) ⇒ Object
- #add_exclusion_constraint(table_name, expression, options = {}) ⇒ Object
- #add_foreign_key(from_table, to_table, options = {}) ⇒ Object
- #add_index(table_name, column_name, options = {}) ⇒ Object
- #add_unique_constraint(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
- #require_relative(relative_path) ⇒ Object
Constructor Details
#initialize(opts = {}) ⇒ Context
Returns a new instance of Context.
8 9 10 11 12 |
# File 'lib/ridgepole/dsl_parser/context.rb', line 8 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.
6 7 8 |
# File 'lib/ridgepole/dsl_parser/context.rb', line 6 def __definition @__definition end |
#__execute ⇒ Object (readonly)
Returns the value of attribute __execute.
6 7 8 |
# File 'lib/ridgepole/dsl_parser/context.rb', line 6 def __execute @__execute end |
Class Method Details
.eval(dsl, opts = {}) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/ridgepole/dsl_parser/context.rb', line 14 def self.eval(dsl, opts = {}) ctx = 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_check_constraint(table_name, expression, options = {}) ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/ridgepole/dsl_parser/context.rb', line 94 def add_check_constraint(table_name, expression, = {}) table_name = table_name.to_s expression = expression.to_s [:name] = [:name].to_s if [:name] idx = [:name] || expression @__definition[table_name] ||= {} @__definition[table_name][:check_constraints] ||= {} @__definition[table_name][:check_constraints][idx] = { expression: expression, options: , } end |
#add_exclusion_constraint(table_name, expression, options = {}) ⇒ Object
109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/ridgepole/dsl_parser/context.rb', line 109 def add_exclusion_constraint(table_name, expression, = {}) table_name = table_name.to_s expression = expression.to_s [:name] = [:name].to_s if [:name] idx = [:name] || expression @__definition[table_name] ||= {} @__definition[table_name][:exclusion_constraints] ||= {} @__definition[table_name][:exclusion_constraints][idx] = { expression: expression, options: , } end |
#add_foreign_key(from_table, to_table, options = {}) ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/ridgepole/dsl_parser/context.rb', line 76 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] [:primary_key] = [:primary_key].to_s if [:primary_key] [:column] = [:column].to_s if [:column] @__definition[from_table] ||= {} @__definition[from_table][:foreign_keys] ||= {} idx = [:name] || [from_table, to_table, [:column]] raise "Foreign Key `#{from_table}(#{idx})` already defined" if @__definition[from_table][:foreign_keys][idx] @__definition[from_table][:foreign_keys][idx] = { to_table: to_table, options: , } end |
#add_index(table_name, column_name, options = {}) ⇒ Object
47 48 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 |
# File 'lib/ridgepole/dsl_parser/context.rb', line 47 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 column_name = [column_name].flatten.map(&:to_s) unless column_name.is_a?(String) && /\W/ === column_name # rubocop:disable Style/CaseEquality [:name] = [:name].to_s if [:name] @__definition[table_name] ||= {} @__definition[table_name][:indices] ||= {} idx = [:name] || column_name raise "Index `#{table_name}(#{idx})` already defined" if @__definition[table_name][:indices][idx] if [:length].is_a?(Numeric) index_length = [:length] [:length] = {} column_name.each do |col| [:length][col] = index_length end end [:length] = [:length].compact.symbolize_keys if [:length] @__definition[table_name][:indices][idx] = { column_name: column_name, options: , } end |
#add_unique_constraint(table_name, column_name, options = {}) ⇒ Object
124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/ridgepole/dsl_parser/context.rb', line 124 def add_unique_constraint(table_name, column_name, = {}) table_name = table_name.to_s column_name = Array(column_name).map(&:to_sym) [:name] = [:name].to_s if [:name] idx = [:name] || column_name @__definition[table_name] ||= {} @__definition[table_name][:unique_constraints] ||= {} @__definition[table_name][:unique_constraints][idx] = { column_name: column_name, options: , } end |
#create_table(table_name, options = {}) {|table_definition| ... } ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/ridgepole/dsl_parser/context.rb', line 26 def create_table(table_name, = {}) table_name = table_name.to_s table_definition = TableDefinition.new(table_name, self) [:primary_key] = [:primary_key].to_s if [:primary_key].is_a?(Symbol) if [:id] && TableDefinition::ALIAS_TYPES.key?([:id]) type, type_default_opts = TableDefinition::ALIAS_TYPES[[:id]] [:id] = type = type_default_opts.merge() end yield(table_definition) @__definition[table_name] ||= {} raise "Table `#{table_name}` already defined" if @__definition[table_name][:definition] @__definition[table_name][:definition] = table_definition.__definition .delete(:force) @__definition[table_name][:options] = end |
#execute(sql, _name = nil, &cond) ⇒ Object
155 156 157 158 159 160 |
# File 'lib/ridgepole/dsl_parser/context.rb', line 155 def execute(sql, _name = nil, &cond) @__execute << { sql: sql, condition: cond, } end |
#require(file) ⇒ Object
139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/ridgepole/dsl_parser/context.rb', line 139 def require(file) schemafile = %r{\A/}.match?(file) ? 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 |
#require_relative(relative_path) ⇒ Object
151 152 153 |
# File 'lib/ridgepole/dsl_parser/context.rb', line 151 def require_relative(relative_path) require(File.(relative_path, File.dirname(caller[0]))) end |