data_composer 1.0.0
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 +7 -0
- data/.rspec +3 -0
- data/.rubocop.yml +12 -0
- data/CHANGELOG.md +5 -0
- data/CODE_OF_CONDUCT.md +132 -0
- data/LICENSE.txt +21 -0
- data/README.md +196 -0
- data/Rakefile +12 -0
- data/example.rb +1 -0
- data/lib/data_composer/migration_helpers.rb +30 -0
- data/lib/data_composer/model.rb +93 -0
- data/lib/data_composer/railtie.rb +19 -0
- data/lib/data_composer/version.rb +5 -0
- data/lib/data_composer.rb +23 -0
- data/sig/data_composer.rbs +4 -0
- metadata +87 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 6ffd1070bf3de72bca82731ba913066c317e7b80d8c3501e342f4fe0435a83f0
|
4
|
+
data.tar.gz: eebe8db8bd858291aaa385bdec02d2c91ecf91cea6624186185decfac4fb4415
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 31323e197afc7c4e58ab9e6128c9d905295430cef244bedf57f00b66debe41f5f4c386722cab34c488de578b7d6a1ab4e5219612cbfd1b9a3fcd4a94ccf85ef9
|
7
|
+
data.tar.gz: bda1447f583f9d16f8f274a5f818b5559273b4c387dd7190a0291b1bfc18ede15f59daffc5c69edce1a42b58f2a6236d0b0b693b2683b0040762dfe2dc573957
|
data/.rspec
ADDED
data/.rubocop.yml
ADDED
data/CHANGELOG.md
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,132 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
We as members, contributors, and leaders pledge to make participation in our
|
6
|
+
community a harassment-free experience for everyone, regardless of age, body
|
7
|
+
size, visible or invisible disability, ethnicity, sex characteristics, gender
|
8
|
+
identity and expression, level of experience, education, socio-economic status,
|
9
|
+
nationality, personal appearance, race, caste, color, religion, or sexual
|
10
|
+
identity and orientation.
|
11
|
+
|
12
|
+
We pledge to act and interact in ways that contribute to an open, welcoming,
|
13
|
+
diverse, inclusive, and healthy community.
|
14
|
+
|
15
|
+
## Our Standards
|
16
|
+
|
17
|
+
Examples of behavior that contributes to a positive environment for our
|
18
|
+
community include:
|
19
|
+
|
20
|
+
* Demonstrating empathy and kindness toward other people
|
21
|
+
* Being respectful of differing opinions, viewpoints, and experiences
|
22
|
+
* Giving and gracefully accepting constructive feedback
|
23
|
+
* Accepting responsibility and apologizing to those affected by our mistakes,
|
24
|
+
and learning from the experience
|
25
|
+
* Focusing on what is best not just for us as individuals, but for the overall
|
26
|
+
community
|
27
|
+
|
28
|
+
Examples of unacceptable behavior include:
|
29
|
+
|
30
|
+
* The use of sexualized language or imagery, and sexual attention or advances of
|
31
|
+
any kind
|
32
|
+
* Trolling, insulting or derogatory comments, and personal or political attacks
|
33
|
+
* Public or private harassment
|
34
|
+
* Publishing others' private information, such as a physical or email address,
|
35
|
+
without their explicit permission
|
36
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
37
|
+
professional setting
|
38
|
+
|
39
|
+
## Enforcement Responsibilities
|
40
|
+
|
41
|
+
Community leaders are responsible for clarifying and enforcing our standards of
|
42
|
+
acceptable behavior and will take appropriate and fair corrective action in
|
43
|
+
response to any behavior that they deem inappropriate, threatening, offensive,
|
44
|
+
or harmful.
|
45
|
+
|
46
|
+
Community leaders have the right and responsibility to remove, edit, or reject
|
47
|
+
comments, commits, code, wiki edits, issues, and other contributions that are
|
48
|
+
not aligned to this Code of Conduct, and will communicate reasons for moderation
|
49
|
+
decisions when appropriate.
|
50
|
+
|
51
|
+
## Scope
|
52
|
+
|
53
|
+
This Code of Conduct applies within all community spaces, and also applies when
|
54
|
+
an individual is officially representing the community in public spaces.
|
55
|
+
Examples of representing our community include using an official email address,
|
56
|
+
posting via an official social media account, or acting as an appointed
|
57
|
+
representative at an online or offline event.
|
58
|
+
|
59
|
+
## Enforcement
|
60
|
+
|
61
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
62
|
+
reported to the community leaders responsible for enforcement at
|
63
|
+
[INSERT CONTACT METHOD].
|
64
|
+
All complaints will be reviewed and investigated promptly and fairly.
|
65
|
+
|
66
|
+
All community leaders are obligated to respect the privacy and security of the
|
67
|
+
reporter of any incident.
|
68
|
+
|
69
|
+
## Enforcement Guidelines
|
70
|
+
|
71
|
+
Community leaders will follow these Community Impact Guidelines in determining
|
72
|
+
the consequences for any action they deem in violation of this Code of Conduct:
|
73
|
+
|
74
|
+
### 1. Correction
|
75
|
+
|
76
|
+
**Community Impact**: Use of inappropriate language or other behavior deemed
|
77
|
+
unprofessional or unwelcome in the community.
|
78
|
+
|
79
|
+
**Consequence**: A private, written warning from community leaders, providing
|
80
|
+
clarity around the nature of the violation and an explanation of why the
|
81
|
+
behavior was inappropriate. A public apology may be requested.
|
82
|
+
|
83
|
+
### 2. Warning
|
84
|
+
|
85
|
+
**Community Impact**: A violation through a single incident or series of
|
86
|
+
actions.
|
87
|
+
|
88
|
+
**Consequence**: A warning with consequences for continued behavior. No
|
89
|
+
interaction with the people involved, including unsolicited interaction with
|
90
|
+
those enforcing the Code of Conduct, for a specified period of time. This
|
91
|
+
includes avoiding interactions in community spaces as well as external channels
|
92
|
+
like social media. Violating these terms may lead to a temporary or permanent
|
93
|
+
ban.
|
94
|
+
|
95
|
+
### 3. Temporary Ban
|
96
|
+
|
97
|
+
**Community Impact**: A serious violation of community standards, including
|
98
|
+
sustained inappropriate behavior.
|
99
|
+
|
100
|
+
**Consequence**: A temporary ban from any sort of interaction or public
|
101
|
+
communication with the community for a specified period of time. No public or
|
102
|
+
private interaction with the people involved, including unsolicited interaction
|
103
|
+
with those enforcing the Code of Conduct, is allowed during this period.
|
104
|
+
Violating these terms may lead to a permanent ban.
|
105
|
+
|
106
|
+
### 4. Permanent Ban
|
107
|
+
|
108
|
+
**Community Impact**: Demonstrating a pattern of violation of community
|
109
|
+
standards, including sustained inappropriate behavior, harassment of an
|
110
|
+
individual, or aggression toward or disparagement of classes of individuals.
|
111
|
+
|
112
|
+
**Consequence**: A permanent ban from any sort of public interaction within the
|
113
|
+
community.
|
114
|
+
|
115
|
+
## Attribution
|
116
|
+
|
117
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage],
|
118
|
+
version 2.1, available at
|
119
|
+
[https://www.contributor-covenant.org/version/2/1/code_of_conduct.html][v2.1].
|
120
|
+
|
121
|
+
Community Impact Guidelines were inspired by
|
122
|
+
[Mozilla's code of conduct enforcement ladder][Mozilla CoC].
|
123
|
+
|
124
|
+
For answers to common questions about this code of conduct, see the FAQ at
|
125
|
+
[https://www.contributor-covenant.org/faq][FAQ]. Translations are available at
|
126
|
+
[https://www.contributor-covenant.org/translations][translations].
|
127
|
+
|
128
|
+
[homepage]: https://www.contributor-covenant.org
|
129
|
+
[v2.1]: https://www.contributor-covenant.org/version/2/1/code_of_conduct.html
|
130
|
+
[Mozilla CoC]: https://github.com/mozilla/diversity
|
131
|
+
[FAQ]: https://www.contributor-covenant.org/faq
|
132
|
+
[translations]: https://www.contributor-covenant.org/translations
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2025 Krzysztof Duda
|
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.
|
data/README.md
ADDED
@@ -0,0 +1,196 @@
|
|
1
|
+
# DataComposer
|
2
|
+
|
3
|
+
[](https://badge.fury.io/rb/data_composer)
|
4
|
+
|
5
|
+
**DataComposer** provides a set of helpers to elegantly compose native Ruby `Data` objects into your ActiveRecord models.
|
6
|
+
|
7
|
+
This gem lets you turn this:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
# db/migrate/xxxx.rb
|
11
|
+
add_column :products, :price_amount, :decimal
|
12
|
+
add_column :products, :price_currency, :string
|
13
|
+
|
14
|
+
# app/models/product.rb
|
15
|
+
Money = Data.define(:amount, :currency)
|
16
|
+
composed_of :price, class_name: "Money", mapping: [%w[price_amount amount], %w[price_currency currency]]
|
17
|
+
```
|
18
|
+
|
19
|
+
Into this:
|
20
|
+
|
21
|
+
```ruby
|
22
|
+
# db/migrate/xxxx.rb
|
23
|
+
t.data_object :price, members: { amount: :decimal, currency: :string }
|
24
|
+
|
25
|
+
# app/models/product.rb
|
26
|
+
Money = Data.define(:amount, :currency)
|
27
|
+
compose_data :price, Money
|
28
|
+
|
29
|
+
Product.first.price.amount #=> 100.0
|
30
|
+
```
|
31
|
+
|
32
|
+
## Core Concept
|
33
|
+
|
34
|
+
The philosophy is simple:
|
35
|
+
|
36
|
+
1. **Define Value Objects with `Data.define`**: Use pure, native Ruby `Data` objects to represent concepts in your domain.
|
37
|
+
2. **Declare Schema in Migrations**: Use the `t.data_object` helper to explicitly define the database columns required to store your value object.
|
38
|
+
3. **Compose in the Model**: Use the `compose_data` helper in your model to map the database columns to your `Data` object.# DataComposer
|
39
|
+
|
40
|
+
## Installation
|
41
|
+
|
42
|
+
Add this line to your application's Gemfile:
|
43
|
+
|
44
|
+
```ruby
|
45
|
+
gem 'data_composer'
|
46
|
+
```
|
47
|
+
|
48
|
+
And then execute:
|
49
|
+
|
50
|
+
```bash
|
51
|
+
$ bundle install
|
52
|
+
```
|
53
|
+
|
54
|
+
## Usage
|
55
|
+
|
56
|
+
Let's walk through an example of storing a `Height` value object, composed of an `amount` and a `unit`, on a `User` model.
|
57
|
+
|
58
|
+
### Step 1: Define Your Value Object
|
59
|
+
|
60
|
+
Create a file for your value object. It's just a plain Ruby `Data` object—no special wrappers are needed.
|
61
|
+
|
62
|
+
```ruby
|
63
|
+
# app/models/height.rb
|
64
|
+
Height = Data.define(:amount, :unit) do
|
65
|
+
# You can add any relevant domain logic here
|
66
|
+
def to_s
|
67
|
+
"#{amount} #{unit}"
|
68
|
+
end
|
69
|
+
|
70
|
+
def in_inches
|
71
|
+
unit == 'cm' ? (amount * 0.393701).round(2) : amount
|
72
|
+
end
|
73
|
+
end
|
74
|
+
```
|
75
|
+
|
76
|
+
### Step 2: Create a Migration
|
77
|
+
|
78
|
+
Generate a migration to add the columns to your table.
|
79
|
+
|
80
|
+
```bash
|
81
|
+
rails g migration AddHeightToUsers
|
82
|
+
```
|
83
|
+
|
84
|
+
Inside the migration file, use the `t.data_object` helper. You must explicitly define the member names and their corresponding database types.
|
85
|
+
|
86
|
+
```ruby
|
87
|
+
# db/migrate/xxxx_add_height_to_users.rb
|
88
|
+
class AddHeightToUsers < ActiveRecord::Migration[7.0]
|
89
|
+
def change
|
90
|
+
change_table :users do |t|
|
91
|
+
t.data_object :height,
|
92
|
+
members: { amount: :decimal, unit: :string },
|
93
|
+
null: false,
|
94
|
+
default: { amount: 0, unit: 'cm' }
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
98
|
+
```
|
99
|
+
|
100
|
+
When you run `rails db:migrate`, this will create two columns: `height_amount` (`DECIMAL`) and `height_unit` (`STRING`).
|
101
|
+
|
102
|
+
### Step 3: Compose it in Your Model
|
103
|
+
|
104
|
+
In your ActiveRecord model, use `compose_data` to tie everything together. This method automatically infers that `:height` will be mapped from the `height_amount` and `height_unit` columns.
|
105
|
+
|
106
|
+
```ruby
|
107
|
+
# app/models/user.rb
|
108
|
+
class User < ApplicationRecord
|
109
|
+
compose_data :height, Height
|
110
|
+
end
|
111
|
+
```
|
112
|
+
|
113
|
+
### Step 4: Use It!
|
114
|
+
|
115
|
+
Your `User` model now has a rich `height` attribute.
|
116
|
+
|
117
|
+
```ruby
|
118
|
+
# Create a user with a Height object
|
119
|
+
user = User.create!(height: Height.new(amount: 180, unit: 'cm'))
|
120
|
+
|
121
|
+
# Access the object and its members
|
122
|
+
puts user.height
|
123
|
+
#=> #<data Height amount=180, unit="cm">
|
124
|
+
|
125
|
+
puts user.height.amount
|
126
|
+
#=> 180
|
127
|
+
|
128
|
+
puts user.height.to_s
|
129
|
+
#=> "180 cm"
|
130
|
+
|
131
|
+
puts user.height.in_inches
|
132
|
+
#=> 70.87
|
133
|
+
|
134
|
+
# The object is transparently saved to the database columns
|
135
|
+
user.reload
|
136
|
+
puts user.read_attribute(:height_amount) #=> 180
|
137
|
+
puts user.read_attribute(:height_unit) #=> "cm"
|
138
|
+
|
139
|
+
# Assigning nil or invalid data also works as expected
|
140
|
+
user.update!(height: nil)
|
141
|
+
puts user.height #=> nil
|
142
|
+
```
|
143
|
+
|
144
|
+
## Advanced Features
|
145
|
+
|
146
|
+
### Default Values
|
147
|
+
|
148
|
+
You can specify default values for individual members:
|
149
|
+
|
150
|
+
```ruby
|
151
|
+
t.data_object :price,
|
152
|
+
members: { amount: :decimal, currency: :string },
|
153
|
+
default: { amount: 0, currency: "USD" }
|
154
|
+
```
|
155
|
+
|
156
|
+
### Multiple Data Objects
|
157
|
+
|
158
|
+
You can have multiple data objects on the same model:
|
159
|
+
|
160
|
+
```ruby
|
161
|
+
class Product < ApplicationRecord
|
162
|
+
compose_data :price, Money
|
163
|
+
compose_data :dimensions, Dimensions
|
164
|
+
compose_data :weight, Weight
|
165
|
+
end
|
166
|
+
```
|
167
|
+
|
168
|
+
### Custom Validation
|
169
|
+
|
170
|
+
Data objects support standard ActiveRecord validations:
|
171
|
+
|
172
|
+
```ruby
|
173
|
+
class User < ApplicationRecord
|
174
|
+
compose_data :height, Height
|
175
|
+
|
176
|
+
validates :height, presence: true
|
177
|
+
validate :height_must_be_reasonable
|
178
|
+
|
179
|
+
private
|
180
|
+
|
181
|
+
def height_must_be_reasonable
|
182
|
+
return unless height
|
183
|
+
|
184
|
+
errors.add(:height, "must be positive") if height.amount <= 0
|
185
|
+
errors.add(:height, "seems too tall") if height.amount > 300
|
186
|
+
end
|
187
|
+
end
|
188
|
+
```
|
189
|
+
|
190
|
+
## Contributing
|
191
|
+
|
192
|
+
Bug reports and pull requests are welcome on GitHub at [https://github.com/your-username/data_composer](https://github.com/your-username/data_composer).
|
193
|
+
|
194
|
+
## License
|
195
|
+
|
196
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/example.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
|
@@ -0,0 +1,30 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module DataComposer
|
4
|
+
# Provides helper methods for defining data objects in database migrations.
|
5
|
+
module MigrationHelpers
|
6
|
+
def data_object(attribute_name, members:, **options)
|
7
|
+
raise MissingMembersError, 'members hash cannot be empty' if members.empty?
|
8
|
+
|
9
|
+
default_value = options.delete(:default)
|
10
|
+
|
11
|
+
members.each do |member_name, member_type|
|
12
|
+
column_name = DataComposer.column_name_for_member(attribute_name, member_name)
|
13
|
+
column_options = options.dup
|
14
|
+
|
15
|
+
column_options[:default] = default_value[member_name] if default_value && default_value.is_a?(Hash)
|
16
|
+
|
17
|
+
if column_options.empty?
|
18
|
+
column(column_name, member_type)
|
19
|
+
else
|
20
|
+
column(column_name, member_type, **column_options)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
if defined?(ActiveRecord)
|
28
|
+
ActiveRecord::ConnectionAdapters::Table.include(DataComposer::MigrationHelpers)
|
29
|
+
ActiveRecord::ConnectionAdapters::TableDefinition.include(DataComposer::MigrationHelpers)
|
30
|
+
end
|
@@ -0,0 +1,93 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module DataComposer
|
4
|
+
# ActiveRecord model concern that provides the compose_data method
|
5
|
+
# for mapping Data objects to database columns.
|
6
|
+
module Model
|
7
|
+
def self.included(base)
|
8
|
+
base.extend(ClassMethods)
|
9
|
+
end
|
10
|
+
|
11
|
+
# Class methods added to ActiveRecord models when DataComposer::Model is included.
|
12
|
+
module ClassMethods
|
13
|
+
def compose_data(attribute_name, data_class, **options)
|
14
|
+
validate_data_class!(data_class)
|
15
|
+
|
16
|
+
members = extract_data_members(data_class)
|
17
|
+
column_mapping = build_column_mapping(attribute_name, members)
|
18
|
+
|
19
|
+
define_data_accessors(attribute_name, data_class, column_mapping)
|
20
|
+
define_validation_if_needed(attribute_name, data_class, options)
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
def validate_data_class!(data_class)
|
26
|
+
return if data_class.respond_to?(:new) && data_class.respond_to?(:members)
|
27
|
+
|
28
|
+
raise InvalidDataObjectError, "#{data_class} must be a Data class created with Data.define"
|
29
|
+
end
|
30
|
+
|
31
|
+
def extract_data_members(data_class)
|
32
|
+
if data_class.respond_to?(:members)
|
33
|
+
data_class.members
|
34
|
+
else
|
35
|
+
sample_instance = data_class.new(*([nil] * data_class.method(:new).arity))
|
36
|
+
sample_instance.members
|
37
|
+
end
|
38
|
+
rescue StandardError
|
39
|
+
raise InvalidDataObjectError, "Cannot determine members for #{data_class}"
|
40
|
+
end
|
41
|
+
|
42
|
+
def build_column_mapping(attribute_name, members)
|
43
|
+
members.map do |member|
|
44
|
+
[member, DataComposer.column_name_for_member(attribute_name, member)]
|
45
|
+
end.to_h
|
46
|
+
end
|
47
|
+
|
48
|
+
def define_data_accessors(attribute_name, data_class, column_mapping)
|
49
|
+
define_data_getter(attribute_name, data_class, column_mapping)
|
50
|
+
define_data_setter(attribute_name, data_class, column_mapping)
|
51
|
+
end
|
52
|
+
|
53
|
+
def define_data_getter(attribute_name, data_class, column_mapping)
|
54
|
+
define_method(attribute_name) do
|
55
|
+
return nil if column_mapping.values.all? { |col| read_attribute(col).nil? }
|
56
|
+
|
57
|
+
member_values = column_mapping.transform_values { |col| read_attribute(col) }
|
58
|
+
data_class.new(**member_values)
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
def define_data_setter(attribute_name, data_class, column_mapping)
|
63
|
+
define_method("#{attribute_name}=") do |value|
|
64
|
+
if value.nil?
|
65
|
+
column_mapping.each_value { |col| write_attribute(col, nil) }
|
66
|
+
elsif value.is_a?(data_class)
|
67
|
+
column_mapping.each do |member, column|
|
68
|
+
write_attribute(column, value.public_send(member))
|
69
|
+
end
|
70
|
+
elsif value.is_a?(Hash)
|
71
|
+
symbolized_hash = value.respond_to?(:symbolize_keys) ? value.symbolize_keys : value.transform_keys(&:to_sym)
|
72
|
+
data_object = data_class.new(**symbolized_hash)
|
73
|
+
column_mapping.each do |member, column|
|
74
|
+
write_attribute(column, data_object.public_send(member))
|
75
|
+
end
|
76
|
+
else
|
77
|
+
raise ArgumentError, "Expected #{data_class}, Hash, or nil, got #{value.class}"
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
def define_validation_if_needed(attribute_name, _data_class, options)
|
83
|
+
return unless options[:validate] || options[:presence]
|
84
|
+
|
85
|
+
validates attribute_name, presence: true if options[:presence]
|
86
|
+
|
87
|
+
return unless options[:validate].is_a?(Proc)
|
88
|
+
|
89
|
+
validate -> { options[:validate].call(public_send(attribute_name)) }
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module DataComposer
|
4
|
+
# Rails integration for automatic inclusion of DataComposer functionality.
|
5
|
+
class Railtie < Rails::Railtie
|
6
|
+
initializer 'data_composer.include_model_concern' do
|
7
|
+
ActiveSupport.on_load(:active_record) do
|
8
|
+
include DataComposer::Model
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
initializer 'data_composer.include_migration_helpers' do
|
13
|
+
ActiveSupport.on_load(:active_record) do
|
14
|
+
ActiveRecord::ConnectionAdapters::Table.include(DataComposer::MigrationHelpers)
|
15
|
+
ActiveRecord::ConnectionAdapters::TableDefinition.include(DataComposer::MigrationHelpers)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'data_composer/version'
|
4
|
+
require_relative 'data_composer/model'
|
5
|
+
require_relative 'data_composer/migration_helpers'
|
6
|
+
|
7
|
+
# DataComposer provides helpers to elegantly compose native Ruby Data objects
|
8
|
+
# into ActiveRecord models, automating migrations and model-level definitions.
|
9
|
+
module DataComposer
|
10
|
+
class Error < StandardError; end
|
11
|
+
class InvalidDataObjectError < Error; end
|
12
|
+
class MissingMembersError < Error; end
|
13
|
+
|
14
|
+
def self.table_name_for_data_object(attribute_name)
|
15
|
+
"#{attribute_name}_"
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.column_name_for_member(attribute_name, member_name)
|
19
|
+
"#{attribute_name}_#{member_name}"
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
require_relative 'data_composer/railtie' if defined?(Rails)
|
metadata
ADDED
@@ -0,0 +1,87 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: data_composer
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Krzysztof Duda
|
8
|
+
bindir: exe
|
9
|
+
cert_chain: []
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
11
|
+
dependencies:
|
12
|
+
- !ruby/object:Gem::Dependency
|
13
|
+
name: activerecord
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
15
|
+
requirements:
|
16
|
+
- - ">="
|
17
|
+
- !ruby/object:Gem::Version
|
18
|
+
version: '7.0'
|
19
|
+
type: :runtime
|
20
|
+
prerelease: false
|
21
|
+
version_requirements: !ruby/object:Gem::Requirement
|
22
|
+
requirements:
|
23
|
+
- - ">="
|
24
|
+
- !ruby/object:Gem::Version
|
25
|
+
version: '7.0'
|
26
|
+
- !ruby/object:Gem::Dependency
|
27
|
+
name: activesupport
|
28
|
+
requirement: !ruby/object:Gem::Requirement
|
29
|
+
requirements:
|
30
|
+
- - ">="
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '7.0'
|
33
|
+
type: :runtime
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - ">="
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '7.0'
|
40
|
+
description: DataComposer provides helpers to elegantly compose native Ruby Data objects
|
41
|
+
into ActiveRecord models, automating migrations and model-level definitions.
|
42
|
+
email:
|
43
|
+
- duda_krzysztof@outlook.com
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- ".rspec"
|
49
|
+
- ".rubocop.yml"
|
50
|
+
- CHANGELOG.md
|
51
|
+
- CODE_OF_CONDUCT.md
|
52
|
+
- LICENSE.txt
|
53
|
+
- README.md
|
54
|
+
- Rakefile
|
55
|
+
- example.rb
|
56
|
+
- lib/data_composer.rb
|
57
|
+
- lib/data_composer/migration_helpers.rb
|
58
|
+
- lib/data_composer/model.rb
|
59
|
+
- lib/data_composer/railtie.rb
|
60
|
+
- lib/data_composer/version.rb
|
61
|
+
- sig/data_composer.rbs
|
62
|
+
homepage: https://github.com/your-username/data_composer
|
63
|
+
licenses:
|
64
|
+
- MIT
|
65
|
+
metadata:
|
66
|
+
homepage_uri: https://github.com/your-username/data_composer
|
67
|
+
source_code_uri: https://github.com/your-username/data_composer
|
68
|
+
changelog_uri: https://github.com/your-username/data_composer/blob/main/CHANGELOG.md
|
69
|
+
rdoc_options: []
|
70
|
+
require_paths:
|
71
|
+
- lib
|
72
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
73
|
+
requirements:
|
74
|
+
- - ">="
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: 3.1.0
|
77
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
78
|
+
requirements:
|
79
|
+
- - ">="
|
80
|
+
- !ruby/object:Gem::Version
|
81
|
+
version: '0'
|
82
|
+
requirements: []
|
83
|
+
rubygems_version: 3.6.7
|
84
|
+
specification_version: 4
|
85
|
+
summary: Compose native Ruby Data objects into ActiveRecord models with automatic
|
86
|
+
migrations
|
87
|
+
test_files: []
|