sdl 0.1.0 → 0.4.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: a6e61a1d5604a66b42b6fb6c1d9ef23cafe16f82
4
- data.tar.gz: 9c0aed1be31e2fd105bdf4797aae965a2470c9cf
3
+ metadata.gz: 293494599d3e932dc85f8057791b79341dcdd0fd
4
+ data.tar.gz: 88bda3e50d8d5ddd4bc1f64785ce41a7cd11f725
5
5
  SHA512:
6
- metadata.gz: c507bebf2605c7dd156219e5ec791660279daf1de15d05cdffcbe5ed2142888c62b657033d6c2b7015565a4824cf5d5282df8919f63169559e9d9d5965c7ddbc
7
- data.tar.gz: 3f0982e9b1785c24a7a5f619bdaa7910f9559310a31008157a9043b2a4dce68172c21b01f4da11992e4022bbb3f711c9feac54ef4ad584cc8cf42bddc9e46375
6
+ metadata.gz: 2b1cfa3f72d1affce168674a9ac1ff081dd8bd69b2bd32ea2ddf510ad40863466a36611887348ac3c8839c7f6a48e1ef055432c55a370e3984f1392851cf84a7
7
+ data.tar.gz: fbab5b8bd7d1976f3ff2af5a55bd5918323afb82d9b230e516fd36b15dbed77e9d19432221156af0e48085d63209a042b7f3ce2b87d31a4acf7b4a90ddd5743f
@@ -1,14 +1,14 @@
1
1
  require "sdl/field"
2
- require "active_support/core_ext/string/inflections"
2
+ require "active_support/inflector"
3
3
 
4
4
  module SDL
5
5
  # Base class for all association types
6
6
  # @abstract
7
7
  class Association < Field
8
8
  # The name of the associated model
9
- # @return [String]
9
+ # @return [Name]
10
10
  def model_name
11
- options.fetch(:model_name, name).to_s
11
+ Name.new(options.fetch(:model_name, name).to_s)
12
12
  end
13
13
  end
14
14
 
@@ -30,9 +30,13 @@ module SDL
30
30
  end
31
31
 
32
32
  # The name of the associated model
33
- # @return [String]
33
+ # @return [Name]
34
34
  def model_name
35
- options.fetch(:model_name) { name.singularize }.to_s
35
+ model_name = options.fetch(:model_name) do
36
+ ActiveSupport::Inflector.singularize(name.to_s)
37
+ end
38
+
39
+ Name.new(model_name.to_s)
36
40
  end
37
41
  end
38
42
 
@@ -51,5 +55,11 @@ module SDL
51
55
  def foreign_key?
52
56
  options.fetch(:foreign_key, false)
53
57
  end
58
+
59
+ # The name of the column
60
+ # @return [Name]
61
+ def column_name
62
+ Name.new("#{name}_id")
63
+ end
54
64
  end
55
65
  end
@@ -14,5 +14,11 @@ module SDL
14
14
  super(name, options)
15
15
  @type = type
16
16
  end
17
+
18
+ # The name of the column
19
+ # @return [Name]
20
+ def column_name
21
+ name
22
+ end
17
23
  end
18
24
  end
@@ -1,4 +1,5 @@
1
1
  require "sdl/field"
2
+ require "sdl/name"
2
3
 
3
4
  module SDL
4
5
  # A field of a {Model} that has a predefined list of possible values
@@ -11,10 +12,22 @@ module SDL
11
12
  :enum
12
13
  end
13
14
 
15
+ # A default value for this field
16
+ # @return [Name]
17
+ def default
18
+ Name.new(options[:default].to_s) if options[:default]
19
+ end
20
+
14
21
  # The possible values for the enum
15
22
  # @return [Array<String>]
16
23
  def values
17
24
  options.fetch(:values, []).map(&:to_s)
18
25
  end
26
+
27
+ # The name of the column
28
+ # @return [Name]
29
+ def column_name
30
+ name
31
+ end
19
32
  end
20
33
  end
@@ -1,3 +1,4 @@
1
+ require "sdl/name"
1
2
  require "sdl/types"
2
3
 
3
4
  module SDL
@@ -5,7 +6,7 @@ module SDL
5
6
  # @abstract
6
7
  class Field
7
8
  # The name of the field
