columns 0.0.1 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2fb05fd5b271d2da52b43da4de35438a74802cc0
4
- data.tar.gz: c2a54c11f138608d8905798d32f91b75e360cc20
3
+ metadata.gz: 74908684891067229e04bdab34a621b9d60e803f
4
+ data.tar.gz: 18d28d782d48a920625c3fd3cc2de7d5dcd63941
5
5
  SHA512:
6
- metadata.gz: bbf3877d98afc1460945b9641b0153c582763522b87668fc5459dbb9c2d485e608abe1d75657d2c1f45234675b03246ad28e6e7ec293b33574abd69adec95b0a
7
- data.tar.gz: 830ff6fafe2c9961be383205715ee7989dde535d117d8c049995ed2d9ff755c3c04e467594a197301cd3fff22db58cfc34ac98563ce719bb79591ea6f3fd0289
6
+ metadata.gz: 366a8c5b0d4271b0132519dabd8585493cce531bc306348f128f6cf1448024f74a0d6829c485c28a686c8c66729466e801ccf8eaab5525dfe8f1517698c808c9
7
+ data.tar.gz: 1cebe951c55d2e4ce23571d14ab986d62ba90481e597d42ecfc11cf0b11359ab90322a5e9a80234d6ca1543cb888e85d1aec1061ebb256098419fbc5094e7d90
data/.gitignore CHANGED
@@ -16,3 +16,4 @@ test/tmp
16
16
  test/version_tmp
17
17
  tmp
18
18
  *.swp
19
+ TODO
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # Columns
1
+ # Columns [![Gem Version](https://badge.fury.io/rb/columns.svg)](http://badge.fury.io/rb/columns)
2
2
 
3
3
  Annotates activerecord models using `db/schema.rb`.
4
4
 
@@ -22,11 +22,21 @@ Or install it yourself as:
22
22
 
23
23
  $ cd your_project
24
24
 
25
- Once you have generate a `db/schema.rb`, just type
25
+ Once you have generate a `db/schema.rb`, just type the following to
26
+ annotate your models:
26
27
 
27
28
  $ columns
28
29
 
29
- to annotate your models.
30
+ ### Do it programmaticaly
31
+
32
+ Instead of using Columns as a shell command, you can use it as a
33
+ library. To do so, call the following method:
34
+
35
+ ``` ruby
36
+ require 'columns'
37
+
38
+ Columns.execute
39
+ ```
30
40
 
31
41
  Note that models must to reside in `app/models`.
32
42
 
data/bin/columns CHANGED
@@ -2,33 +2,5 @@
2
2
 
3
3
  require 'columns'
4
4
 
5
- # find all tables in the schema
6
- schema_file = File.expand_path('./db/schema.rb')
7
-
8
- unless File.exists?(schema_file)
9
- puts "COLUMNS ERROR : #{schema_file} doesn't exist!"
10
- exit 1
11
- end
12
- table = Columns::Table.new(File.read(schema_file))
13
-
14
- # create RawData for each tables
15
- raw_data_objects = []
16
- table.names.each do |name|
17
- raw_data_objects << Columns::RawData.new(name, table.content_for(name))
18
- end
19
-
20
- # create ModelData from each RawData
21
- model_data_objects = raw_data_objects.map do |o|
22
- Columns::ModelData.new(o)
23
- end
24
-
25
- # clean all models using ModelCleaner
26
- # write all models using ModelData
27
- writer = Columns::ModelWriter.new
28
- model_data_objects.each do |o|
29
- path = File.expand_path('./app/models/' + o.name + '.rb')
30
- Columns::ModelCleaner.clean(path)
31
- writer.add_info(o)
32
- end
33
-
5
+ Columns.execute
34
6
 
@@ -1,6 +1,6 @@
1
1
  module Columns
2
2
 
3
- # Public: This class is responsible of removing a possible schema info
3
+ # This class is responsible of removing a possible schema info
4
4
  # from a ruby model file. Note that it isn't any attempt to actually
5
5
  # check if we really deals with a model.
6
6
  class ModelCleaner
@@ -1,6 +1,6 @@
1
1
  module Columns
2
2
 
3
- # Public: Stores data about a model.
3
+ # Stores data about a model.
4
4
  #
