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_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
67 68 69 70 71 |
# File 'lib/ridgepole/dsl_parser.rb', line 67 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.
64 65 66 |
# File 'lib/ridgepole/dsl_parser.rb', line 64 def __definition @__definition end |
#__execute ⇒ Object (readonly)
Returns the value of attribute __execute.
65 66 67 |
# File 'lib/ridgepole/dsl_parser.rb', line 65 def __execute @__execute end |
Class Method Details
.eval(dsl, opts = {}) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/ridgepole/dsl_parser.rb', line 73 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 |
.include_module(mod) ⇒ Object
3 4 5 6 7 |
# File 'lib/ridgepole/dsl_parser.rb', line 3 def self.include_module(mod) unless self.included_modules.include?(mod) include mod end end |
Instance Method Details
#add_index(table_name, column_name, options = {}) ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/ridgepole/dsl_parser.rb', line 105 def add_index(table_name, column_name, = {}) table_name = table_name.to_s column_name = [column_name].flatten.map {|i| i.to_s } [: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 @__definition[table_name][:indices][idx] = { :column_name => column_name, :options => , } end |
#create_table(table_name, options = {}) {|table_definition| ... } ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/ridgepole/dsl_parser.rb', line 85 def create_table(table_name, = {}) table_name = table_name.to_s table_definition = TableDefinition.new [:primary_key].each do |key| [key] = [key].to_s if [key] 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
135 136 137 138 139 140 |
# File 'lib/ridgepole/dsl_parser.rb', line 135 def execute(sql, name = nil, &cond) @__execute << { :sql => sql, :condition => cond, } end |
#require(file) ⇒ Object
123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/ridgepole/dsl_parser.rb', line 123 def require(file) schemafile = 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 |