sdl 0.2.0 → 0.4.2

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: f3c0c72e2ceb5b542143447458c5428edf813ae3
4
- data.tar.gz: 74287f31894980163d4f623578f72ded367dc480
3
+ metadata.gz: a883a3fe7f9d06a141c189ec28b1f67e6c523b37
4
+ data.tar.gz: c22ea329243286c14df082b7db23190c4d0223a9
5
5
  SHA512:
6
- metadata.gz: 9fb06fcea7e7cb5c7eaa711d3c57269508257246afbb7e9eee6c73bfdfb1159c4d30bb3858ced2c50bd3f6fd0ecfbd2fb5d4016c0d9829cbdc28c3709c875b32
7
- data.tar.gz: 83bec3ca9b3c0d44aaf4cd82b3fafb1425e18c04637463deb0645b476729570f9deee94d338a97a892023041fb0f30fffa738aef4e4cbcc911fb67546e206224
6
+ metadata.gz: 7ab655cb02458de96d51682ce9c529da31660f46679190b967a66017a36990e3c003d5ade0fe1bb03ffda435e3c3db67d18419479078ec4fab14924f535c6e91
7
+ data.tar.gz: 95fa9a930d1e683900bd236aca56f3aa299d6908651ac233144d3965e6efac594052878be0cf1ab28a5eb9391ae2b5d25cc34a89075094b3ed91f58325fc7eea
@@ -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
- options.fetch(:values, []).map(&:to_s)
24
+ options.fetch(:values, []).map { |value| Name.new(value.to_s) }
25
+ end
26
+
27
+ # The name of the column
28
+ # @return [Name]
29
+ def column_name
30
+ name
18
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,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
 
@@ -36,6 +37,32 @@ module SDL
36
37
  has_one? || has_many? || belongs_to?
37
38
  end
38
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
+
39
66
  # @!method id?
40
67
  # Indicates that this is an {Attribute} whose type is `:id`
41
68
  # @return [Boolean]
@@ -91,12 +118,6 @@ module SDL
91
118
  end
92
119
 
93
120
  module ColumnOptions
94
- # Is this field required?
95
- # @return [Boolean]
96
- def required?
97
- options.fetch(:required, false)
98
- end
99
-
100
121
  # A default value for this field
101
122
  # @return [Object]
102
123
  def default
@@ -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?
@@ -28,7 +29,7 @@ module SDL
28
29
  # Adds an {Attribute} to the model
29
30
  # @param name [Symbol]
30
31
  # @param type [Symbol]
31
- # @option options [Boolean] :required
32
+ # @option options [Boolean] :nullable
32
33
  # @option options [Boolean] :unique
33
34
  # @option options [Object] :default
34
35
  # @option options [Integer] :limit
@@ -46,7 +47,7 @@ module SDL
46
47
  # Adds an {Enum} to the model
47
48
  # @param name [Symbol]
48
49
  # @option options [Array<Symbol>] :values
49
- # @option options [Boolean] :required
50
+ # @option options [Boolean] :nullable
50
51
  # @option options [Boolean] :unique
51
52
  # @option options [Object] :default
52
53
  #
@@ -61,7 +62,7 @@ module SDL
61
62
  # Adds an {Association::BelongsTo} to the model
62
63
  # @param name [Symbol]
63
64
  # @option options [Symbol] :model_name
64
- # @option options [Boolean] :required
65
+ # @option options [Boolean] :nullable
65
66
  # @option options [Boolean] :unique
66
67
  # @option options [Boolean] :foreign_key
67
68
  #
@@ -76,6 +77,7 @@ module SDL
76
77
  # Adds an {Association::HasOne} to the model
77
78
  # @param name [Symbol]
78
79
  # @option options [Symbol] :model_name
80
+ # @option options [Boolean] :nullable
79
81
  #
80
82
  # @example
81
83
  # model :user do
@@ -99,7 +101,7 @@ module SDL
99
101
 
100
102
  # Adds an {Attachment::HasOne} to the model
101
103
  # @param name [Symbol]
102
- # @option options [Hash]
104
+ # @option options [Symbol] :nullable
103
105
  #
104
106
  # @example
105
107
  # model :user do
@@ -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
@@ -1,3 +1,3 @@
1
1
  module SDL
2
- VERSION = "0.2.0"
2
+ VERSION = "0.4.2"
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.2.0
4
+ version: 0.4.2
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