5
5
  # Model name
6
6
  # ----------
@@ -20,12 +20,16 @@ module Columns
20
20
  # # integer "foo"
21
21
  class ModelData
22
22
 
23
- # Public: Get the model's name. For a model `app/models/user.rb`,
23
+ # Public: Get the String model's name. For a model `app/models/user.rb`,
24
24
  # the model's name will be `user`.
25
25
  attr_reader :name
26
26
 
27
+ # Public: Get a String formatted content.
27
28
  attr_reader :content
28
29
 
30
+ # Public: Creates a new ModelData.
31
+ #
32
+ # raw_data - A RawData object.
29
33
  def initialize(raw_data)
30
34
  # Ok, this is really idiot, I know, I know. Must use inflectors in
31
35
  # the future.
@@ -1,12 +1,23 @@
1
1
  module Columns
2
2
 
3
- # Public: Add schema info to the end of a ruby model.
3
+ # Add schema info to the end of a ruby model.
4
4
  class ModelWriter
5
5
 
6
+ # Public: Creates a new ModelWriter.
7
+ #
8
+ # path - String relative or absolute path to the models directory.
9
+ # It's handy to be able to change the path during tests.
10
+ # Default is './app/models/'.
6
11
  def initialize(path: './app/models/')
7
12
  @path = path
8
13
  end
9
14
 
15
+ # Public: Puts model data and meta data to the end of a model file.
16
+ #
17
+ # model_data - A ModelData, used to deduce the model file and to
18
+ # grab data.
19
+ #
20
+ # Returns nothing.
10
21
  def add_info(model_data)
11
22
  @model_data = model_data
12
23
  ensure_file_end_with_empty_line
@@ -1,9 +1,20 @@
1
1
  module Columns
2
2
 
3
- # Public: A simple object to store raw data about a table.
3
+ # A simple object to store raw data about a table.
4
4
  class RawData
5
- attr_reader :name, :content
6
5
 
6
+ # Public: Get the String table's name.
7
+ attr_reader :name
8
+
9
+ # Public: Get the String table's content, i.e column's names and
10
+ # types.
11
+ attr_reader :content
12
+
13
+ # Public: Creates a new RawData.
14
+ #
15
+ # name - A String table's name.
16
+ # content - A String content related to the table, directly grabbed
17
+ # from a `schema.rb`.
7
18
  def initialize(name, content)
8
19
  @name = name
9
20
  @content = content
data/lib/columns/table.rb CHANGED
@@ -1,22 +1,19 @@
1
1
  module Columns
2
2
 
3
- # Public: Represents data for the tables found in schema.
3
+ # Represents data for the tables found in schema.
4
4
  class Table
5
5
 
6
- # Public:
6
+ # Public: Find the table names.
7
+ #
8
+ # Returns an Array of String.
9
+ attr_reader :names
10
+
11
+ # Public: Creates a new Table.
7
12
  #
8
13
  # schema - The db/schema.rb as a String.
9
14
  def initialize(schema)
10
- # Records the original schema file. TODO See if it's really needed.
11
- @schema = schema
12
15
  @schema_lines = schema.split("\n")
13
- end
14
-
15
- # Public: Find the table names.
16
- #
17
- # Returns an Array of String.
18
- def names
19
- @schema_lines.map {|line| Regex.table_name(line) }.compact
16
+ @names = @schema_lines.map {|line| Regex.table_name(line) }.compact
20
17
  end
21
18
 
22
19
  # Public: Get the column names and types for a given table.
@@ -1,3 +1,3 @@
1
1
  module Columns
2
- VERSION = "0.0.1"
2
+ VERSION = "0.1.0"
3
3
  end
data/lib/columns.rb CHANGED
@@ -7,5 +7,66 @@ require "columns/model_cleaner"
7
7
  require "columns/model_writer"
8
8
  require "columns/extended_content"
9
9
 
10
+ # Public: Columns annotate your activerecord models from a `schema.rb` file.
11
+ # That could be handy when your application don't use Rails.
10
12
  module Columns