8
- # @return [String]
9
+ # @return [Name]
9
10
  attr_reader :name
10
11
 
11
12
  # All options that were passed to the field
@@ -14,10 +15,54 @@ module SDL
14
15
 
15
16
  # @api private
16
17
  def initialize(name, **options)
17
- @name = name.to_s
18
+ @name = Name.new(name.to_s)
18
19
  @options = options
19
20
  end
20
21
 
22
+ # Indicates that this is an {Attribute}
23
+ # @return [Boolean]
24
+ def attribute?
25
+ !enum? && !association? && !attachment?
26
+ end
27
+
28
+ # Indicates that this is an {Attachment}
29
+ # @return [Boolean]
30
+ def attachment?
31
+ has_one_attached? || has_many_attached?
32
+ end
33
+
34
+ # Indicates that this is an {Association}
35
+ # @return [Boolean]
36
+ def association?
37
+ has_one? || has_many? || belongs_to?
38
+ end
39
+
40
+ # Can this field be null? By default, this is `false`. But, it can
41
+ # be overridden by passing `nullable: true` to a field.
42
+ # @return [Boolean]
43
+ def nullable?
44
+ options.fetch(:nullable, false)
45
+ end
46
+
47
+ # The opposite of {#nullable?}. All fields are required by default
48
+ # @return [Boolean]
49
+ def required?
50
+ !nullable?
51
+ end
52
+
53
+ # The type of the field
54
+ # @abstract
55
+ # @return [Symbol]
56
+ def type
57
+ raise NotImplementedError, __method__
58
+ end
59
+
60
+ # The name of the type
61
+ # @return [Name]
62
+ def type_name
63
+ Name.new(type.to_s)
64
+ end
65
+
21
66
  # @!method id?
22
67
  # Indicates that this is an {Attribute} whose type is `:id`
23
68
  # @return [Boolean]
@@ -73,12 +118,6 @@ module SDL
73
118
  end
74
119
 
75
120
  module ColumnOptions
76
- # Is this field required?
77
- # @return [Boolean]
78
- def required?
79
- options.fetch(:required, false)
80
- end
81
-
82
121
  # A default value for this field
83
122
  # @return [Object]
84
123
  def default
@@ -1,37 +1,35 @@
1
- require "sdl/fields"
2
- require "sdl/attribute"
3
- require "sdl/enum"
4
1
  require "sdl/association"
5
2
  require "sdl/attachment"
3
+ require "sdl/attribute"
4
+ require "sdl/enum"
5
+ require "sdl/name"
6
6
 
7
7
  module SDL
8
8
  class Model
9
9
  # Name of the model
10
- # @return [String]
10
+ # @return [Name]
11
11
  attr_reader :name
12
12
 
13
+ # All of the fields that have been registered
14
+ # @return [Array<Field>]
15
+ attr_reader :fields
16
+
13
17
  # Any additional options
14
18
  # @return [Hash]
15
19
  attr_reader :options
16
20
 
17
21
  # @api private
18
22
  def initialize(name, fields: [], **options, &block)
19
- @name = name.to_s
23
+ @name = Name.new(name.to_s)
20
24
  @fields = fields
21
25
  @options = options
22
26
  instance_eval(&block) if block_given?
23
27
  end
24
28
 
25
- # All of the fields that have been registered
26
- # @return [Fields]
27
- def fields
28
- Fields.new(@fields)
29
- end
30
-
31
29
  # Adds an {Attribute} to the model
32
30
  # @param name [Symbol]
33
31
  # @param type [Symbol]
34
- # @option options [Boolean] :required
32
+ # @option options [Boolean] :nullable
35
33
  # @option options [Boolean] :unique
36
34
  # @option options [Object] :default
37
35
  # @option options [Integer] :limit
@@ -49,7 +47,7 @@ module SDL
49
47
  # Adds an {Enum} to the model
50
48
  # @param name [Symbol]
51
49
  # @option options [Array<Symbol>] :values
52
- # @option options [Boolean] :required
50
+ # @option options [Boolean] :nullable
53
51
  # @option options [Boolean] :unique
54
52
  # @option options [Object] :default
55
53
  #
@@ -64,7 +62,7 @@ module SDL
64
62
  # Adds an {Association::BelongsTo} to the model
65
63
  # @param name [Symbol]
