sdl 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 439056885825969f584e56567236de2cda2e665b
4
+ data.tar.gz: dd4d702e74641a85dbbfb5819e89275bbd50c5bc
5
+ SHA512:
6
+ metadata.gz: bb929bd4fd891001eb5c37ba99ae9429f803488c4fca67791eafaf00c44af41d687675ea6475e9bb2554d0fa39b91f9f2cb30eb42c754700cd0067a77b9b85a2
7
+ data.tar.gz: 3551c896ee004cb6e07ed52b5cf488c1222d2fca8c00438bf030ac4af02f85bf84cf345de9950977610a5f87737942b7f0759140fa08359c34e427774e521cd3
@@ -0,0 +1,13 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
12
+
13
+ Gemfile.lock
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.5.3
7
+ before_install: gem install bundler -v 2.0.2
@@ -0,0 +1,6 @@
1
+ --output-dir=doc/api
2
+ --embed-mixins
3
+ --protected
4
+ --no-private
5
+ --markup-provider=redcarpet
6
+ --markup=markdown
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at raymondzane@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in sdl.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 Ray Zane
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,80 @@
1
+ # SDL
2
+
3
+ SDL is generic schema definition language.
4
+
5
+ Alone, it doesn't do anything useful. However, you might find it to be helpful
6
+ for the purpose of code generation.
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ ```ruby
13
+ gem 'sdl'
14
+ ```
15
+
16
+ And then execute:
17
+
18
+ $ bundle
19
+
20
+ Or install it yourself as:
21
+
22
+ $ gem install sdl
23
+
24
+ ## Examples
25
+
26
+ ### Define a schema
27
+
28
+ ```ruby
29
+ SDL.define do
30
+ model :user do
31
+ attribute :email, :string, required: true, unique: true
32
+ has_many :posts
33
+ timestamps
34
+ end
35
+
36
+ model :post do
37
+ attribute :title, :string, limit: 120, required: true
38
+ attribute :body, :text
39
+ enum :status, values: [:draft, :published]
40
+ belongs_to :user, foreign_key: true
41
+ has_one_attached :image
42
+ timestamps
43
+ end
44
+ end
45
+ ```
46
+
47
+ ### Load a schema from a file
48
+
49
+ ```ruby
50
+ SDL.load_file "schema.rb"
51
+ ```
52
+
53
+ ### Parse command-line arguments
54
+
55
+ ```ruby
56
+ SDL.parse "user", %w[
57
+ email:required:unique
58
+ posts:has_many
59
+ ]
60
+
61
+ SDL.parse "post", %w[
62
+ title:string{120}:required
63
+ body:text
64
+ status:enum{draft,published}
65
+ user:belongs_to:foreign_key
66
+ image:has_one_attached
67
+ ]
68
+ ```
69
+
70
+ ## Contributing
71
+
72
+ Bug reports and pull requests are welcome on GitHub at https://github.com/rzane/sdl. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
73
+
74
+ ## License
75
+
76
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
77
+
78
+ ## Code of Conduct
79
+
80
+ Everyone interacting in the SDL project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/rzane/sdl/blob/master/CODE_OF_CONDUCT.md).
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "sdl"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,57 @@
1
+ require "sdl/version"
2
+ require "sdl/parser"
3
+ require "sdl/schema"
4
+
5
+ module SDL
6
+
7
+ # A base class for all errors
8
+ class Error < StandardError
9
+ end
10
+
11
+ # Raised when the parser encounters an unrecognized directive
12
+ class ParseError < Error
13
+ end
14
+
15
+ # Raised when models can't be sorted by their dependencies
16
+ class CircularDependencyError < Error
17
+ end
18
+
19
+ # Defines a new schema. The block will be evaluated in the
20
+ # context of a {Schema}
21
+ # @return [Schema]
22
+ #
23
+ # @example
24
+ # SDL.define do
25
+ # model :user do
26
+ # attribute :name, :string
27
+ # end
28
+ # end
29
+ def self.define(&block)
30
+ Schema.new(&block)
31
+ end
32
+
33
+ # Loads a schema from a file. The contents of the file
34
+ # will be evaluated in the context of a {Schema}
35
+ # @return [Schema]
36
+ #
37
+ # @example
38
+ # SDL.load_file("schema.rb")
39
+ def self.load_file(file)
40
+ schema = Schema.new
41
+ schema.instance_eval(File.read(file), file)
42
+ schema
43
+ end
44
+
45
+ # Constructs a model from command-line arguments
46
+ # @raise [ParseError]
47
+ # @return [Model]
48
+ # @see Parser
49
+ #
50
+ # @example
51
+ # SDL.parse("user", ["name", "email:required:unique"])
52
+ def self.parse(name, fields)
53
+ parser = Parser.new
54
+ fields = fields.map { |field| parser.parse(field) }
55
+ Model.new(name, fields: fields)
56
+ end
57
+ end
@@ -0,0 +1,65 @@
1
+ require "sdl/field"
2
+ require "active_support/inflector/methods"
3
+
4
+ module SDL
5
+ # Base class for all association types
6
+ # @abstract
7
+ class Association < Field
8
+ # The name of the associated model
9
+ # @return [Name]
10
+ def model_name
11
+ Name.new(options.fetch(:model_name, name).to_s)
12
+ end
13
+ end
14
+
15
+ # Indicates a one-to-one connection with another {Model}
16
+ class Association::HasOne < Association
17
+ # The type of field
18
+ # @return [Symbol]
19
+ def type
20
+ :has_one
21
+ end
22
+ end
23
+
24
+ # Indicates a one-to-many connection with another {Model}
25
+ class Association::HasMany < Association
26
+ # The type of field
27
+ # @return [Symbol]
28
+ def type
29
+ :has_many
30
+ end
31
+
32
+ # The name of the associated model
33
+ # @return [Name]
34
+ def model_name
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)
40
+ end
41
+ end
42
+
43
+ # Indicates a one-to-one connection with another {Model}
44
+ class Association::BelongsTo < Association
45
+ include Field::ColumnOptions
46
+
47
+ # The type of field
48
+ # @return [Symbol]
49
+ def type
50
+ :belongs_to
51
+ end
52
+
53
+ # Should this field have a foreign key?
54
+ # @return [Boolean]
55
+ def foreign_key?
56
+ options.fetch(:foreign_key, false)
57
+ end
58
+
59
+ # The name of the column
60
+ # @return [Name]
61
+ def column_name
62
+ Name.new("#{name}_id")
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,26 @@
1
+ require "sdl/field"
2
+
3
+ module SDL
4
+ # Base class for all attachment types
5
+ # @abstract
6
+ class Attachment < Field
7
+ end
8
+
9
+ # A file that is attached to a {Model}
10
+ class Attachment::HasOne < Attachment
11
+ # The type of field
12
+ # @return [Symbol]
13
+ def type
14
+ :has_one_attached
15
+ end
16
+ end
17
+
18
+ # A list of files that are attached to a {Model}
19
+ class Attachment::HasMany < Attachment
20
+ # The type of field
21
+ # @return [Symbol]
22
+ def type
23
+ :has_many_attached
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,24 @@
1
+ require "sdl/field"
2
+
3
+ module SDL
4
+ # An attribute of a {Model}
5
+ class Attribute < Field
6
+ include Field::ColumnOptions
7
+
8
+ # The type of field
9
+ # @return [Symbol]
10
+ attr_reader :type
11
+
12
+ # @api private
13
+ def initialize(name, type, **options)
14
+ super(name, options)
15
+ @type = type
16
+ end
17
+
18
+ # The name of the column
19
+ # @return [Name]
20
+ def column_name
21
+ name
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,33 @@
1
+ require "sdl/field"
2
+ require "sdl/name"
3
+
4
+ module SDL
5
+ # A field of a {Model} that has a predefined list of possible values
6
+ class Enum < Field
7
+ include Field::ColumnOptions
8
+
9
+ # The type of field
10
+ # @return [Symbol]
11
+ def type
12
+ :enum
13
+ end
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
+
21
+ # The possible values for the enum
22
+ # @return [Array<String>]
23
+ def values
24
+ options.fetch(:values, []).map(&:to_s)
25
+ end
26
+
27
+ # The name of the column
28
+ # @return [Name]
29
+ def column_name
30
+ name
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,158 @@
1
+ require "sdl/name"
2
+ require "sdl/types"
3
+
4
+ module SDL
5
+ # A base class for all fields of a model
6
+ # @abstract
7
+ class Field
8
+ # The name of the field
9
+ # @return [Name]
10
+ attr_reader :name
11
+
12
+ # All options that were passed to the field
13
+ # @return [Hash]
14
+ attr_reader :options
15
+
16
+ # @api private
17
+ def initialize(name, **options)
18
+ @name = Name.new(name.to_s)
19
+ @options = options
20
+ end
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
+
66
+ # @!method id?
67
+ # Indicates that this is an {Attribute} whose type is `:id`
68
+ # @return [Boolean]
69
+ # @!method string?
70
+ # Indicates that this is an {Attribute} whose type is `:string`
71
+ # @return [Boolean]
72
+ # @!method boolean?
73
+ # Indicates that this is an {Attribute} whose type is `:boolean`
74
+ # @return [Boolean]
75
+ # @!method integer?
76
+ # Indicates that this is an {Attribute} whose type is `:integer`
77
+ # @return [Boolean]
78
+ # @!method float?
79
+ # Indicates that this is an {Attribute} whose type is `:float`
80
+ # @return [Boolean]
81
+ # @!method decimal?
82
+ # Indicates that this is an {Attribute} whose type is `:decimal`
83
+ # @return [Boolean]
84
+ # @!method date?
85
+ # Indicates that this is an {Attribute} whose type is `:date`
86
+ # @return [Boolean]
87
+ # @!method datetime?
88
+ # Indicates that this is an {Attribute} whose type is `:datetime`
89
+ # @return [Boolean]
90
+ # @!method text?
91
+ # Indicates that this is an {Attribute} whose type is `:text`
92
+ # @return [Boolean]
93
+ # @!method binary?
94
+ # Indicates that this is an {Attribute} whose type is `:binary`
95
+ # @return [Boolean]
96
+ # @!method enum?
97
+ # Indicates that this is an {Enum}
98
+ # @return [Boolean]
99
+ # @!method belongs_to?
100
+ # Indicates that this is an {Association::BelongsTo}
101
+ # @return [Boolean]
102
+ # @!method has_one?
103
+ # Indicates that this is an {Association::HasOne}
104
+ # @return [Boolean]
105
+ # @!method has_many?
106
+ # Indicates that this is an {Association::HasMany}
107
+ # @return [Boolean]
108
+ # @!method has_one_attached?
109
+ # Indicates that this is an {Attachment::HasOne}
110
+ # @return [Boolean]
111
+ # @!method has_many_attached?
112
+ # Indicates that this is an {Attachment::HasMany}
113
+ # @return [Boolean]
114
+ TYPES.each do |meth|
115
+ define_method "#{meth}?" do
116
+ type == meth
117
+ end
118
+ end
119
+
120
+ module ColumnOptions
121
+ # A default value for this field
122
+ # @return [Object]
123
+ def default
124
+ options[:default]
125
+ end
126
+
127
+ # A limit for the field
128
+ # @return [Integer]
129
+ def limit
130
+ options[:limit]
131
+ end
132
+
133
+ # A precision for the field
134
+ # @return [Integer]
135
+ def precision
136
+ options[:precision]
137
+ end
138
+
139
+ # A scale for the field
140
+ # @return [Integer]
141
+ def scale
142
+ options[:scale]
143
+ end
144
+
145
+ # Is this field unique?
146
+ # @return [Boolean]
147
+ def unique?
148
+ options.fetch(:unique, false)
149
+ end
150
+
151
+ # Should this field have an index?
152
+ # @return [Boolean]
153
+ def index?
154
+ options.fetch(:index, false)
155
+ end
156
+ end
157
+ end
158
+ end
@@ -0,0 +1,209 @@
1
+ require "sdl/association"
2
+ require "sdl/attachment"
3
+ require "sdl/attribute"
4
+ require "sdl/enum"
5
+ require "sdl/name"
6
+
7
+ module SDL
8
+ class Model
9
+ # Name of the model
10
+ # @return [Name]
11
+ attr_reader :name
12
+
13
+ # All of the fields that have been registered
14
+ # @return [Array<Field>]
15
+ attr_reader :fields
16
+
17
+ # Any additional options
18
+ # @return [Hash]
19
+ attr_reader :options
20
+
21
+ # @api private
22
+ def initialize(name, fields: [], **options, &block)
23
+ @name = Name.new(name.to_s)
24
+ @fields = fields
25
+ @options = options
26
+ instance_eval(&block) if block_given?
27
+ end
28
+
29
+ # Adds an {Attribute} to the model
30
+ # @param name [Symbol]
31
+ # @param type [Symbol]
32
+ # @option options [Boolean] :nullable
33
+ # @option options [Boolean] :unique
34
+ # @option options [Object] :default
35
+ # @option options [Integer] :limit
36
+ # @option options [Integer] :precision
37
+ # @option options [Integer] :scale
38
+ #
39
+ # @example
40
+ # model :user do
41
+ # attribute :name, :string, required: true
42
+ # end
43
+ def attribute(name, type, **options)
44
+ @fields << Attribute.new(*name, type, **options)
45
+ end
46
+
47
+ # Adds an {Enum} to the model
48
+ # @param name [Symbol]
49
+ # @option options [Array<Symbol>] :values
50
+ # @option options [Boolean] :nullable
51
+ # @option options [Boolean] :unique
52
+ # @option options [Object] :default
53
+ #
54
+ # @example
55
+ # model :user do
56
+ # enum :status, values: [:accepted, :rejected], required: true
57
+ # end
58
+ def enum(name, **options)
59
+ @fields << Enum.new(name, **options)
60
+ end
61
+
62
+ # Adds an {Association::BelongsTo} to the model
63
+ # @param name [Symbol]
64
+ # @option options [Symbol] :model_name
65
+ # @option options [Boolean] :nullable
66
+ # @option options [Boolean] :unique
67
+ # @option options [Boolean] :foreign_key
68
+ #
69
+ # @example
70
+ # model :user do
71
+ # belongs_to :organization, required: true
72
+ # end
73
+ def belongs_to(name, **options)
74
+ @fields << Association::BelongsTo.new(name, **options)
75
+ end
76
+
77
+ # Adds an {Association::HasOne} to the model
78
+ # @param name [Symbol]
79
+ # @option options [Symbol] :model_name
80
+ # @option options [Boolean] :nullable
81
+ #
82
+ # @example
83
+ # model :user do
84
+ # has_one :profile
85
+ # end
86
+ def has_one(name, **options)
87
+ @fields << Association::HasOne.new(name, **options)
88
+ end
89
+
90
+ # Adds an {Association::HasMany} to the model
91
+ # @param name [Symbol]
92
+ # @option options [Symbol] :model_name
93
+ #
94
+ # @example
95
+ # model :user do
96
+ # has_many :devices
97
+ # end
98
+ def has_many(name, **options)
99
+ @fields << Association::HasMany.new(name, **options)
100
+ end
101
+
102
+ # Adds an {Attachment::HasOne} to the model
103
+ # @param name [Symbol]
104
+ # @option options [Symbol] :nullable
105
+ #
106
+ # @example
107
+ # model :user do
108
+ # has_one_attached :avatar
109
+ # end
110
+ def has_one_attached(name, **options)
111
+ @fields << Attachment::HasOne.new(name, **options)
112
+ end
113
+
114
+ # Adds an {Attachment::HasMany} to the model
115
+ # @param name [Symbol]
116
+ # @param options [Hash]
117
+ #
118
+ # @example
119
+ # model :product do
120
+ # has_many_attached :images
121
+ # end
122
+ def has_many_attached(name, **options)
123
+ @fields << Attachment::HasMany.new(name, **options)
124
+ end
125
+
126
+ # Adds attributes for +:created_at+ and +:updated_at+ to the model
127
+ #
128
+ # @example
129
+ # model :user do
130
+ # timestamps
131
+ # end
132
+ def timestamps
133
+ attribute :created_at, :datetime, required: true
134
+ attribute :updated_at, :datetime, required: true
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
208
+ end
209
+ end
@@ -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
@@ -0,0 +1,135 @@
1
+ require "sdl/types"
2
+ require "sdl/field"
3
+ require "sdl/enum"
4
+ require "sdl/attribute"
5
+ require "sdl/association"
6
+ require "sdl/attachment"
7
+ require "active_support/inflector/methods"
8
+
9
+ module SDL
10
+ # The parser takes a string and converts it to a {Field}.
11
+ # A field is described using a series of directives separated
12
+ # by a colon.
13
+ #
14
+ # Examples:
15
+ #
16
+ # * `title:string{120}:required`
17
+ # * `body:text`
18
+ # * `status:enum{draft,published}`
19
+ # * `user:belongs_to:foreign_key`
20
+ # * `image:has_one_attached`
21
+ #
22
+ # Available directives:
23
+ #
24
+ # * `id`
25
+ # * `string`
26
+ # * `string{limit}`
27
+ # * `boolean`
28
+ # * `integer`
29
+ # * `integer{limit}`
30
+ # * `float`
31
+ # * `decimal`
32
+ # * `decimal{precision,scale}`
33
+ # * `date`
34
+ # * `datetime`
35
+ # * `text`
36
+ # * `text{limit}`
37
+ # * `binary`
38
+ # * `binary{limit}`
39
+ # * `enum{value,...}`
40
+ # * `belongs_to`
41
+ # * `belongs_to{model}`
42
+ # * `has_one`
43
+ # * `has_one{model}`
44
+ # * `has_many`
45
+ # * `has_many{model}`
46
+ # * `has_one_attached`
47
+ # * `has_many_attached`
48
+ # * `unique`
49
+ # * `nullable`
50
+ # * `index`
51
+ # * `foreign_key`
52
+ # * `default{value}`
53
+ class Parser
54
+ # Parses a string into a {Field}
55
+ # @param value [String]
56
+ # @raise [ParseError] when a directive is not recognized
57
+ # @return [Field]
58
+ def parse(value)
59
+ name, *args = value.split(":")
60
+ name = name.to_sym
61
+
62
+ opts = {type: :string}
63
+ args.each { |arg| parse!(arg, opts) }
64
+ type = opts.delete(:type)
65
+
66
+ # Attempt to coerce the default
67
+ opts[:default] = coerce(opts[:default], type) if opts[:default]
68
+
69
+ if type.is_a?(Symbol)
70
+ Attribute.new(name, type, opts)
71
+ else
72
+ type.new(name, opts)
73
+ end
74
+ end
75
+
76
+ private
77
+
78
+ SEPARATOR = /[,.-]/
79
+ TYPES = /^(#{SCALAR_TYPES.join("|")})$/
80
+ TYPES_WITH_LIMIT = /^(string|text|binary|integer)\{(\d+)\}$/
81
+ TYPES_WITH_PRECISION = /^(decimal)\{(\d+)#{SEPARATOR}(\d+)\}$/
82
+
83
+ ENUM = /^enum\{(.*)\}$/
84
+ ATTACHMENT = /^(has_one|has_many)_attached$/
85
+ ASSOCIATION = /^(belongs_to|has_one|has_many)$/
86
+ ASSOCIATION_WITH_NAME = /^(belongs_to|has_one|has_many)\{(.*)\}$/
87
+
88
+ DEFAULT = /^default\{(.*)\}$/
89
+ MODIFIERS = %w[nullable unique index foreign_key]
90
+
91
+ def parse!(arg, opts)
92
+ case arg
93
+ when TYPES
94
+ opts[:type] = $1.to_sym
95
+ when TYPES_WITH_LIMIT
96
+ opts[:type] = $1.to_sym
97
+ opts[:limit] = $2.to_i
98
+ when TYPES_WITH_PRECISION
99
+ opts[:type] = $1.to_sym
100
+ opts[:precision] = $2.to_i
101
+ opts[:scale] = $3.to_i
102
+ when ENUM
103
+ opts[:type] = Enum
104
+ opts[:values] = $1.split(SEPARATOR)
105
+ when ASSOCIATION
106
+ opts[:type] = Association.const_get(camelize($1))
107
+ when ASSOCIATION_WITH_NAME
108
+ opts[:type] = Association.const_get(camelize($1))
109
+ opts[:model_name] = $2.to_sym
110
+ when ATTACHMENT
111
+ opts[:type] = Attachment.const_get(camelize($1))
112
+ when DEFAULT
113
+ opts[:default] = $1
114
+ when *MODIFIERS
115
+ opts[arg.to_sym] = true
116
+ else
117
+ raise ParseError, "Unrecognized parameter: #{arg}"
118
+ end
119
+ end
120
+
121
+ def camelize(value)
122
+ ActiveSupport::Inflector.camelize(value)
123
+ end
124
+
125
+ def coerce(value, type)
126
+ case type
127
+ when :integer then value.to_i
128
+ when :float then value.to_f
129
+ when :boolean then value.match?(/^true$/)
130
+ else
131
+ value
132
+ end
133
+ end
134
+ end
135
+ end
@@ -0,0 +1,51 @@
1
+ require "tsort"
2
+ require "sdl/model"
3
+
4
+ module SDL
5
+ class Schema
6
+ # All models that have been defined
7
+ # @return [Array<Model>]
8
+ attr_reader :models
9
+
10
+ # @api private
11
+ def initialize(&block)
12
+ @models = []
13
+ instance_eval(&block) if block_given?
14
+ end
15
+
16
+ # Find a model by name
17
+ # @param name [Symbol,String] name of the model to find
18
+ # @return [Model,nil]
19
+ def find_model(name)
20
+ models.find { |m| m.name == name.to_s }
21
+ end
22
+
23
+ # Adds a new {Model} to the schema
24
+ # @param name [Symbol]
25
+ # @param options [Hash]
26
+ #
27
+ # @example
28
+ # model :user do
29
+ # attribute :name, :string
30
+ # end
31
+ def model(name, **options, &block)
32
+ @models << Model.new(name, **options, &block)
33
+ end
34
+
35
+ # Sort all {Model} instances in order of dependency
36
+ # @raise [CircularDependencyError]
37
+ def depsort!
38
+ each_node = models.method(:each)
39
+ each_child = lambda do |model, &block|
40
+ belongs_to = model.belongs_to_fields.map(&:model_name)
41
+ children = models.select { |m| belongs_to.include?(m.name) }
42
+ children.each(&block)
43
+ end
44
+
45
+ @models = TSort.tsort(each_node, each_child)
46
+ @models
47
+ rescue TSort::Cyclic
48
+ raise CircularDependencyError, "The schema contains a circular dependency."
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,23 @@
1
+ module SDL
2
+ SCALAR_TYPES = %i[
3
+ id
4
+ string
5
+ boolean
6
+ integer
7
+ float
8
+ decimal
9
+ date
10
+ datetime
11
+ text
12
+ binary
13
+ ]
14
+
15
+ TYPES = SCALAR_TYPES + %i[
16
+ enum
17
+ belongs_to
18
+ has_one
19
+ has_many
20
+ has_one_attached
21
+ has_many_attached
22
+ ]
23
+ end
@@ -0,0 +1,3 @@
1
+ module SDL
2
+ VERSION = "0.4.0"
3
+ end
@@ -0,0 +1,34 @@
1
+ lib = File.expand_path("lib", __dir__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require "sdl/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "sdl"
7
+ spec.version = SDL::VERSION
8
+ spec.authors = ["Ray Zane"]
9
+ spec.email = ["raymondzane@gmail.com"]
10
+
11
+ spec.summary = %q{A generic schema definition language.}
12
+ spec.homepage = "https://github.com/rzane/sdl"
13
+ spec.license = "MIT"
14
+
15
+ spec.metadata["homepage_uri"] = spec.homepage
16
+ spec.metadata["source_code_uri"] = "https://github.com/rzane/sdl"
17
+ spec.metadata["documentation_uri"] = "https://rubydoc.info/github/rzane/sdl"
18
+
19
+ # Specify which files should be added to the gem when it is released.
20
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
21
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
22
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
23
+ end
24
+ spec.bindir = "exe"
25
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
26
+ spec.require_paths = ["lib"]
27
+
28
+ spec.add_dependency "activesupport", "~> 6.0"
29
+
30
+ spec.add_development_dependency "bundler", "~> 2.0"
31
+ spec.add_development_dependency "rake", "~> 10.0"
32
+ spec.add_development_dependency "rspec", "~> 3.0"
33
+ spec.add_development_dependency "simplecov", "~> 0.17.1"
34
+ end
metadata ADDED
@@ -0,0 +1,141 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sdl
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.4.0
5
+ platform: ruby
6
+ authors:
7
+ - Ray Zane
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-12-16 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '6.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '6.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '2.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '2.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: simplecov
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 0.17.1
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 0.17.1
83
+ description:
84
+ email:
85
+ - raymondzane@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".rspec"
92
+ - ".travis.yml"
93
+ - ".yardopts"
94
+ - CODE_OF_CONDUCT.md
95
+ - Gemfile
96
+ - LICENSE.txt
97
+ - README.md
98
+ - Rakefile
99
+ - bin/console
100
+ - bin/setup
101
+ - lib/sdl.rb
102
+ - lib/sdl/association.rb
103
+ - lib/sdl/attachment.rb
104
+ - lib/sdl/attribute.rb
105
+ - lib/sdl/enum.rb
106
+ - lib/sdl/field.rb
107
+ - lib/sdl/model.rb
108
+ - lib/sdl/name.rb
109
+ - lib/sdl/parser.rb
110
+ - lib/sdl/schema.rb
111
+ - lib/sdl/types.rb
112
+ - lib/sdl/version.rb
113
+ - sdl.gemspec
114
+ homepage: https://github.com/rzane/sdl
115
+ licenses:
116
+ - MIT
117
+ metadata:
118
+ homepage_uri: https://github.com/rzane/sdl
119
+ source_code_uri: https://github.com/rzane/sdl
120
+ documentation_uri: https://rubydoc.info/github/rzane/sdl
121
+ post_install_message:
122
+ rdoc_options: []
123
+ require_paths:
124
+ - lib
125
+ required_ruby_version: !ruby/object:Gem::Requirement
126
+ requirements:
127
+ - - ">="
128
+ - !ruby/object:Gem::Version
129
+ version: '0'
130
+ required_rubygems_version: !ruby/object:Gem::Requirement
131
+ requirements:
132
+ - - ">="
133
+ - !ruby/object:Gem::Version
134
+ version: '0'
135
+ requirements: []
136
+ rubyforge_project:
137
+ rubygems_version: 2.6.14
138
+ signing_key:
139
+ specification_version: 4
140
+ summary: A generic schema definition language.
141
+ test_files: []