ar2gostruct 0.2.0 → 0.2.1

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: aa224af8ae2ec6ea6829496a2e3bdda2539f9a7f
4
- data.tar.gz: 9fdc2a6f65cd429b3fef64b817f82dc816dbff90
3
+ metadata.gz: ed2f1667ec57d8767c34154cba06b76f9acfcca2
4
+ data.tar.gz: f065da8c5d60b56de535d5863e5ce4ed4f701287
5
5
  SHA512:
6
- metadata.gz: b95874bb14f79adb8169afc43b97205357e6dd071b960d44e7dd86d73aeb3c3425ceb023bc56d2ad30b082ed931e2bf38d6d74379d5f19b658f5f17a69571e6b
7
- data.tar.gz: a6db670082617c894c8ae56e0fd1bf6de5cb5df7d412a8ebf9db58ad6468fbac89559896993cdb644a40b5b70fd1a8af07fb8a87071c66f1611eddc45c87c2aa
6
+ metadata.gz: 8aafb9b733b11a883bffb1e17c3208c4e5c9056f13830ac3a673c80c651879044e72854c531eff8a84663144abda7e99055d62551c5010530980acfd565b73d4
7
+ data.tar.gz: d942cb77850cff9743f3b71c26d463466e41a4cbebb09a4dc1a147c6908a38b66f774122d9cf888107e423b93eeddc2586e5cefd82974252085a3487ffa23dab
data/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## 0.2.1
4
+ * Add support for Package validator https://github.com/go-validator/validator
5
+ * Allow multiple orm
6
+
3
7
  ## 0.2.0
4
8
  * Add support for GORM ORM https://github.com/jinzhu/gorm
5
9
  * Ass support Active Record Associations(has_many, has_one, belongs_to)
@@ -16,10 +16,10 @@ module Ar2gostruct
16
16
  # set size
17
17
  if col.type == :string
18
18
  # SQL type
19
- if col.sql_type
19
+ if col.sql_type && /\A\w+\(\d+\)/.match(col.sql_type)
20
20
  orm_option << "type:#{col.sql_type}"
21
21
  end
22
- orm_option << "size:#{col.limit}"
22
+ orm_option << "size:#{col.limit}" if col.limit
23
23
  end
24
24
 
25
25
  if orm_option.present?
@@ -0,0 +1,48 @@
1
+ # validator https://github.com/go-validator/validator
2
+ require "ar2gostruct/builder/orm/base"
3
+ module Ar2gostruct
4
+ module Builder
5
+ module ORM
6
+ class Validator < Base
7
+
8
+ def get_option(col)
9
+ orm_option = []
10
+
11
+ # not null Constraint
12
+ orm_option << "nonzero" unless col.null
13
+
14
+ validators = self.klass.validators_on col.name
15
+
16
+ validators.each do |validator|
17
+ orm_option.concat get_validation_rules(validator)
18
+ end
19
+
20
+ if orm_option.present?
21
+ return "validate:\"#{orm_option.join(TAG_SEPARATOR)}\""
22
+ else
23
+ return nil
24
+ end
25
+ end
26
+
27
+ def get_validation_rules(validator)
28
+ rules = []
29
+ case validator.class.to_s
30
+ # when "ActiveModel::Validations::FormatValidator"
31
+ # if validator.options && validator.options[:with]
32
+ # rules << "regexp=#{validator.options[:with]}"
33
+ # end
34
+ when "ActiveModel::Validations::LengthValidator"
35
+ if validator.options
36
+ rules << "min=#{validator.options[:minimum]}" if validator.options[:maximum]
37
+ rules << "max=#{validator.options[:maximum]}" if validator.options[:maximum]
38
+ end
39
+ end
40
+ return rules
41
+ rescue => e
42
+ []
43
+ end
44
+
45
+ end
46
+ end
47
+ end
48
+ end
@@ -76,13 +76,28 @@ module Ar2gostruct
76
76
 
77
77
  def get_orm_options(col)
78
78
  tags ||= []
79
- builder = "Ar2gostruct::Builder::ORM::#{self.orm.upcase}".constantize.new(self.klass)
80
- option = builder.get_option col
81
- tags << option if option
79
+ self.orm.split(",").each do |orm_name|
80
+ builder = get_orm_builder orm_name
81
+ if builder
82
+ option = builder.get_option col
83
+ tags << option if option
84
+ end
85
+ end
86
+ return tags
82
87
  rescue => e
83
88
  []
84
89
  end
85
90
 
91
+ def get_orm_builder(orm_name)
92
+ prefix = "Ar2gostruct::Builder::ORM::"
93
+ builder = if Object.const_defined?("#{prefix}#{orm_name.upcase}")
94
+ "#{prefix}#{orm_name.upcase}"
95
+ elsif Object.const_defined?("#{prefix}#{orm_name.camelize}")
96
+ "#{prefix}#{orm_name.camelize}"
97
+ end
98
+ return builder.constantize.new(self.klass) if builder
99
+ end
100
+
86
101
  def get_struct_name
