Module: DbAgile::Core::Schema::Builder::Coercion

Included in:
DbAgile::Core::Schema::Builder
Defined in:
lib/dbagile/core/schema/builder/coercion.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.unsymbolize_array(array) ⇒ Object

Unsymbolizes an array of names



18
19
20
# File 'lib/dbagile/core/schema/builder/coercion.rb', line 18

def unsymbolize_array(array)
  array.collect{|c| c.to_s}
end

.unsymbolize_hash(h) ⇒ Object

Tools



10
11
12
13
14
# File 'lib/dbagile/core/schema/builder/coercion.rb', line 10

def unsymbolize_hash(h)
  unsymbolized = {}
  h.each_pair{|k,v| unsymbolized[k.to_s] = v}
  unsymbolized
end

Instance Method Details

#coerce_array(array, non_empty) ⇒ Object

Coerces an array



84
85
86
87
88
89
90
91
92
# File 'lib/dbagile/core/schema/builder/coercion.rb', line 84

def coerce_array(array, non_empty)
  unless array.kind_of?(Array)
    coercion_error!
  end
  if non_empty and array.empty?
    coercion_error!
  end
  array
end

#coerce_attribute_definition(defn) ⇒ Object

Coerces an attribute definition



162
163
164
165
166
167
168
169
170
# File 'lib/dbagile/core/schema/builder/coercion.rb', line 162

def coerce_attribute_definition(defn)
  defn = coerce_symbolized_hash(defn)
  defn[:domain] = coerce_domain(defn[:domain])
  if defn.key?(:default)
    defn[:default] = coerce_default_value(defn[:default], defn[:domain])
  end
  defn[:mandatory] = coerce_mandatory(defn[:mandatory])
  defn
end

#coerce_attribute_names(defn, non_empty = true) ⇒ Object

Coerces attributes names



152
153
154
155
# File 'lib/dbagile/core/schema/builder/coercion.rb', line 152

def coerce_attribute_names(defn, non_empty = true)
  defn = coerce_array(defn, non_empty)
  defn.collect{|c| coerce_name(c)}
end

#coerce_constraint_definition(defn) ⇒ Object

Coerces a constraint definition



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/dbagile/core/schema/builder/coercion.rb', line 173

def coerce_constraint_definition(defn)
  defn = coerce_symbolized_hash(defn)
  defn[:type] = coerce_name(defn[:type])
  
  case type = defn[:type]
    when :primary_key, :candidate_key
      has_exactly_hash_keys!(defn, :type, :attributes)
      defn[:attributes] = coerce_attribute_names(defn[:attributes], true)
    when :foreign_key
      if defn.key?(:key)
        has_exactly_hash_keys!(defn, :type, :attributes, :references, :key)
        defn[:attributes] = coerce_attribute_names(defn[:attributes], true)
        defn[:references] = coerce_name(defn[:references])
        defn[:key]        = coerce_name(defn[:key])
      else
        has_exactly_hash_keys!(defn, :type, :attributes, :references)
        defn[:attributes] = coerce_attribute_names(defn[:attributes], true)
        defn[:references] = coerce_name(defn[:references])
      end
    else
      invalid!("unknown constraint type #{type}")
  end
  defn
end

#coerce_default_value(value, domain) ⇒ Object

Coerces a default value



122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/dbagile/core/schema/builder/coercion.rb', line 122

def coerce_default_value(value, domain)
  not_nil!(value, domain)
  if value.kind_of?(Symbol)
    case value 
      when :autonumber
        :autonumber
      else
        invalid!("unknown default value handler #{value}") 
    end
  else
    SByC::TypeSystem::Ruby::coerce(value, domain)
  end
end

#coerce_domain(domain) ⇒ Object

Coerces a domain



137
138
139
140
141
142
143
144
# File 'lib/dbagile/core/schema/builder/coercion.rb', line 137

def coerce_domain(domain)
  not_nil!(domain)
  domain = SByC::TypeSystem::Ruby::coerce(domain, Module)
  unless DbAgile::RECOGNIZED_DOMAINS.include?(domain)
    invalid!("unable to use #{domain} for attribute domain")
  end
  domain