66
64
  # @option options [Symbol] :model_name
67
- # @option options [Boolean] :required
65
+ # @option options [Boolean] :nullable
68
66
  # @option options [Boolean] :unique
69
67
  # @option options [Boolean] :foreign_key
70
68
  #
@@ -79,6 +77,7 @@ module SDL
79
77
  # Adds an {Association::HasOne} to the model
80
78
  # @param name [Symbol]
81
79
  # @option options [Symbol] :model_name
80
+ # @option options [Boolean] :nullable
82
81
  #
83
82
  # @example
84
83
  # model :user do
@@ -102,7 +101,7 @@ module SDL
102
101
 
103
102
  # Adds an {Attachment::HasOne} to the model
104
103
  # @param name [Symbol]
105
- # @option options [Hash]
104
+ # @option options [Symbol] :nullable
106
105
  #
107
106
  # @example
108
107
  # model :user do
@@ -134,5 +133,77 @@ module SDL
134
133
  attribute :created_at, :datetime, required: true
135
134
  attribute :updated_at, :datetime, required: true
136
135
  end
136
+
137
+ # Get all {Attribute} fields
138
+ # @return [Array<Attribute>]
139
+ def attribute_fields
140
+ fields.grep Attribute
141
+ end
142
+
143
+ # Get all {Association} fields
144
+ # @return [Array<Association>]
145
+ def association_fields
146
+ fields.grep Association
147
+ end
148
+
149
+ # Get all {Attachment} fields
150
+ # @return [Array<Attachment>]
151
+ def attachment_fields
152
+ fields.grep Attachment
153
+ end
154
+
155
+ # @!method id_fields
156
+ # Get all {Attribute} fields whose type is `:id`
157
+ # @return [Array<Attribute>]
158
+ # @!method string_fields
159
+ # Get all {Attribute} fields whose type is `:string`
160
+ # @return [Array<Attribute>]
161
+ # @!method boolean_fields
162
+ # Get all {Attribute} fields whose type is `:boolean`
163
+ # @return [Array<Attribute>]
164
+ # @!method integer_fields
165
+ # Get all {Attribute} fields whose type is `:integer`
166
+ # @return [Array<Attribute>]
167
+ # @!method float_fields
168
+ # Get all {Attribute} fields whose type is `:float`
169
+ # @return [Array<Attribute>]
170
+ # @!method decimal_fields
171
+ # Get all {Attribute} fields whose type is `:decimal`
172
+ # @return [Array<Attribute>]
173
+ # @!method date_fields
174
+ # Get all {Attribute} fields whose type is `:date`
175
+ # @return [Array<Attribute>]
176
+ # @!method datetime_fields
177
+ # Get all {Attribute} fields whose type is `:datetime`
178
+ # @return [Array<Attribute>]
179
+ # @!method text_fields
180
+ # Get all {Attribute} fields whose type is `:text`
181
+ # @return [Array<Attribute>]
182
+ # @!method binary_fields
183
+ # Get all {Attribute} fields whose type is `:binary`
184
+ # @return [Array<Attribute>]
185
+ # @!method enum_fields
186
+ # Get all {Enum} fields
187
+ # @return [Array<Enum>]
188
+ # @!method belongs_to_fields
189
+ # Get all {Association::BelongsTo} fields
190
+ # @return [Array<Association::BelongsTo>]
191
+ # @!method has_one_fields
192
+ # Get all {Association::HasOne} fields
193
+ # @return [Array<Assocation::HasOne>]
194
+ # @!method has_many_fields
195
+ # Get all {Association::HasMany} fields
196
+ # @return [Array<Association::HasMany>]
197
+ # @!method has_one_attached_fields
198
+ # Get all {Attachment::HasOne} fields
199
+ # @return [Array<Attachment::HasOne>]
200
+ # @!method has_many_attached_fields
201
+ # Get all {Attachment::HasMany} fields
202
+ # @return [Array<Attachment::HasMany>]
203
+ TYPES.each do |meth|
204
+ define_method "#{meth}_fields" do
205
+ fields.select { |field| field.type == meth }
206
+ end
207
+ end
137
208
  end
138
209
  end
