sdl 0.3.0 → 0.4.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: '0097ab232199555efc3955e120f59065251710a2'
4
- data.tar.gz: fc7c8f6704e0b2cd6a7b1930e50078ca1f74b1aa
3
+ metadata.gz: 439056885825969f584e56567236de2cda2e665b
4
+ data.tar.gz: dd4d702e74641a85dbbfb5819e89275bbd50c5bc
5
5
  SHA512:
6
- metadata.gz: c4096110627803a917bd2fe3d39127bea5c2e203f3bd48f53597bb100e1f6b502e250873a4ea6743e7f56303f381d5c13a29c79f8a2453af58139bc4c5fdb306
7
- data.tar.gz: 2fee1715e1f601cb3af618f2158d5d9451af36e3506da0e91665e03264d32af3a9e9b71f2bf24d755a9dd73e094a4cbae33feb5c3327756c121224c757af05e9
6
+ metadata.gz: bb929bd4fd891001eb5c37ba99ae9429f803488c4fca67791eafaf00c44af41d687675ea6475e9bb2554d0fa39b91f9f2cb30eb42c754700cd0067a77b9b85a2
7
+ data.tar.gz: 3551c896ee004cb6e07ed52b5cf488c1222d2fca8c00438bf030ac4af02f85bf84cf345de9950977610a5f87737942b7f0759140fa08359c34e427774e521cd3
@@ -1,14 +1,14 @@
1
1
  require "sdl/field"
2
- require "active_support/core_ext/string/inflections"
2
+ require "active_support/inflector/methods"
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
@@ -12,9 +13,9 @@ module SDL
12
13
  end
13
14
 
14
15
  # A default value for this field
15
- # @return [Object]
16
+ # @return [Name]
16
17
  def default
17
- options[:default]&.to_s
18
+ Name.new(options[:default].to_s) if options[:default]
18
19
  end
19
20
 
20
21
  # The possible values for the enum
@@ -22,5 +23,11 @@ module SDL
22
23
  def values
23
24
  options.fetch(:values, []).map(&:to_s)
24
25
  end
26
+
27
+ # The name of the column
28
+ # @return [Name]
29
+ def column_name
30
+ name
31
+ end
25
32
  end
26
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,7 +15,7 @@ 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
 
@@ -49,6 +50,19 @@ module SDL
49
50
  !nullable?
50
51
  end
51
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
+
52
66
  # @!method id?
53
67
  # Indicates that this is an {Attribute} whose type is `:id`
54
68
  # @return [Boolean]
@@ -1,12 +1,13 @@
1
- require "sdl/attribute"
2
- require "sdl/enum"
3
1
  require "sdl/association"
4
2
  require "sdl/attachment"
3
+ require "sdl/attribute"
4
+ require "sdl/enum"
5
+ require "sdl/name"
5
6
 
6
7
  module SDL
7
8
  class Model
8
9
  # Name of the model
9
- # @return [String]
10
+ # @return [Name]
10
11
  attr_reader :name
11
12
 
12
13
  # All of the fields that have been registered
@@ -19,7 +20,7 @@ module SDL
19
20
 
20
21
  # @api private
21
22
  def initialize(name, fields: [], **options, &block)
22
- @name = name.to_s
23
+ @name = Name.new(name.to_s)
23
24
  @fields = fields
24
25
  @options = options
25
26
  instance_eval(&block) if block_given?
@@ -0,0 +1,43 @@
1
+ require "active_support/inflector/methods"
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 :snake, %i[underscore]
27
+ inflect :snakes, %i[underscore pluralize]
28
+ inflect :scream, %i[underscore upcase]
29
+ inflect :screams, %i[pluralize underscore upcase]
30
+ inflect :camel, %i[lower_camelize]
31
+ inflect :camels, %i[pluralize lower_camelize]
32
+ inflect :param, %i[dasherize]
33
+ inflect :params, %i[pluralize dasherize]
34
+ inflect :entity, %i[camelize]
35
+ inflect :entities, %i[pluralize camelize]
36
+ inflect :title, %i[titleize]
37
+ inflect :titles, %i[pluralize titleize]
38
+ inflect :label, %i[humanize]
39
+ inflect :labels, %i[pluralize humanize]
40
+ inflect :description, %i[lower_humanize]
41
+ inflect :descriptions, %i[pluralize lower_humanize]
42
+ end
43
+ 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/methods"
8
8
 
9
9
  module SDL
10
10
  # The parser takes a string and converts it to a {Field}.
@@ -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
@@ -1,3 +1,3 @@
1
1
  module SDL
2
- VERSION = "0.3.0"
2
+ VERSION = "0.4.0"
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.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ray Zane
@@ -105,6 +105,7 @@ files:
105
105
  - lib/sdl/enum.rb
106
106
  - lib/sdl/field.rb
107
107
  - lib/sdl/model.rb
108
+ - lib/sdl/name.rb
108
109
  - lib/sdl/parser.rb
109
110
  - lib/sdl/schema.rb
110
111
  - lib/sdl/types.rb