model_probe 0.0.6 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a018480b4783c49c364d4cc3b4780fef4042a146
4
- data.tar.gz: 78816322d1c24af65c46596094afa9f14175b885
3
+ metadata.gz: 389c5a6af7396bd046a4f70061717ba925abb2cc
4
+ data.tar.gz: 6f87991ffb894e05a9f512e797509606bfcb3f76
5
5
  SHA512:
6
- metadata.gz: 5793273d3efde3e7a3d6fcb17072096cdde1722debc9d6fc6ae0899267b05dcd4c6662e1b46065327d08e4d65ff4fea86a2f19af0b5fdc05d4c654e91cb732b0
7
- data.tar.gz: 702e289f5de4b39203f262242d5518f64363b3ffc0add945e7bf6232472fea80f8ebdeff73a11394061935ca66b5a0abbf8fca4821e03d5ba95541a55fd3babb
6
+ metadata.gz: 40b316189777cd388b52c45aafc3a67a87b2cd0e13c00521403927a93293c86936c51c822159c4979bd680edb8ec0a44705917908ab3d8bc22ecb9f4f89ec61e
7
+ data.tar.gz: 7b1a797aa6404ed9bdf98ee3f450f10217bcc3a37a1642ff19151b1a18f8c01ce454e6a917429c611f66c7ddb51643fa01f8e90fce2957f77e5999ea99b76b5c
data/README.md CHANGED
@@ -25,5 +25,6 @@ Or install it yourself as:
25
25
  ```ruby
26
26
  MyModel.extend ModelProbe
27
27
  MyModel.probe
28
- MyModel.fixture
28
+ MyModel.print_fixture
29
+ MyModel.print_model
29
30
  ```
@@ -1,3 +1,3 @@
1
1
  module ModelProbe
2
- VERSION = "0.0.6"
2
+ VERSION = "1.0.0"
3
3
  end
data/lib/model_probe.rb CHANGED
@@ -4,7 +4,7 @@ require "model_probe/color"
4
4
  module ModelProbe
5
5
  include ModelProbe::Color
6
6
 
7
- # Pretty prints column meta data for an ActiveModel.
7
+ # Pretty prints column meta data for an ActiveModel
8
8
  def probe
9
9
  name_pad = columns.map{ |c| c.name.length }.max + 1
10
10
  type_pad = columns.map{ |c| c.type.length }.max + 2
@@ -24,8 +24,8 @@ module ModelProbe
24
24
  nil
25
25
  end
26
26
 
27
- # Prints a stub that can be used for a test fixture.
28
- def fixture
27
+ # Prints a stub that can be used for a test fixture
28
+ def print_fixture
29
29
  name = self.name.underscore
30
30
  hash = { name => {} }
31
31
 
@@ -38,4 +38,60 @@ module ModelProbe
38
38
  nil
39
39
  end
40
40
 
41
+ # Prints a new model definition based on the database schema
42
+ def print_model
43
+ puts "class #{name} < #{superclass.name}"
44
+ puts " # extends ..................................................................."
45
+ puts " # includes .................................................................."
46
+ puts if relation_columns.size > 0
47
+ puts " # relationships ............................................................."
48
+ relation_columns.sort_by(&:name).each do |column|
49
+ next if column.name == primary_key
50
+ puts " belongs_to :#{column.name.sub(/_id\z/, "")}" if column.name =~ /_id\z/
51
+ end
52
+ puts if relation_columns.size > 0 || validation_columns.size > 0
53
+ puts " # validations ..............................................................."
54
+ validation_columns.sort_by(&:name).each do |column|
55
+ next if column.name == primary_key
56
+ puts " validates :#{column.name} presence: :true" unless column.null
57
+ if %i(text string).include?(column.type) && column.limit.to_i > 0
58
+ puts " validates :#{column.name} length: { maximum: #{column.limit} }"
59
+ end
60
+ end
61
+ puts if validation_columns.size > 0
62
+ puts " # callbacks ................................................................."
63
+ puts " # scopes ...................................................................."
64
+ puts " # additional config (i.e. accepts_nested_attribute_for etc...) .............."
65
+ puts " # class methods ............................................................."
66
+ puts " # public instance methods ..................................................."
67
+ puts " # protected instance methods ................................................"
68
+ puts " # private instance methods .................................................."
69
+ puts "end"
70
+ end
71
+
72
+ private
73
+
74
+ def relation_columns
75
+ @relation_columns ||= begin
76
+ columns.select { |column| relation_column? column }
77
+ end
78
+ end
79
+
80
+ def validation_columns
81
+ @validation_columns ||= begin
82
+ columns.select { |column| validation_column? column }
83
+ end
84
+ end
85
+
86
+ def relation_column?(column)
87
+ return false if column.name == primary_key
88
+ column.name.end_with?("_id")
89
+ end
90
+
91
+ def validation_column?(column)
92
+ return false if column.name == primary_key
93
+ return true if column.null
94
+ %i(text string).include?(column.type) && column.limit.to_i > 0
95
+ end
96
+
41
97
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: model_probe
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nathan Hopkins
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-11-20 00:00:00.000000000 Z
11
+ date: 2016-08-12 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Provides a detailed view of the underlying schema that backs an ActiveModel.
14
14
  email:
@@ -45,7 +45,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
45
45
  version: '0'
46
46
  requirements: []
47
47
  rubyforge_project:
48
- rubygems_version: 2.4.5
48
+ rubygems_version: 2.5.1
49
49
  signing_key:
50
50
  specification_version: 4
51
51
  summary: Schema introspection for ActiveModel.