87
102
  if self.plural
88
103
  self.klass.table_name.camelize
@@ -1,3 +1,3 @@
1
1
  module Ar2gostruct
2
- VERSION = "0.2.0"
2
+ VERSION = "0.2.1"
3
3
  end
data/lib/ar2gostruct.rb CHANGED
@@ -42,4 +42,5 @@ require "ar2gostruct/gostruct"
42
42
  require "ar2gostruct/builder/association"
43
43
  require "ar2gostruct/builder/orm/gorm"
44
44
  require "ar2gostruct/builder/orm/qbs"
45
+ require "ar2gostruct/builder/orm/validator"
45
46
  require "ar2gostruct/builder/association"
@@ -10,7 +10,7 @@ describe "Ar2gostruct::Builder::ORM::GORM" do
10
10
  expect(result).to eq("sql:\"not null\"")
11
11
  # email
12
12
  result = orm_converter.get_option User.columns[1]
13
- expect(result).to eq("sql:\"not null;type:varchar(255);size:255\"")
13
+ expect(result).to eq("sql:\"not null\"")
14
14
  # sign_in_count
15
15
  result = orm_converter.get_option User.columns[2]
16
16
  expect(result).to eq("sql:\"not null\"")
@@ -0,0 +1,38 @@
1
+ require "spec_helper"
2
+
3
+ describe "Ar2gostruct::Builder::ORM::Validator" do
4
+
5
+ describe "#get_option" do
6
+ it "should output Validator format tags" do
7
+ orm_converter = Ar2gostruct::Builder::ORM::Validator.new User
8
+ # id
9
+ result = orm_converter.get_option User.columns[0]
10
+ expect(result).to eq("validate:\"nonzero\"")
11
+ # email
12
+ result = orm_converter.get_option User.columns[1]
13
+ expect(result).to eq("validate:\"nonzero,min=1,max=120\"")
14
+ # sign_in_count
15
+ result = orm_converter.get_option User.columns[2]
16
+ expect(result).to eq("validate:\"nonzero\"")
17
+ # current_sign_in_at
18
+ result = orm_converter.get_option User.columns[3]
19
+ expect(result).to eq(nil)
20
+ # last_sign_in_at
21
+ result = orm_converter.get_option User.columns[4]
22
+ expect(result).to eq(nil)
23
+ # current_sign_in_ip
24
+ result = orm_converter.get_option User.columns[5]
25
+ expect(result).to eq(nil)
26
+ # last_sign_in_ip
27
+ result = orm_converter.get_option User.columns[6]
28
+ expect(result).to eq(nil)
29
+ # created_at
30
+ result = orm_converter.get_option User.columns[7]
31
+ expect(result).to eq(nil)
32
+ # updated_at
33
+ result = orm_converter.get_option User.columns[8]
34
+ expect(result).to eq(nil)
35
+ end
36
+ end
37
+
38
+ end
@@ -3,6 +3,7 @@ require "active_record"
3
3
  class User < ActiveRecord::Base
4
4
  has_one :profile
5
5
  has_many :projects
6
+ validates :email, length: { minimum: 1, maximum: 120 }
6
7
  end
7
8
 
8
9
  class Profile < ActiveRecord::Base
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ar2gostruct
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tatsuo Kaniwa
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-07-29 00:00:00.000000000 Z
11
+ date: 2015-04-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -90,6 +90,7 @@ files:
90
90
  - lib/ar2gostruct/builder/orm/base.rb
91
91
  - lib/ar2gostruct/builder/orm/gorm.rb
92
92
  - lib/ar2gostruct/builder/orm/qbs.rb
93
+ - lib/ar2gostruct/builder/orm/validator.rb
93
94
  - lib/ar2gostruct/const.rb
94
95
  - lib/ar2gostruct/converter.rb
95
96
  - lib/ar2gostruct/gostruct.rb
@@ -100,6 +101,7 @@ files:
100
101
  - spec/ar2gostruct/builder/association_spec.rb
101
102
  - spec/ar2gostruct/builder/orm/gorm_spec.rb
102
103
  - spec/ar2gostruct/builder/orm/qbs_spec.rb
104
+ - spec/ar2gostruct/builder/orm/validator_spec.rb
103
105
  - spec/db/database.yml
104
106
  - spec/db/migrate/0001_create_tables.rb
105
107
  - spec/spec_helper.rb
@@ -128,7 +130,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
128
130
  version: '0'
129
131
  requirements: []
130
132
  rubyforge_project:
131
- rubygems_version: 2.2.2
133
+ rubygems_version: 2.4.6
132
134
  signing_key:
133
135
  specification_version: 4
134
136
  summary: Generate Go Struct from activerecord models