end

#coerce_index_definition(defn) ⇒ Object

Coerces an index definition



199
200
201
202
203
204
# File 'lib/dbagile/core/schema/builder/coercion.rb', line 199

def coerce_index_definition(defn)
  defn = coerce_symbolized_hash(defn)
  defn[:relvar] = coerce_name(defn[:relvar])
  defn[:attributes] = coerce_attribute_names(defn[:attributes], true)
  defn
end

#coerce_mandatory(mandatory) ⇒ Object

Coerces a mandatory value



147
148
149
# File 'lib/dbagile/core/schema/builder/coercion.rb', line 147

def coerce_mandatory(mandatory)
  mandatory.nil? ? true : SByC::TypeSystem::Ruby::coerce(mandatory, SByC::TypeSystem::Ruby::Boolean)
end

#coerce_name(name) ⇒ Object Also known as: coerce_relvar_name, coerce_attribute_name, coerce_constraint_name, coerce_index_name

Coerces a name



112
113
114
115
# File 'lib/dbagile/core/schema/builder/coercion.rb', line 112

def coerce_name(name)
  valid_name!(name)
  name.to_s.to_sym
end

#coerce_symbolized_hash(hash, recursive = false) ⇒ Object

Coerces a symbolized hash



95
96
97
98
99
100
101
102
103
104
105
# File 'lib/dbagile/core/schema/builder/coercion.rb', line 95

def coerce_symbolized_hash(hash, recursive = false)
  hash = not_nil_hash!(hash)
  symbolized = {}
  hash.each_pair{|k,v| 
    if recursive and v.kind_of?(Hash)
      v = coerce_symbolized_hash(v)
    end
    symbolized[coerce_name(k)] = v
  }
  symbolized
end

#coercion_error!(msg = "") ⇒ Object

Raises a coercion error

Raises:

  • (::SByC::TypeSystem::CoercionError)


33
34
35
# File 'lib/dbagile/core/schema/builder/coercion.rb', line 33

def coercion_error!(msg = "")
  raise ::SByC::TypeSystem::CoercionError, msg, caller
end

#has_exactly_hash_keys!(hash, *keys) ⇒ Object



62
63
64
65
66
67
68
# File 'lib/dbagile/core/schema/builder/coercion.rb', line 62

def has_exactly_hash_keys!(hash, *keys)
  not_nil!(hash)
  unless keys.all?{|k| hash.key?(k)}
    coercion_error!("Expected #{keys.inspect}, found #{hash.keys.inspect}")
  end
  hash
end

#invalid!(msg) ⇒ Object

Raises a DbAgile::SchemaSyntaxError



28
29
30
# File 'lib/dbagile/core/schema/builder/coercion.rb', line 28

def invalid!(msg)
  raise DbAgile::SchemaSyntaxError, msg, caller
end

#not_empty!(*args) ⇒ Object

Asserts that all args are not empty



46
47
48
49
50
51
# File 'lib/dbagile/core/schema/builder/coercion.rb', line 46

def not_empty!(*args)
  if args.any?{|arg| arg.nil? or arg.to_s.empty?}
    coercion_error!
  end
  args
end

#not_nil!(*args) ⇒ Object

Asserts that all args are not nil



38
39
40
41
42
43
# File 'lib/dbagile/core/schema/builder/coercion.rb', line 38

def not_nil!(*args)
  if args.any?{|arg| arg.nil?}
    coercion_error!
  end
  args
end

#not_nil_hash!(hash) ⇒ Object

Asserts that a hash is not nil and a hash



54
55
56
57
58
59
60
# File 'lib/dbagile/core/schema/builder/coercion.rb', line 54

def not_nil_hash!(hash)
  not_nil!(hash)
  unless hash.kind_of?(Hash)
    coercion_error!("Hash expected, #{hash.class} received")
  end
  hash
end

#valid_name!(name) ⇒ Object

Asserts that a name is valid



71
72
73
74
75
76
77
# File 'lib/dbagile/core/schema/builder/coercion.rb', line 71

def valid_name!(name)
  not_empty!(name)
  unless [String, Symbol].include?(name.class)
    coercion_error!
  end
  name
end