Class: DbAgile::Core::Schema::Composite

Inherits:
SchemaObject show all
Defined in:
lib/dbagile/core/schema/composite.rb

Instance Attribute Summary

Attributes inherited from SchemaObject

#parent, #status

Instance Method Summary collapse

Methods inherited from SchemaObject

#ancestors, #attribute?, #builder_args, #builder_handler, #candidate_key?, #composite?, #constraint?, #foreign_key?, #index?, #logical?, #outside_dependencies, #outside_dependents, #part?, #physical?, #primary_key?, #relation_variable, #relvar?, #relview?, #schema

Constructor Details

#initialize(composite_parts = _default_parts) ⇒ Composite

Creates a composite instance with parts



7
8
9
10
11
12
13
# File 'lib/dbagile/core/schema/composite.rb', line 7

def initialize(composite_parts = _default_parts)
  unless composite_parts.kind_of?(Hash)
    raise ArgumentError, "Composite parts must be a hash, got #{composite_parts.inspect}" 
  end
  _install_parts(composite_parts)
  @insert_order = nil
end

Instance Method Details

#[](name) ⇒ Object

See Also:

  • DbAgile::Core::SchemaObject


157
158
159
# File 'lib/dbagile/core/schema/composite.rb', line 157

def [](name)
  @composite_parts[name]
end

#[]=(name, part, status = nil) ⇒ Object

See Also:

  • DbAgile::Core::SchemaObject


162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/dbagile/core/schema/composite.rb', line 162

def []=(name, part, status = nil)
  if @composite_parts.key?(name)
    raise SchemaConflictError.new(self[name], part, name)
  end
  @composite_parts[name] = part
  (@insert_order ||= []) << name
  part.send(:parent=, self)
  unless status.nil?
    part.visit{|p, parent| p.status = status}
  end
  part
end

#dependencies(include_parent = false) ⇒ Object

Returns an array with part dependencies



107
108
109
110
111
# File 'lib/dbagile/core/schema/composite.rb', line 107

def dependencies(include_parent = false)
  deps = parts.collect{|p| p.dependencies(include_parent)}.flatten.uniq
  deps += [ parent ] if include_parent and not(parent.nil?)
  deps
end

#dupObject

See Also:

  • DbAgile::Core::SchemaObject


227
228
229
# File 'lib/dbagile/core/schema/composite.rb', line 227

def dup
  self.class.new(_dup_parts)
end

#each_part(&block) ⇒ Object

Yields the block with each part in turn



114
115
116
# File 'lib/dbagile/core/schema/composite.rb', line 114

def each_part(&block)
  parts.each(&block)
end

#empty?(recurse = true) ⇒ Boolean

Returns:

  • (Boolean)

See Also:

  • DbAgile::Core::SchemaObject


129
130
131
132
133
134
135
# File 'lib/dbagile/core/schema/composite.rb', line 129

def empty?(recurse = true)
  if recurse
    parts.all?{|p| p.composite? && p.empty?(recurse)}
  else
    @composite_parts.empty?
  end
end

#look_same_as?(other) ⇒ Boolean

Returns:

  • (Boolean)

See Also:

  • DbAgile::Core::SchemaObject


219
220
221
222
223
224
# File 'lib/dbagile/core/schema/composite.rb', line 219

def look_same_as?(other)
  return nil unless other.kind_of?(self.class)
  my_parts = part_keys(true)
  return false unless (my_parts == other.part_keys(true))
  my_parts.all?{|k| self[k].look_same_as?(other[k])}
end

#part_keys(sort = (RUBY_VERSION < "1.9")) ⇒ Object

See Also:

  • DbAgile::Core::SchemaObject


143
144
145
146
147
148
149
# File 'lib/dbagile/core/schema/composite.rb', line 143

def part_keys(sort = (RUBY_VERSION < "1.9"))
  if sort
    @composite_parts.keys.sort{|k1,k2| k1.to_s <=> k2.to_s}
  else
    (_prefered_order + @composite_parts.keys).uniq
  end
end

#partsObject

See Also:

  • DbAgile::Core::SchemaObject


152
153
154
# File 'lib/dbagile/core/schema/composite.rb', line 152

def parts
  part_keys(false).collect{|k| self[k]}
end

#sizeObject

Returns number of parts



138
139
140
# File 'lib/dbagile/core/schema/composite.rb', line 138

def size
  @composite_parts.size
end

#to_sObject

Returns a string representation



232
233
234
# File 'lib/dbagile/core/schema/composite.rb', line 232

def to_s
  "#{DbAgile::RubyTools::unqualified_class_name(self.class)}"
end

#to_yaml(opts = {}) ⇒ Object

See Also:

  • DbAgile::Core::SchemaObject


180
181
182
183
184
185
186
187
188
# File 'lib/dbagile/core/schema/composite.rb', line 180

def to_yaml(opts = {})
  YAML::quick_emit(self, opts){|out|
    out.map("tag:yaml.org,2002:map") do |map|
      part_keys.each{|k|
        map.add(k.to_s, self[k])
      }
    end
  }
end

#visit(&block) ⇒ Object



119
120
121
122
# File 'lib/dbagile/core/schema/composite.rb', line 119

def visit(&block)
  block.call(self, parent)
  parts.each{|p| p.visit(&block)}
end

#yaml_display(env, options = {}, colors = DbAgile::Core::Schema::STATUS_TO_COLOR, indent = 0) ⇒ Object

Returns a yaml string



191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/dbagile/core/schema/composite.rb', line 191

def yaml_display(env, 
                 options = {}, 
                 colors = DbAgile::Core::Schema::STATUS_TO_COLOR, 
                 indent = 0)
  part_keys.each{|k|
    part = self[k]
    status = part.status.to_s.ljust(25)
    show_it = !(part.status == Schema::NO_CHANGE and options[:skip_unchanged])
    if show_it
      mine = "  "*indent + k.to_s + ":"
      if part.composite?
        env.display(mine, colors[part.status])
        part.yaml_display(env, options, colors, indent+1)
      else
        part_str = part.to_yaml
        part_str =~ /---\s*(.*)$/
        part_str = $1
        env.display(mine + " " + part_str, colors[part.status])
      end
    end
  }
end