Class: Bricolage::Declarations

Inherits:
Object
  • Object
show all
Defined in:
lib/bricolage/sqlstatement.rb

Overview

Variable declarations in parameter files

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(vars = {}) ⇒ Declarations

Returns a new instance of Declarations.



116
117
118
119
120
121
# File 'lib/bricolage/sqlstatement.rb', line 116

def initialize(vars = {})
  @vars = {}
  vars.each do |name, default_value|
    declare name, default_value
  end
end

Class Method Details

.parse(source) ⇒ Object



112
113
114
# File 'lib/bricolage/sqlstatement.rb', line 112

def Declarations.parse(source)
  SQLMetaDataParser.new.parse_declarations(source)
end

Instance Method Details

#[](name) ⇒ Object



131
132
133
# File 'lib/bricolage/sqlstatement.rb', line 131

def [](name)
  @vars[name.to_s] or raise ParameterError, "undeclared variable: #{name}"
end

#declare(name, value) ⇒ Object

Raises:



154
155
156
157
158
# File 'lib/bricolage/sqlstatement.rb', line 154

def declare(name, value)
  raise ParameterError, "duplicated variable declaration: #{name}" if declared?(name)
  varname = name.to_s.tr('-', '_')
  @vars[varname] = Declaration.new(varname, value)
end

#declare_map(label, value) ⇒ Object



188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/bricolage/sqlstatement.rb', line 188

def declare_map(label, value)
  case value
  when String
    declare value, nil
  when Array
    value.each do |name|
      declare name, nil
    end
  when Hash
    value.each do |name, val|
      declare name, val
    end
  else
    raise ParameterError, "'#{label}' entry value must be a string, a list or a map but a #{value.class}"
  end
end

#declare_none(label, value) ⇒ Object



205
206
207
# File 'lib/bricolage/sqlstatement.rb', line 205

def declare_none(label, value)
  # ignore
end

#declare_table(name, value) ⇒ Object



160
161
162
163
164
165
166
167
168
169
# File 'lib/bricolage/sqlstatement.rb', line 160

def declare_table(name, value)
  case value
  when String
    declare name, value
  when nil
    declare name, name
  else
    raise ParameterError, "bad default value for table variable: #{value.class} (#{value.inspect})"
  end
end

#declare_tables(label, value) ⇒ Object



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/bricolage/sqlstatement.rb', line 171

def declare_tables(label, value)
  case value
  when String
    declare value, value
  when Array
    value.each do |name|
      declare name, name
    end
  when Hash
    value.each do |name, val|
      declare name, val
    end
  else
    raise ParameterError, "'#{label}' entry value must be a string, a list or a map but a #{value.class}"
  end
end

#declared?(name) ⇒ Boolean

Returns:

  • (Boolean)


127
128
129
# File 'lib/bricolage/sqlstatement.rb', line 127

def declared?(name)
  @vars.key?(name.to_s)
end

#default_variablesObject



139
140
141
142
143
144
145
146
147
# File 'lib/bricolage/sqlstatement.rb', line 139

def default_variables
  Variables.define {|vars|
    each do |decl|
      if decl.have_default_value?
        vars[decl.name.to_s] = decl.default_value
      end
    end
  }
end

#each(&block) ⇒ Object



135
136
137
# File 'lib/bricolage/sqlstatement.rb', line 135

def each(&block)
  @vars.each_value(&block)
end

#freezeObject



149
150
151
152
# File 'lib/bricolage/sqlstatement.rb', line 149

def freeze
  @vars.freeze
  super
end

#inspectObject



123
124
125
# File 'lib/bricolage/sqlstatement.rb', line 123

def inspect
  "\#<#{self.class} #{@vars.inspect}>"
end