sdl 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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 33252bd783b69ae9ac6fb9e579817a9392dcd3a7
4
+ data.tar.gz: aaf572106a7f32f494ea0e4d050e7e5894e70de8
5
+ SHA512:
6
+ metadata.gz: f4066429d325386c0c79b1b53443ae61f30530a81c87da81b1663ebf2fb4aacfd10132700c13791a0eba3573f1141916e36f588ae7073c41c7d127f5cb5804a7
7
+ data.tar.gz: 55bb962046a882e09bee9cb98eeb2ee3afb7c633a6148986f23d7671f638be91aa111b2b077b561b4486dd174b86137a8eb5d002911240e067fc8dc73284487f
@@ -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,55 @@
1
+ require "sdl/field"
2
+ require "active_support/core_ext/string/inflections"
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 [String]
10
+ def model_name
11
+ 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 [String]
34
+ def model_name
35
+ options.fetch(:model_name) { name.singularize }.to_s
36
+ end
37
+ end
38
+
39
+ # Indicates a one-to-one connection with another {Model}
40
+ class Association::BelongsTo < Association
41
+ include Field::ColumnOptions
42
+
43
+ # The type of field
44
+ # @return [Symbol]
45
+ def type
46
+ :belongs_to
47
+ end
48
+
49
+ # Should this field have a foreign key?
50
+ # @return [Boolean]
51
+ def foreign_key?
52
+ options.fetch(:foreign_key, false)
53
+ end
54
+ end
55
+ 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,18 @@
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
+ end
18
+ end
@@ -0,0 +1,26 @@
1
+ require "sdl/field"
2
+
3
+ module SDL
4
+ # A field of a {Model} that has a predefined list of possible values
5
+ class Enum < Field
6
+ include Field::ColumnOptions
7
+
8
+ # The type of field
9
+ # @return [Symbol]
10
+ def type
11
+ :enum
12
+ end
13
+
14
+ # A default value for this field
15
+ # @return [Object]
16
+ def default
17
+ options[:default]&.to_s
18
+ end
19
+
20
+ # The possible values for the enum
21
+ # @return [Array<String>]
22
+ def values
23
+ options.fetch(:values, []).map(&:to_s)
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,137 @@
1
+ require "sdl/types"
2
+
3
+ module SDL
4
+ # A base class for all fields of a model
5
+ # @abstract
6
+ class Field
7
+ # The name of the field
8
+ # @return [String]
9
+ attr_reader :name
10
+
11
+ # All options that were passed to the field
12
+ # @return [Hash]
13
+ attr_reader :options
14
+
15
+ # @api private
16
+ def initialize(name, **options)
17
+ @name = name.to_s
18
+ @options = options
19
+ end
20
+
21
+ # Indicates that this is an {Attribute}
22
+ # @return [Boolean]
23
+ def attribute?
24
+ !enum? && !association? && !attachment?
25
+ end
26
+
27
+ # Indicates that this is an {Attachment}
28
+ # @return [Boolean]
29
+ def attachment?
30
+ has_one_attached? || has_many_attached?
31
+ end
32
+
33
+ # Indicates that this is an {Association}
34
+ # @return [Boolean]
35
+ def association?
36
+ has_one? || has_many? || belongs_to?
37
+ end
38
+
39
+ # @!method id?
40
+ # Indicates that this is an {Attribute} whose type is `:id`
41
+ # @return [Boolean]
42
+ # @!method string?
43
+ # Indicates that this is an {Attribute} whose type is `:string`
44
+ # @return [Boolean]
45
+ # @!method boolean?
46
+ # Indicates that this is an {Attribute} whose type is `:boolean`
47
+ # @return [Boolean]
48
+ # @!method integer?
49
+ # Indicates that this is an {Attribute} whose type is `:integer`
50
+ # @return [Boolean]
51
+ # @!method float?
52
+ # Indicates that this is an {Attribute} whose type is `:float`
53
+ # @return [Boolean]
54
+ # @!method decimal?
55
+ # Indicates that this is an {Attribute} whose type is `:decimal`
56
+ # @return [Boolean]
57
+ # @!method date?
58
+ # Indicates that this is an {Attribute} whose type is `:date`
59
+ # @return [Boolean]
60
+ # @!method datetime?
61
+ # Indicates that this is an {Attribute} whose type is `:datetime`
62
+ # @return [Boolean]
63
+ # @!method text?
64
+ # Indicates that this is an {Attribute} whose type is `:text`
65
+ # @return [Boolean]
66
+ # @!method binary?
67
+ # Indicates that this is an {Attribute} whose type is `:binary`
68
+ # @return [Boolean]
69
+ # @!method enum?
70
+ # Indicates that this is an {Enum}
71
+ # @return [Boolean]
72
+ # @!method belongs_to?
73
+ # Indicates that this is an {Association::BelongsTo}
74
+ # @return [Boolean]
75
+ # @!method has_one?
76
+ # Indicates that this is an {Association::HasOne}
77
+ # @return [Boolean]
78
+ # @!method has_many?
79
+ # Indicates that this is an {Association::HasMany}
80
+ # @return [Boolean]
81
+ # @!method has_one_attached?
82
+ # Indicates that this is an {Attachment::HasOne}
83
+ # @return [Boolean]
84
+ # @!method has_many_attached?
85
+ # Indicates that this is an {Attachment::HasMany}
86
+ # @return [Boolean]
87
+ TYPES.each do |meth|
88
+ define_method "#{meth}?" do
89
+ type == meth
90
+ end
91
+ end
92
+
93
+ module ColumnOptions
94
+ # Is this field required?
95
+ # @return [Boolean]
96
+ def required?
97
+ options.fetch(:required, false)
98
+ end
99
+
100
+ # A default value for this field
101
+ # @return [Object]
102
+ def default
103
+ options[:default]
104
+ end
105
+
106
+ # A limit for the field
107
+ # @return [Integer]
108
+ def limit
109
+ options[:limit]
110
+ end
111
+
112
+ # A precision for the field
113
+ # @return [Integer]
114
+ def precision
115
+ options[:precision]
116
+ end
117
+
118
+ # A scale for the field
119
+ # @return [Integer]
120
+ def scale
121
+ options[:scale]
122
+ end
123
+
124
+ # Is this field unique?
125
+ # @return [Boolean]
126
+ def unique?
127
+ options.fetch(:unique, false)
128
+ end
129
+
130
+ # Should this field have an index?
131
+ # @return [Boolean]
132
+ def index?
133
+ options.fetch(:index, false)
134
+ end
135
+ end
136
+ end
137
+ end
@@ -0,0 +1,207 @@
1
+ require "sdl/attribute"
2
+ require "sdl/enum"
3
+ require "sdl/association"
4
+ require "sdl/attachment"
5
+
6
+ module SDL
7
+ class Model
8
+ # Name of the model
9
+ # @return [String]
10
+ attr_reader :name
11
+
12
+ # All of the fields that have been registered
13
+ # @return [Array<Field>]
14
+ attr_reader :fields
15
+
16
+ # Any additional options
17
+ # @return [Hash]
18
+ attr_reader :options
19
+
20
+ # @api private
21
+ def initialize(name, fields: [], **options, &block)
22
+ @name = name.to_s
23
+ @fields = fields
24
+ @options = options
25
+ instance_eval(&block) if block_given?
26
+ end
27
+
28
+ # Adds an {Attribute} to the model
29
+ # @param name [Symbol]
30
+ # @param type [Symbol]
31
+ # @option options [Boolean] :required
32
+ # @option options [Boolean] :unique
33
+ # @option options [Object] :default
34
+ # @option options [Integer] :limit
35
+ # @option options [Integer] :precision
36
+ # @option options [Integer] :scale
37
+ #
38
+ # @example
39
+ # model :user do
40
+ # attribute :name, :string, required: true
41
+ # end
42
+ def attribute(name, type, **options)
43
+ @fields << Attribute.new(*name, type, **options)
44
+ end
45
+
46
+ # Adds an {Enum} to the model
47
+ # @param name [Symbol]
48
+ # @option options [Array<Symbol>] :values
49
+ # @option options [Boolean] :required
50
+ # @option options [Boolean] :unique
51
+ # @option options [Object] :default
52
+ #
53
+ # @example
54
+ # model :user do
55
+ # enum :status, values: [:accepted, :rejected], required: true
56
+ # end
57
+ def enum(name, **options)
58
+ @fields << Enum.new(name, **options)
59
+ end
60
+
61
+ # Adds an {Association::BelongsTo} to the model
62
+ # @param name [Symbol]
63
+ # @option options [Symbol] :model_name
64
+ # @option options [Boolean] :required
65
+ # @option options [Boolean] :unique
66
+ # @option options [Boolean] :foreign_key
67
+ #
68
+ # @example
69
+ # model :user do
70
+ # belongs_to :organization, required: true
71
+ # end
72
+ def belongs_to(name, **options)
73
+ @fields << Association::BelongsTo.new(name, **options)
74
+ end
75
+
76
+ # Adds an {Association::HasOne} to the model
77
+ # @param name [Symbol]
78
+ # @option options [Symbol] :model_name
79
+ #
80
+ # @example
81
+ # model :user do
82
+ # has_one :profile
83
+ # end
84
+ def has_one(name, **options)
85
+ @fields << Association::HasOne.new(name, **options)
86
+ end
87
+
88
+ # Adds an {Association::HasMany} to the model
89
+ # @param name [Symbol]
90
+ # @option options [Symbol] :model_name
91
+ #
92
+ # @example
93
+ # model :user do
94
+ # has_many :devices
95
+ # end
96
+ def has_many(name, **options)
97
+ @fields << Association::HasMany.new(name, **options)
98
+ end
99
+
100
+ # Adds an {Attachment::HasOne} to the model
101
+ # @param name [Symbol]
102
+ # @option options [Hash]
103
+ #
104
+ # @example
105
+ # model :user do
106
+ # has_one_attached :avatar
107
+ # end
108
+ def has_one_attached(name, **options)
109
+ @fields << Attachment::HasOne.new(name, **options)
110
+ end
111
+
112
+ # Adds an {Attachment::HasMany} to the model
113
+ # @param name [Symbol]
114
+ # @param options [Hash]
115
+ #
116
+ # @example
117
+ # model :product do
118
+ # has_many_attached :images
119
+ # end
120
+ def has_many_attached(name, **options)
121
+ @fields << Attachment::HasMany.new(name, **options)
122
+ end
123
+
124
+ # Adds attributes for +:created_at+ and +:updated_at+ to the model
125
+ #
126
+ # @example
127
+ # model :user do
128
+ # timestamps
129
+ # end
130
+ def timestamps
131
+ attribute :created_at, :datetime, required: true
132
+ attribute :updated_at, :datetime, required: true
133
+ end
134
+
135
+ # Get all {Attribute} fields
136
+ # @return [Array<Attribute>]
137
+ def attribute_fields
138
+ fields.grep Attribute
139
+ end
140
+
141
+ # Get all {Association} fields
142
+ # @return [Array<Association>]
143
+ def association_fields
144
+ fields.grep Association
145
+ end
146
+
147
+ # Get all {Attachment} fields
148
+ # @return [Array<Attachment>]
149
+ def attachment_fields
150
+ fields.grep Attachment
151
+ end
152
+
153
+ # @!method id_fields
154
+ # Get all {Attribute} fields whose type is `:id`
155
+ # @return [Array<Attribute>]
156
+ # @!method string_fields
157
+ # Get all {Attribute} fields whose type is `:string`
158
+ # @return [Array<Attribute>]
159
+ # @!method boolean_fields
160
+ # Get all {Attribute} fields whose type is `:boolean`
161
+ # @return [Array<Attribute>]
162
+ # @!method integer_fields
163
+ # Get all {Attribute} fields whose type is `:integer`
164
+ # @return [Array<Attribute>]
165
+ # @!method float_fields
166
+ # Get all {Attribute} fields whose type is `:float`
167
+ # @return [Array<Attribute>]
168
+ # @!method decimal_fields
169
+ # Get all {Attribute} fields whose type is `:decimal`
170
+ # @return [Array<Attribute>]
171
+ # @!method date_fields
172
+ # Get all {Attribute} fields whose type is `:date`
173
+ # @return [Array<Attribute>]
174
+ # @!method datetime_fields
175
+ # Get all {Attribute} fields whose type is `:datetime`
176
+ # @return [Array<Attribute>]
177
+ # @!method text_fields
178
+ # Get all {Attribute} fields whose type is `:text`
179
+ # @return [Array<Attribute>]
180
+ # @!method binary_fields
181
+ # Get all {Attribute} fields whose type is `:binary`
182
+ # @return [Array<Attribute>]
183
+ # @!method enum_fields
184
+ # Get all {Enum} fields
185
+ # @return [Array<Enum>]
186
+ # @!method belongs_to_fields
187
+ # Get all {Association::BelongsTo} fields
188
+ # @return [Array<Association::BelongsTo>]
189
+ # @!method has_one_fields
190
+ # Get all {Association::HasOne} fields
191
+ # @return [Array<Assocation::HasOne>]
192
+ # @!method has_many_fields
193
+ # Get all {Association::HasMany} fields
194
+ # @return [Array<Association::HasMany>]
195
+ # @!method has_one_attached_fields
196
+ # Get all {Attachment::HasOne} fields
197
+ # @return [Array<Attachment::HasOne>]
198
+ # @!method has_many_attached_fields
199
+ # Get all {Attachment::HasMany} fields
200
+ # @return [Array<Attachment::HasMany>]
201
+ TYPES.each do |meth|
202
+ define_method "#{meth}_fields" do
203
+ fields.select { |field| field.type == meth }
204
+ end
205
+ end
206
+ end
207
+ end
@@ -0,0 +1,131 @@
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/core_ext/string/inflections"
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
+ # * `required`
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[required 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($1.camelize)
107
+ when ASSOCIATION_WITH_NAME
108
+ opts[:type] = Association.const_get($1.camelize)
109
+ opts[:model_name] = $2.to_sym
110
+ when ATTACHMENT
111
+ opts[:type] = Attachment.const_get($1.camelize)
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 coerce(value, type)
122
+ case type
123
+ when :integer then value.to_i
124
+ when :float then value.to_f
125
+ when :boolean then value.match?(/^true$/)
126
+ else
127
+ value
128
+ end
129
+ end
130
+ end
131
+ 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.2.1"
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,140 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sdl
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.1
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/parser.rb
109
+ - lib/sdl/schema.rb
110
+ - lib/sdl/types.rb
111
+ - lib/sdl/version.rb
112
+ - sdl.gemspec
113
+ homepage: https://github.com/rzane/sdl
114
+ licenses:
115
+ - MIT
116
+ metadata:
117
+ homepage_uri: https://github.com/rzane/sdl
118
+ source_code_uri: https://github.com/rzane/sdl
119
+ documentation_uri: https://rubydoc.info/github/rzane/sdl
120
+ post_install_message:
121
+ rdoc_options: []
122
+ require_paths:
123
+ - lib
124
+ required_ruby_version: !ruby/object:Gem::Requirement
125
+ requirements:
126
+ - - ">="
127
+ - !ruby/object:Gem::Version
128
+ version: '0'
129
+ required_rubygems_version: !ruby/object:Gem::Requirement
130
+ requirements:
131
+ - - ">="
132
+ - !ruby/object:Gem::Version
133
+ version: '0'
134
+ requirements: []
135
+ rubyforge_project:
136
+ rubygems_version: 2.6.14
137
+ signing_key:
138
+ specification_version: 4
139
+ summary: A generic schema definition language.
140
+ test_files: []