@@ -0,0 +1,44 @@
1
+ require "active_support/inflector"
2
+
3
+ module SDL
4
+ # An extension of a string that will format a name
5
+ # This is especially useful for code generation
6
+ class Name < String
7
+ # @api private
8
+ def self.inflect(name, transforms)
9
+ define_method(name) do
10
+ transforms.reduce(self) do |acc, arg|
11
+ case arg
12
+ when :upcase
13
+ acc.upcase
14
+ when :lower_camelize
15
+ ActiveSupport::Inflector.camelize(acc, false)
16
+ when :lower_humanize
17
+ ActiveSupport::Inflector.humanize(acc, capitalize: false)
18
+ else
19
+ ActiveSupport::Inflector.send(arg, acc)
20
+ end
21
+ end
22
+ end
23
+ end
24
+
25
+ inflect :table, %i[tableize]
26
+ inflect :plural, %i[pluralize]
27
+ inflect :snake, %i[underscore]
28
+ inflect :snakes, %i[underscore pluralize]
29
+ inflect :scream, %i[underscore upcase]
30
+ inflect :screams, %i[pluralize underscore upcase]
31
+ inflect :camel, %i[lower_camelize]
32
+ inflect :camels, %i[pluralize lower_camelize]
33
+ inflect :param, %i[dasherize]
34
+ inflect :params, %i[pluralize dasherize]
35
+ inflect :entity, %i[camelize]
36
+ inflect :entities, %i[pluralize camelize]
37
+ inflect :title, %i[titleize]
38
+ inflect :titles, %i[pluralize titleize]
39
+ inflect :label, %i[humanize]
40
+ inflect :labels, %i[pluralize humanize]
41
+ inflect :description, %i[lower_humanize]
42
+ inflect :descriptions, %i[pluralize lower_humanize]
43
+ end
44
+ end
@@ -4,7 +4,7 @@ require "sdl/enum"
4
4
  require "sdl/attribute"
5
5
  require "sdl/association"
6
6
  require "sdl/attachment"
7
- require "active_support/core_ext/string/inflections"
7
+ require "active_support/inflector"
8
8
 
9
9
  module SDL
10
10
  # The parser takes a string and converts it to a {Field}.
@@ -46,7 +46,7 @@ module SDL
46
46
  # * `has_one_attached`
47
47
  # * `has_many_attached`
48
48
  # * `unique`
49
- # * `required`
49
+ # * `nullable`
50
50
  # * `index`
51
51
  # * `foreign_key`
52
52
  # * `default{value}`
@@ -86,7 +86,7 @@ module SDL
86
86
  ASSOCIATION_WITH_NAME = /^(belongs_to|has_one|has_many)\{(.*)\}$/
87
87
 
88
88
  DEFAULT = /^default\{(.*)\}$/
89
- MODIFIERS = %w[required unique index foreign_key]
89
+ MODIFIERS = %w[nullable unique index foreign_key]
90
90
 
91
91
  def parse!(arg, opts)
92
92
  case arg
@@ -103,12 +103,12 @@ module SDL
103
103
  opts[:type] = Enum
104
104
  opts[:values] = $1.split(SEPARATOR)
105
105
  when ASSOCIATION
106
- opts[:type] = Association.const_get($1.camelize)
106
+ opts[:type] = Association.const_get(camelize($1))
107
107
  when ASSOCIATION_WITH_NAME
108
- opts[:type] = Association.const_get($1.camelize)
108
+ opts[:type] = Association.const_get(camelize($1))
109
109
  opts[:model_name] = $2.to_sym
110
110
  when ATTACHMENT
111
- opts[:type] = Attachment.const_get($1.camelize)
111
+ opts[:type] = Attachment.const_get(camelize($1))
112
112
  when DEFAULT
113
113
  opts[:default] = $1
114
114
  when *MODIFIERS
@@ -118,6 +118,10 @@ module SDL
118
118
  end
119
119
  end
120
120
 
121
+ def camelize(value)
122
+ ActiveSupport::Inflector.camelize(value)
123
+ end
124
+
121
125
  def coerce(value, type)
122
126
  case type
123
127
  when :integer then value.to_i
@@ -37,7 +37,7 @@ module SDL
37
37
  def depsort!
38
38
  each_node = models.method(:each)
39
39
  each_child = lambda do |model, &block|
40
- belongs_to = model.fields.belongs_to.map(&:model_name)
40
+ belongs_to = model.belongs_to_fields.map(&:model_name)
41
41
  children = models.select { |m| belongs_to.include?(m.name) }
