Class: SchemaComments::SchemaComment::SortedStore
- Inherits:
-
YAML::Store
- Object
- YAML::Store
- SchemaComments::SchemaComment::SortedStore
- Defined in:
- lib/schema_comments/schema_comment.rb
Class Method Summary collapse
Instance Method Summary collapse
Class Method Details
.sort_yaml_content!(root) ⇒ Object
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 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/schema_comments/schema_comment.rb', line 158 def self.sort_yaml_content!(root) self.validate_yaml!(root) table_comments = (root['table_comments'] ||= {}) column_comments = (root['column_comments'] ||= {}) # 大元は # table_comments: # ... # column_comments: # ... # その他 # ... # の順番です。 root.extend(HashKeyOrderable) root.key_order = %w(table_comments column_comments) # table_comments はテーブル名のアルファベット順 table_names = ActiveRecord::Base.connection.tables.sort - ['schema_migrations'] table_comments.extend(HashKeyOrderable) table_comments.key_order = table_names # column_comments もテーブル名のアルファベット順 column_comments.extend(HashKeyOrderable) column_comments.key_order = table_names # column_comments の各値はテーブルのカラム順 conn = ActiveRecord::Base.connection columns_method = conn.respond_to?(:columns_without_schema_comments) ? :columns_without_schema_comments : :columns column_comments.each do |table_name, column_hash| column_hash ||= {} column_names = nil begin columns = conn.send(columns_method, table_name) column_names = columns.map(&:name) rescue ActiveRecord::ActiveRecordError column_names = column_hash.keys.sort end column_names.delete('id') raise YamlError, "Broken schame_comments.yml" unless column_hash.is_a?(Hash) column_hash.extend(HashKeyOrderable) column_hash.key_order = column_names end end |
.validate_yaml!(root) ⇒ Object
146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/schema_comments/schema_comment.rb', line 146 def self.validate_yaml!(root) table_comments = (root['table_comments'] ||= {}) column_comments = (root['column_comments'] ||= {}) # raise YamlError, "Broken schame_comments.yml by invalid root: #{root.inspect}" unless root.is_a?(Hash) raise YamlError, "Broken schame_comments.yml by invalid table_comments" unless table_comments.is_a?(Hash) raise YamlError, "Broken schame_comments.yml by invalid_column_comments" unless column_comments.is_a?(Hash) column_comments.each do |table_name, value| next if value.nil? raise YamlError, "Broken schame_comments.yml by invalid column_comments for #{table_name}" unless value.is_a?(Hash) end end |
Instance Method Details
#dump(table) ⇒ Object
135 136 137 138 139 140 141 142 143 144 |
# File 'lib/schema_comments/schema_comment.rb', line 135 def dump(table) root = nil StringIO.open do |io| YAML.dump(@table, io) io.rewind root = YAML.load(io) end SortedStore.sort_yaml_content!(root) root.to_yaml(@opt) end |