nueca_rails_interfaces 0.1.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 +14 -0
- data/CODE_OF_CONDUCT.md +84 -0
- data/LICENSE.txt +21 -0
- data/README.md +31 -0
- data/Rakefile +12 -0
- data/lib/nueca_rails_interfaces/util.rb +14 -0
- data/lib/nueca_rails_interfaces/version.rb +5 -0
- data/lib/nueca_rails_interfaces.rb +16 -0
- data/lib/v1/form_interface.rb +29 -0
- data/lib/v1/query_interface.rb +140 -0
- data/lib/v1/service_interface.rb +91 -0
- data/lib/v2/form_interface.rb +35 -0
- data/lib/v2/service_interface.rb +98 -0
- data/nueca_rails_interfaces.gemspec +34 -0
- metadata +87 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 71471580cc28c42f5997160d6e01684e081244da014e44ecd110e37e4c62db91
|
4
|
+
data.tar.gz: 3236fcf542d757322a14ab338330ad721089445afc4f7b1877bd45cf6dbeda65
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: '089db05b4843a21ba0462efe9e07717a6b17f8e35117fcb096279d0f136f1f6828ed7c7d85f423e38781f325b58e678bb4a8f0808010daee14f530716d5ede3c'
|
7
|
+
data.tar.gz: 6061f449b9deb9130c7aeaba1d5de4a7a02542bc560333dc1995a088771a88f9f2a23e67eeb0e947b25a340e804f74b5eb8f1aef157d8900dccec7cd685136a7
|
data/.rspec
ADDED
data/.rubocop.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
|
6
|
+
|
7
|
+
We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
|
8
|
+
|
9
|
+
## Our Standards
|
10
|
+
|
11
|
+
Examples of behavior that contributes to a positive environment for our community include:
|
12
|
+
|
13
|
+
* Demonstrating empathy and kindness toward other people
|
14
|
+
* Being respectful of differing opinions, viewpoints, and experiences
|
15
|
+
* Giving and gracefully accepting constructive feedback
|
16
|
+
* Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
|
17
|
+
* Focusing on what is best not just for us as individuals, but for the overall community
|
18
|
+
|
19
|
+
Examples of unacceptable behavior include:
|
20
|
+
|
21
|
+
* The use of sexualized language or imagery, and sexual attention or
|
22
|
+
advances of any kind
|
23
|
+
* Trolling, insulting or derogatory comments, and personal or political attacks
|
24
|
+
* Public or private harassment
|
25
|
+
* Publishing others' private information, such as a physical or email
|
26
|
+
address, without their explicit permission
|
27
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
28
|
+
professional setting
|
29
|
+
|
30
|
+
## Enforcement Responsibilities
|
31
|
+
|
32
|
+
Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
|
33
|
+
|
34
|
+
Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
|
35
|
+
|
36
|
+
## Scope
|
37
|
+
|
38
|
+
This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
|
39
|
+
|
40
|
+
## Enforcement
|
41
|
+
|
42
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at jenek@nuecca.com.ph. All complaints will be reviewed and investigated promptly and fairly.
|
43
|
+
|
44
|
+
All community leaders are obligated to respect the privacy and security of the reporter of any incident.
|
45
|
+
|
46
|
+
## Enforcement Guidelines
|
47
|
+
|
48
|
+
Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
|
49
|
+
|
50
|
+
### 1. Correction
|
51
|
+
|
52
|
+
**Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
|
53
|
+
|
54
|
+
**Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
|
55
|
+
|
56
|
+
### 2. Warning
|
57
|
+
|
58
|
+
**Community Impact**: A violation through a single incident or series of actions.
|
59
|
+
|
60
|
+
**Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
|
61
|
+
|
62
|
+
### 3. Temporary Ban
|
63
|
+
|
64
|
+
**Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
|
65
|
+
|
66
|
+
**Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
|
67
|
+
|
68
|
+
### 4. Permanent Ban
|
69
|
+
|
70
|
+
**Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
|
71
|
+
|
72
|
+
**Consequence**: A permanent ban from any sort of public interaction within the community.
|
73
|
+
|
74
|
+
## Attribution
|
75
|
+
|
76
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
|
77
|
+
available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
|
78
|
+
|
79
|
+
Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
|
80
|
+
|
81
|
+
[homepage]: https://www.contributor-covenant.org
|
82
|
+
|
83
|
+
For answers to common questions about this code of conduct, see the FAQ at
|
84
|
+
https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2024 TODO: Write your name
|
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,31 @@
|
|
1
|
+
# Nueca Rails Interfaces
|
2
|
+
|
3
|
+
This gem contains the culture and conventions that employees of Nueca should follow when developing Ruby on Rails applications. Inspect each interface for more details as they are documented.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add in your Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'nueca_rails_interfaces'
|
11
|
+
```
|
12
|
+
|
13
|
+
## Usage
|
14
|
+
|
15
|
+
Simply include the interfaces in classes and they will be enforced.
|
16
|
+
|
17
|
+
## Development
|
18
|
+
|
19
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
20
|
+
|
21
|
+
## Contributing
|
22
|
+
|
23
|
+
Bug reports and pull requests are welcome on Nueca GitLab at https://github.com/tieeeeen1994/nueca-rails-interfaces. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/tieeeeen1994/nueca-rails-interfaces/-/blob/main/CODE_OF_CONDUCT.md).
|
24
|
+
|
25
|
+
## License
|
26
|
+
|
27
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
28
|
+
|
29
|
+
## Code of Conduct
|
30
|
+
|
31
|
+
Everyone interacting in the Nueca Rails Interfaces project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/tieeeeen1994/nueca-rails-interfaces/-/blob/main/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module NuecaRailsInterfaces
|
4
|
+
# Utility module helper.
|
5
|
+
module Util
|
6
|
+
def self.process_class_arguments(class_object, *arguments)
|
7
|
+
if arguments.size == 1 && arguments.first.is_a?(Hash)
|
8
|
+
class_object.new(**arguments.first)
|
9
|
+
else
|
10
|
+
class_object.new(*arguments)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
raise 'Rails not found' unless defined?(Rails)
|
4
|
+
|
5
|
+
require 'to_bool'
|
6
|
+
|
7
|
+
require_relative 'nueca_rails_interfaces/version'
|
8
|
+
|
9
|
+
# This module is a namespace for the gem.
|
10
|
+
module NuecaRailsInterfaces
|
11
|
+
end
|
12
|
+
|
13
|
+
require_relative 'nueca_rails_interfaces/util'
|
14
|
+
|
15
|
+
# Require all interfaces.
|
16
|
+
Dir["#{__dir__}/v*/*_interface.rb"].each { |file| require_relative file }
|
@@ -0,0 +1,29 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module V1
|
4
|
+
# Form Interface. Include this module to create a new form.
|
5
|
+
# In this version, a form is defined as a data verifier, mainly coming from parameters
|
6
|
+
# to shoulder off validation from processors such as services and interactors, or action controllers.
|
7
|
+
# A form should be treated like a custom model without the need for a database.
|
8
|
+
# Forms are responsible for validating data and returning the data in a format that is ready for processing.
|
9
|
+
# Forms are responsible for handling bad data, returning errors provided by ActiveModel.
|
10
|
+
# Forms should implement the `attributes` method to define the attributes of the form.
|
11
|
+
# Forms should not override methods from ActiveModel for customization.
|
12
|
+
# The `attributes` method should return a hash of the attributes of the form (strictly not an array).
|
13
|
+
# It is up to the developer what `attributes` method will contain if there is an error. Treat as such like an API.
|
14
|
+
module FormInterface
|
15
|
+
# Allows the form mixin to include ActiveModel::Model powers.
|
16
|
+
# @param [self] base Instance of the base form that would include this module.
|
17
|
+
# @return [void]
|
18
|
+
def self.included(base)
|
19
|
+
base.include(ActiveModel::Model)
|
20
|
+
end
|
21
|
+
|
22
|
+
# Final attributes to be returned by the form after validation.
|
23
|
+
# This is the data that is expected of the form to produce for processing.
|
24
|
+
# @raise [NotImplementedError] If the method is not overridden.
|
25
|
+
def attributes
|
26
|
+
raise NotImplementedError, 'Requires implementation of attributes.'
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,140 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module V1
|
4
|
+
# Query Mixin Interface. Include this module to a class that will be used as a query object.
|
5
|
+
# Query objects are eaily identified as helpers for querying with filters and sorting with complex algorithms.
|
6
|
+
# Thanks to ActiveRecord's inner workings, Ruby alone can handle the avdanced filtering
|
7
|
+
# before firing the query in the database. In this version, all query objects will be paginated.
|
8
|
+
# This is to avoid really heavy queries from hitting the database, either be it intentnional or malicious.
|
9
|
+
# In this version, pagination is enforced but only lightly encouraged.
|
10
|
+
# There will be a deprecation warning when no_pagination is used.
|
11
|
+
# However, the implementation of no_pagination is still a 1 page result;
|
12
|
+
# just that it supports a large number of queries in a single page.
|
13
|
+
# Developers will be required to move away from V1 of Query Interface soon to enforce strict pagination.
|
14
|
+
module QueryInterface
|
15
|
+
# The basis for validity of pagination settings. It also contains default values.
|
16
|
+
VALID_PAGINATION_HASH = {
|
17
|
+
max: 20, # Absolute maximum number of records per page, even if the query requests for more.
|
18
|
+
min: 1, # Absolute minimum number of records per page, even if the query requests for less.
|
19
|
+
per_page: 20, # Default number of records per page if not specified in the query.
|
20
|
+
page: 1 # Default page number if not specified in the query.
|
21
|
+
}.freeze
|
22
|
+
|
23
|
+
# Basis for considering a non-paging result even when the query is being processed for pagination.
|
24
|
+
# This number states the invalidity of pagination, but it exists for legacy support.
|
25
|
+
NO_PAGING_THRESHOLD = 1_000_000
|
26
|
+
|
27
|
+
class << self
|
28
|
+
def included(base)
|
29
|
+
# This is the method to call outside this object to apply the query filters, sortings and paginations.
|
30
|
+
# @param [Hash] query The query parameters.
|
31
|
+
# @param [ActiveRecord::Relation] collection The collection to be queried.
|
32
|
+
base.define_singleton_method(:call) do |query, collection|
|
33
|
+
new(query, collection).call
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
attr_reader :query, :collection
|
39
|
+
|
40
|
+
# Do not override! This is how we will always initialize our query objects.
|
41
|
+
# No processing should be done in the initialize method.
|
42
|
+
# @param [Hash] query The query parameters.
|
43
|
+
# @param [ActiveRecord::Relation] collection The collection to be queried.
|
44
|
+
def initialize(query, collection, pagination: true)
|
45
|
+
@query = query
|
46
|
+
@collection = collection
|
47
|
+
@pagination_flag = pagination
|
48
|
+
query_aliases
|
49
|
+
end
|
50
|
+
|
51
|
+
# Do not override. This is the method to call outside this object
|
52
|
+
# to apply the query filters, sortings and paginations.
|
53
|
+
def call
|
54
|
+
apply_filters!
|
55
|
+
apply_sorting!
|
56
|
+
apply_pagination!
|
57
|
+
collection
|
58
|
+
end
|
59
|
+
|
60
|
+
private
|
61
|
+
|
62
|
+
# Place here filters. Be sure to assign @collection to override the original collection. Be sure it is private!
|
63
|
+
def filters; end
|
64
|
+
|
65
|
+
# Place here sorting logic. Be sure to assign @collection to override the original collection.
|
66
|
+
# Be sure it is private!
|
67
|
+
def sorts; end
|
68
|
+
|
69
|
+
# Pagination settings to modify the default behavior of a query object.
|
70
|
+
# Default values are in VALID_PAGINATION_HASH constant.
|
71
|
+
# Override the method to change the default values.
|
72
|
+
def pagination_settings
|
73
|
+
{}
|
74
|
+
end
|
75
|
+
|
76
|
+
# Always updated alias of filters.
|
77
|
+
def apply_filters!
|
78
|
+
filters
|
79
|
+
end
|
80
|
+
|
81
|
+
# Always updated alias of sorts.
|
82
|
+
def apply_sorting!
|
83
|
+
sorts
|
84
|
+
end
|
85
|
+
|
86
|
+
# Paginates the collection based on query or settings.
|
87
|
+
def apply_pagination!
|
88
|
+
raise 'Invalid pagination settings.' unless correct_pagination_settings?
|
89
|
+
return unless @pagination_flag
|
90
|
+
|
91
|
+
@collection = collection.paginate(page: fetch_page_value, per_page: fetch_per_page_value)
|
92
|
+
end
|
93
|
+
|
94
|
+
# Logic for fetching the page value from the query or settings.
|
95
|
+
def fetch_page_value
|
96
|
+
query&.key?(:page) ? query[:page].to_i : merged_pagination_settings[:page]
|
97
|
+
end
|
98
|
+
|
99
|
+
# Logic for fetching the per page value from the query or settings.
|
100
|
+
def fetch_per_page_value
|
101
|
+
per_page = query&.key?(:per_page) ? query[:per_page].to_i : merged_pagination_settings[:per_page]
|
102
|
+
per_page.clamp(merged_pagination_settings[:min], merged_pagination_settings[:max])
|
103
|
+
end
|
104
|
+
|
105
|
+
# Checks if the pagination settings are correct.
|
106
|
+
# The app crashes on misconfiguration.
|
107
|
+
def correct_pagination_settings?
|
108
|
+
return false unless pagination_settings.is_a?(Hash)
|
109
|
+
|
110
|
+
detected_keys = []
|
111
|
+
merged_pagination_settings.each_key do |key|
|
112
|
+
return false unless VALID_PAGINATION_HASH.key?(key)
|
113
|
+
|
114
|
+
detected_keys << key
|
115
|
+
end
|
116
|
+
|
117
|
+
detected_keys.sort == VALID_PAGINATION_HASH.keys.sort
|
118
|
+
end
|
119
|
+
|
120
|
+
# The final result of pagination settings, and thus the used one.
|
121
|
+
def merged_pagination_settings
|
122
|
+
@merged_pagination_settings ||= VALID_PAGINATION_HASH.merge(pagination_settings)
|
123
|
+
end
|
124
|
+
|
125
|
+
# Aliases for query parameters for legacy support.
|
126
|
+
# No need to override this in children. Directly modify this method in this interface if need be.
|
127
|
+
def query_aliases
|
128
|
+
query[:per_page] = query[:limit] if query[:limit].present? && query[:per_page].blank?
|
129
|
+
end
|
130
|
+
|
131
|
+
# For deprecation. Use this for queries that do not need pagination.
|
132
|
+
# Queries will still be paginated as a result, but with the use of the threshold,
|
133
|
+
# the result is as good as a non-paginated result,
|
134
|
+
# and it will be treated as such.
|
135
|
+
def no_pagination
|
136
|
+
Rails.logger.warn 'Querying without paging is deprecated. Enforce paging in queries!'
|
137
|
+
{ max: NO_PAGING_THRESHOLD, min: NO_PAGING_THRESHOLD }
|
138
|
+
end
|
139
|
+
end
|
140
|
+
end
|
@@ -0,0 +1,91 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module V1
|
4
|
+
# Service Mixin Interface. Include from this module when creating a new service.
|
5
|
+
# In this version, a service is defined as an absolute reusable piece of code.
|
6
|
+
# They should not be related to views and presentation of data; they are instead processors.
|
7
|
+
# Because of this, errors encountered should be raised as exceptions naturally.
|
8
|
+
# Errors here are treated as bad data. Services are not responsible for handling bad data.
|
9
|
+
# Services assume all data passed is correct and valid. Services should no longer validate these data.
|
10
|
+
# Services can still store warnings; these warnings are sent to Sentry, although not yet implemented.
|
11
|
+
# All services will have a `perform` method that will call the `action` method. This is the invoker of the service.
|
12
|
+
# All services will have an `action` method that will contain the main logic of the service.
|
13
|
+
# All services will have a `data` method that will contain the resulting data that the service produced.
|
14
|
+
# Developers will mainly override `action` method and `data method`.
|
15
|
+
module ServiceInterface
|
16
|
+
# This is the main method of the service. This is the method that should be called to perform the service.
|
17
|
+
# Do not override this method. Instead, override the `action` method.
|
18
|
+
# @return [self] Instance of the service.
|
19
|
+
def perform
|
20
|
+
unless performed?
|
21
|
+
action
|
22
|
+
@performed = true
|
23
|
+
process_warnings
|
24
|
+
end
|
25
|
+
|
26
|
+
self
|
27
|
+
end
|
28
|
+
|
29
|
+
# Override this method and put the main logic of the service here.
|
30
|
+
# @raise [NotImplementedError] If the method is not overridden.
|
31
|
+
def action
|
32
|
+
raise NotImplementedError, 'Requires implementation of action.'
|
33
|
+
end
|
34
|
+
|
35
|
+
# Override this method and put the resulting data of the service here.
|
36
|
+
# If blank, then return an empty hash manually.
|
37
|
+
# Reason being is for readability's sake in the services.
|
38
|
+
# @raise [NotImplementedError] If the method is not overridden.
|
39
|
+
def data
|
40
|
+
raise NotImplementedError, 'Requires implementation of data.'
|
41
|
+
end
|
42
|
+
|
43
|
+
# Method used to add a warning. Do not override.
|
44
|
+
# @param [String] warning_message Descriptive sentence of the warning.
|
45
|
+
# @return [Array<String>] Array of all warnings.
|
46
|
+
def add_warning(warning_message)
|
47
|
+
_warnings << warning_message
|
48
|
+
warnings
|
49
|
+
end
|
50
|
+
|
51
|
+
# Checks if the service has been performed. Do not override.
|
52
|
+
# @return [Boolean] True or False
|
53
|
+
def performed?
|
54
|
+
performed
|
55
|
+
end
|
56
|
+
|
57
|
+
# Used to check if the service has encountered any warnings. Do not override.
|
58
|
+
# @return [Boolean] True or False
|
59
|
+
def warnings?
|
60
|
+
_warnings.any?
|
61
|
+
end
|
62
|
+
|
63
|
+
# This should contain all the warnings that the service has encountered.
|
64
|
+
# Intentionally made to be frozen to avoid free modification.
|
65
|
+
# Do not override this method.
|
66
|
+
# @return [Array<String>] Array of all warnings.
|
67
|
+
def warnings
|
68
|
+
_warnings.dup.freeze
|
69
|
+
end
|
70
|
+
|
71
|
+
private
|
72
|
+
|
73
|
+
# Status of the service. If the service has been performed, this should be true.
|
74
|
+
# Do not override this method.
|
75
|
+
# @return [Boolean] True or False
|
76
|
+
def performed
|
77
|
+
@performed ||= false
|
78
|
+
end
|
79
|
+
|
80
|
+
# The real warnings array. Do not override this method. Use `add_warning` method to add warnings.
|
81
|
+
# @return [Array<String>] Array of all warnings.
|
82
|
+
def _warnings
|
83
|
+
@warnings ||= []
|
84
|
+
end
|
85
|
+
|
86
|
+
# Iterates through the warnings and sends them to Sentry, supposedly. Unimplemented so far.
|
87
|
+
# Do not override the method.
|
88
|
+
# @return [void]
|
89
|
+
def process_warnings; end
|
90
|
+
end
|
91
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module V2
|
4
|
+
# V2 Form Interface is the same as V1 Form Interface,
|
5
|
+
# except forces an exception on the attributes method when intializing a form.
|
6
|
+
module FormInterface
|
7
|
+
class << self
|
8
|
+
# Allows the form mixin to include ActiveModel::Model powers.
|
9
|
+
def included(base)
|
10
|
+
base.include(ActiveModel::Model)
|
11
|
+
|
12
|
+
# Initializes the form in a class context with the options passed in.
|
13
|
+
base.define_singleton_method(:check) do |*arguments|
|
14
|
+
instance = NuecaRailsInterfaces::Util.process_class_arguments(self, *arguments)
|
15
|
+
instance.valid?
|
16
|
+
instance
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
# Initializes the form with the options passed in.
|
22
|
+
# It also calls the attributes method to ensure it is implemented.
|
23
|
+
def initialize(options = {})
|
24
|
+
super(**options)
|
25
|
+
attributes
|
26
|
+
end
|
27
|
+
|
28
|
+
# Final attributes to be returned by the form after validation.
|
29
|
+
# This is the data that is expected of the form to produce for processing.
|
30
|
+
# @raise [NotImplementedError] If the method is not overridden.
|
31
|
+
def attributes
|
32
|
+
raise NotImplementedError, 'Requires implementation of attributes.'
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,98 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module V2
|
4
|
+
# V2 Service Interface is the same as V1 Service Interface, except it is more straightforward.
|
5
|
+
# When performing the service, it will immediately return the data
|
6
|
+
module ServiceInterface
|
7
|
+
class << self
|
8
|
+
def included(base)
|
9
|
+
# This is the main method of the service in a class context.
|
10
|
+
# This is the method that should be called to perform the service statically.
|
11
|
+
# Use this instead if the service instance is not needed.
|
12
|
+
# Do not override this method. Instead, override the `action` method. Returns the data immediately.
|
13
|
+
# @return [Object] Data of the service.
|
14
|
+
base.define_singleton_method(:perform) do |*arguments|
|
15
|
+
instance = NuecaRailsInterfaces::Util.process_class_arguments(self, *arguments)
|
16
|
+
instance.perform
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
# This is the main method of the service. This is the method that should be called to perform the service.
|
22
|
+
# Do not override this method. Instead, override the `action` method. Returns the data immediately.
|
23
|
+
# @return [Object] Data of the service.
|
24
|
+
def perform
|
25
|
+
unless performed?
|
26
|
+
action
|
27
|
+
@performed = true
|
28
|
+
process_warnings
|
29
|
+
end
|
30
|
+
|
31
|
+
data
|
32
|
+
end
|
33
|
+
|
34
|
+
# Override this method and put the main logic of the service here.
|
35
|
+
# @raise [NotImplementedError] If the method is not overridden.
|
36
|
+
# @return [void]
|
37
|
+
def action
|
38
|
+
raise NotImplementedError, 'Requires implementation of action.'
|
39
|
+
end
|
40
|
+
|
41
|
+
# Override this method and put the resulting data of the service here.
|
42
|
+
# If blank, then return an empty hash manually.
|
43
|
+
# Reason being is for readability's sake in the services.
|
44
|
+
# @raise [NotImplementedError] If the method is not overridden.
|
45
|
+
# @return [Object] Data of the service.
|
46
|
+
def data
|
47
|
+
raise NotImplementedError, 'Requires implementation of data.'
|
48
|
+
end
|
49
|
+
|
50
|
+
# Method used to add a warning. Do not override.
|
51
|
+
# @param [String] warning_message Descriptive sentence of the warning.
|
52
|
+
# @return [Array<String>] Array of all warnings.
|
53
|
+
def add_warning(warning_message)
|
54
|
+
_warnings << warning_message
|
55
|
+
warnings
|
56
|
+
end
|
57
|
+
|
58
|
+
# Checks if the service has been performed. Do not override.
|
59
|
+
# @return [Boolean] True or False
|
60
|
+
def performed?
|
61
|
+
performed
|
62
|
+
end
|
63
|
+
|
64
|
+
# Used to check if the service has encountered any warnings. Do not override.
|
65
|
+
# @return [Boolean] True or False
|
66
|
+
def warnings?
|
67
|
+
_warnings.any?
|
68
|
+
end
|
69
|
+
|
70
|
+
# This should contain all the warnings that the service has encountered.
|
71
|
+
# Intentionally made to be frozen to avoid free modification.
|
72
|
+
# Do not override this method.
|
73
|
+
# @return [Array<String>] Array of all warnings.
|
74
|
+
def warnings
|
75
|
+
_warnings.dup.freeze
|
76
|
+
end
|
77
|
+
|
78
|
+
private
|
79
|
+
|
80
|
+
# Status of the service. If the service has been performed, this should be true.
|
81
|
+
# Do not override this method.
|
82
|
+
# @return [Boolean] True or False
|
83
|
+
def performed
|
84
|
+
@performed ||= false
|
85
|
+
end
|
86
|
+
|
87
|
+
# The real warnings array. Do not override this method. Use `add_warning` method to add warnings.
|
88
|
+
# @return [Array<String>] Array of all warnings.
|
89
|
+
def _warnings
|
90
|
+
@warnings ||= []
|
91
|
+
end
|
92
|
+
|
93
|
+
# Iterates through the warnings and sends them to Sentry, supposedly. Unimplemented so far.
|
94
|
+
# Do not override the method.
|
95
|
+
# @return [void]
|
96
|
+
def process_warnings; end
|
97
|
+
end
|
98
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'lib/nueca_rails_interfaces/version'
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = 'nueca_rails_interfaces'
|
7
|
+
spec.version = NuecaRailsInterfaces::VERSION
|
8
|
+
spec.authors = ['Tien']
|
9
|
+
spec.email = ['tieeeeen1994@gmail.com']
|
10
|
+
spec.summary = 'Interfaces for known object entities in Rails Development at Nueca.'
|
11
|
+
spec.homepage = 'https://github.com/tieeeeen1994/nueca-rails-interfaces'
|
12
|
+
spec.required_ruby_version = '>= 3.3.0'
|
13
|
+
|
14
|
+
# Specify which files should be added to the gem when it is released.
|
15
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
16
|
+
spec.files = Dir.chdir(__dir__) do
|
17
|
+
`git ls-files -z`.split("\x0").reject do |f|
|
18
|
+
(File.expand_path(f) == __FILE__) ||
|
19
|
+
f.start_with?(*%w[bin/ test/ spec/ features/ .git .gitlab-ci.yml appveyor Gemfile])
|
20
|
+
end
|
21
|
+
end
|
22
|
+
spec.bindir = 'exe'
|
23
|
+
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
|
24
|
+
spec.require_paths = ['lib']
|
25
|
+
|
26
|
+
# Uncomment to register a new dependency of your gem
|
27
|
+
spec.add_dependency 'rails', '~> 7'
|
28
|
+
spec.add_dependency 'to_bool', '~> 2.1'
|
29
|
+
|
30
|
+
# For more information and examples about making a new gem, check out our
|
31
|
+
# guide at: https://bundler.io/guides/creating_gem.html
|
32
|
+
spec.metadata['rubygems_mfa_required'] = 'true'
|
33
|
+
spec.license = 'MIT'
|
34
|
+
end
|
metadata
ADDED
@@ -0,0 +1,87 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: nueca_rails_interfaces
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Tien
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2024-06-06 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rails
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '7'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '7'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: to_bool
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '2.1'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '2.1'
|
41
|
+
description:
|
42
|
+
email:
|
43
|
+
- tieeeeen1994@gmail.com
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- ".rspec"
|
49
|
+
- ".rubocop.yml"
|
50
|
+
- CODE_OF_CONDUCT.md
|
51
|
+
- LICENSE.txt
|
52
|
+
- README.md
|
53
|
+
- Rakefile
|
54
|
+
- lib/nueca_rails_interfaces.rb
|
55
|
+
- lib/nueca_rails_interfaces/util.rb
|
56
|
+
- lib/nueca_rails_interfaces/version.rb
|
57
|
+
- lib/v1/form_interface.rb
|
58
|
+
- lib/v1/query_interface.rb
|
59
|
+
- lib/v1/service_interface.rb
|
60
|
+
- lib/v2/form_interface.rb
|
61
|
+
- lib/v2/service_interface.rb
|
62
|
+
- nueca_rails_interfaces.gemspec
|
63
|
+
homepage: https://github.com/tieeeeen1994/nueca-rails-interfaces
|
64
|
+
licenses:
|
65
|
+
- MIT
|
66
|
+
metadata:
|
67
|
+
rubygems_mfa_required: 'true'
|
68
|
+
post_install_message:
|
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.3.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.5.10
|
84
|
+
signing_key:
|
85
|
+
specification_version: 4
|
86
|
+
summary: Interfaces for known object entities in Rails Development at Nueca.
|
87
|
+
test_files: []
|