42
42
  children.each(&block)
43
43
  end
@@ -1,3 +1,3 @@
1
1
  module SDL
2
- VERSION = "0.1.0"
2
+ VERSION = "0.4.1"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sdl
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ray Zane
@@ -104,8 +104,8 @@ files:
104
104
  - lib/sdl/attribute.rb
105
105
  - lib/sdl/enum.rb
106
106
  - lib/sdl/field.rb
107
- - lib/sdl/fields.rb
108
107
  - lib/sdl/model.rb
108
+ - lib/sdl/name.rb
109
109
  - lib/sdl/parser.rb
110
110
  - lib/sdl/schema.rb
111
111
  - lib/sdl/types.rb
@@ -1,103 +0,0 @@
1
- require "sdl/types"
2
- require "sdl/field"
3
- require "sdl/attribute"
4
- require "sdl/enum"
5
- require "sdl/association"
6
- require "sdl/attachment"
7
-
8
- module SDL
9
- # All fields of a {Model}
10
- class Fields
11
- include Enumerable
12
-
13
- # @api private
14
- def initialize(fields)
15
- @fields = fields
16
- end
17
-
18
- # Get a field by index
19
- # @param index [Integer]
20
- # @return [Field,nil]
21
- def [](index)
22
- @fields[index]
23
- end
24
-
25
- # Iterate over all fields
26
- # @yield [Field]
27
- def each(&block)
28
- @fields.each(&block)
29
- end
30
-
31
- # Get all instances of {Attribute}
32
- # @return [Array<Attribute>]
33
- def attributes
34
- grep Attribute
35
- end
36
-
37
- # Get all instances of {Association}
38
- # @return [Array<Association>]
39
- def associations
40
- grep Association
41
- end
42
-
43
- # Get all instances of {Attachment}
44
- # @return [Array<Attachment>]
45
- def attachments
46
- grep Attachment
47
- end
48
-
49
- # @!method id
50
- # Get all instances of {Attribute} whose type is `:id`
51
- # @return [Array<Attribute>]
52
- # @!method string
53
- # Get all instances of {Attribute} whose type is `:string`
54
- # @return [Array<Attribute>]
55
- # @!method boolean
56
- # Get all instances of {Attribute} whose type is `:boolean`
57
- # @return [Array<Attribute>]
58
- # @!method integer
59
- # Get all instances of {Attribute} whose type is `:integer`
60
- # @return [Array<Attribute>]
61
- # @!method float
62
- # Get all instances of {Attribute} whose type is `:float`
63
- # @return [Array<Attribute>]
64
- # @!method decimal
65
- # Get all instances of {Attribute} whose type is `:decimal`
66
- # @return [Array<Attribute>]
67
- # @!method date
68
- # Get all instances of {Attribute} whose type is `:date`
69
- # @return [Array<Attribute>]
70
- # @!method datetime
71
- # Get all instances of {Attribute} whose type is `:datetime`
72
- # @return [Array<Attribute>]
73
- # @!method text
74
- # Get all instances of {Attribute} whose type is `:text`
75
- # @return [Array<Attribute>]
76
- # @!method binary
77
- # Get all instances of {Attribute} whose type is `:binary`
78
- # @return [Array<Attribute>]
79
- # @!method enum
80
- # Get all instances of {Enum}
81
- # @return [Array<Enum>]
82
- # @!method belongs_to
83
- # Get all instances of {Association::BelongsTo}
84
- # @return [Array<Association::BelongsTo>]
85
- # @!method has_one
86
- # Get all instances of {Association::HasOne}
87
- # @return [Array<Assocation::HasOne>]
88
- # @!method has_many
89
- # Get all instances of {Association::HasMany}
90
- # @return [Array<Association::HasMany>]
91
- # @!method has_one_attached
92
- # Indicates that this is an {Attachment::HasOne}
93
- # @return [Array<Attachment::HasOne>]
94
- # @!method has_many_attached
95
- # Indicates that this is an {Attachment::HasMany}
96
- # @return [Array<Attachment::HasMany>]
97
- TYPES.each do |meth|
98
- define_method meth do
99
- select { |field| field.type == meth }
100
- end
101
- end
102
- end
103
- end