13
+
14
+ # Public: Annotate models.
15
+ #
16
+ # schema_dir - String directory (absolute or relative) to find the
17
+ # `schema.rb` file. Default is `./db/`.
18
+ # models_dir - String directory (absolute or relative) to find the
19
+ # models. Default is `./app/models/`.
20
+ #
21
+ # Returns nothing.
22
+ #
23
+ # Raises SystemExit if the `schema.rb` doesn't exist.
24
+ def self.execute(schema_dir: './db/', models_dir: './app/models/')
25
+ application = Application.new(schema_dir, models_dir)
26
+ application.execute
27
+ end
28
+
29
+ private
30
+
31
+ # The tool chain.
32
+ class Application
33
+
34
+ # Creates a new Application.
35
+ #
36
+ # See Columns.execute for arguments.
37
+ #
38
+ # Raises SystemExit if it can't find the `schema.rb`.
39
+ def initialize(schema_dir, models_dir)
40
+ @models_dir = models_dir
41
+ schema_path = File.expand_path(File.join(schema_dir,'schema.rb'))
42
+
43
+ unless File.exists?(schema_path)
44
+ puts "COLUMNS ERROR : #{schema_path} doesn't exist!"
45
+ exit 1
46
+ end
47
+ table = Table.new(File.read(schema_path))
48
+
49
+ raw_data_objects = []
50
+ table.names.each do |name|
51
+ raw_data_objects << RawData.new(name, table.content_for(name))
52
+ end
53
+
54
+ @model_data_objects = raw_data_objects.map do |object|
55
+ ModelData.new(object)
56
+ end
57
+ end
58
+
59
+ # Cleans then writes models.
60
+ #
61
+ # Returns nothing.
62
+ def execute
63
+ writer = ModelWriter.new(path: @models_dir)
64
+ @model_data_objects.each do |object|
65
+ path = File.expand_path(File.join(@models_dir, object.name) + '.rb')
66
+ ModelCleaner.clean(path)
67
+ writer.add_info(object)
68
+ end
69
+ end
70
+
71
+ end
11
72
  end
@@ -0,0 +1,8 @@
1
+ class User
2
+
3
+ end
4
+
5
+ # == Schema Information
6
+ #
7
+ # integer "id", null: false
8
+ # string "name"
@@ -0,0 +1,35 @@
1
+ require 'spec_helper'
2
+
3
+ describe Columns do
4
+
5
+ describe 'executes the application' do
6
+ before { create_clean_user_rb }
7
+ after { remove_user_rb }
8
+
9
+ specify 'on virgin models', :speed => 'slow' do
10
+ schema_dir = 'spec/fixtures'
11
+ models_dir = 'spec/fixtures'
12
+
13
+ Columns.execute(schema_dir: schema_dir, models_dir: models_dir)
14
+
15
+ expect(user_rb_file).to eq integration_user_filled_file
16
+ end
17
+
18
+ skip 'on already annotated models', :speed => 'slow' do
19
+ end
20
+
21
+ end
22
+
23
+ context 'when there is no schema' do
24
+ it 'exits with status code 1', :speed => 'slow' do
25
+ schema_dir = 'spec/'
26
+ models_dir = 'spec/fixtures'
27
+
28
+ begin
29
+ Columns.execute(schema_dir: schema_dir, models_dir: models_dir)
30
+ rescue SystemExit => ex
31
+ expect(ex.status).to eq 1
32
+ end
33
+ end
34
+ end
35
+ end
@@ -2,10 +2,8 @@ require 'spec_helper'
2
2
 
3
3
  describe ModelWriter do
4
4
 
5
- before do
6
- remove_user_rb
7
- create_clean_user_rb
8
- end
5
+ before { create_clean_user_rb }
6
+ after { remove_user_rb }
9
7
 
10
8
  it 'writes schema info to the end of a file' do
11
9
  writer = ModelWriter.new(path: fixtures_path)
data/spec/spec_helper.rb CHANGED
@@ -5,46 +5,52 @@ require_relative '../lib/columns'
5
5
  include Columns
6
6
 
7
7
  def fixtures_path
8
- File.expand_path(File.dirname(__FILE__) + '/fixtures')
9
- end
8
+ File.expand_path(File.dirname(__FILE__) + '/fixtures')# {{{
9
+ end# }}}
10
10
 
11
11
  def schema_file
12
- file_path = File.expand_path(File.dirname(__FILE__) + '/fixtures/schema.rb')
12
+ file_path = File.expand_path(File.dirname(__FILE__) + '/fixtures/schema.rb')# {{{
13
13
  File.read(file_path)
14
- end
14
+ end# }}}
15
15
 
16
16
  def user_rb_path
17
- File.expand_path(File.dirname(__FILE__) + '/fixtures/user.rb')
18
- end
17
+ File.expand_path(File.dirname(__FILE__) + '/fixtures/user.rb')# {{{
18
+ end# }}}
19
19
 
20
20
  def user_rb_file
21
- file_path = File.expand_path(File.dirname(__FILE__) + '/fixtures/user.rb')
21
+ file_path = File.expand_path(File.dirname(__FILE__) + '/fixtures/user.rb')# {{{
22
22
  File.read(file_path)
23
- end
23
+ end# }}}
24
24
 
25
25
  def user_cleaned_file
26
- file_path = File.expand_path(File.dirname(__FILE__) + '/fixtures/user_cleaned')
26
+ file_path = File.expand_path(File.dirname(__FILE__) + '/fixtures/user_cleaned')# {{{
27
27
  File.read(file_path)
28
- end
28
+ end# }}}
29
29
 
30
30
  def user_filled_file
31
- file_path = File.expand_path(File.dirname(__FILE__) + '/fixtures/user_filled')
31
+ file_path = File.expand_path(File.dirname(__FILE__) + '/fixtures/user_filled')# {{{
32
32
  File.read(file_path)
33
- end
33
+ end# }}}
34
+
35
+ def integration_user_filled_file
36
+ file_path = File.expand_path(File.dirname(__FILE__) +# {{{
37
+ '/fixtures/integration/user_filled')
38
+ File.read(file_path)
39
+ end# }}}
34
40
 
35
41
  def remove_user_rb
36
- file_path = File.expand_path(File.dirname(__FILE__) + '/fixtures/user.rb')
42
+ file_path = File.expand_path(File.dirname(__FILE__) + '/fixtures/user.rb')# {{{
37
43
  FileUtils.rm_f(file_path)
38
- end
44
+ end# }}}
39
45
 
40
46
  def create_user_rb
41
- src = File.expand_path(File.dirname(__FILE__) + '/fixtures/user_filled')
47
+ src = File.expand_path(File.dirname(__FILE__) + '/fixtures/user_filled')# {{{
42
48
  dest = File.expand_path(File.dirname(__FILE__) + '/fixtures/user.rb')
43
49
  FileUtils.cp(src, dest)
44
- end
50
+ end# }}}
45
51
 
46
52
  def create_clean_user_rb
47
- src = File.expand_path(File.dirname(__FILE__) + '/fixtures/user_cleaned')
53
+ src = File.expand_path(File.dirname(__FILE__) + '/fixtures/user_cleaned')# {{{
48
54
  dest = File.expand_path(File.dirname(__FILE__) + '/fixtures/user.rb')
49
55
  FileUtils.cp(src, dest)
50
- end
56
+ end# }}}
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: columns
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - lkdjiin
@@ -106,9 +106,11 @@ files:
106
106
  - lib/columns/table.rb
107
107
  - lib/columns/version.rb
108
108
  - spec/extended_content_spec.rb
109
+ - spec/fixtures/integration/user_filled
109
110
  - spec/fixtures/schema.rb
110
111
  - spec/fixtures/user_cleaned
111
112
  - spec/fixtures/user_filled
113
+ - spec/integration/columns_spec.rb
112
114
  - spec/model_cleaner_spec.rb
113
115
  - spec/model_data_spec.rb
114
116
  - spec/model_writer_spec.rb
@@ -146,9 +148,11 @@ specification_version: 4
146
148
  summary: Annotates activerecord models using `db/schema.rb`.
147
149
  test_files:
148
150
  - spec/extended_content_spec.rb
151
+ - spec/fixtures/integration/user_filled
149
152
  - spec/fixtures/schema.rb
150
153
  - spec/fixtures/user_cleaned
151
154
  - spec/fixtures/user_filled
155
+ - spec/integration/columns_spec.rb
152
156
  - spec/model_cleaner_spec.rb
153
157
  - spec/model_data_spec.rb
154
158
  - spec/model_writer_spec.rb