octodomain 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: d6b33cbb7c9311b3aaa76529f9b3aa6918c40a5856e256dbd690b92fc0e6097d
4
+ data.tar.gz: 7e2b164f1d142d1d6d06dd35a3afa36edf103780f3183b2e5ccb8612358fa024
5
+ SHA512:
6
+ metadata.gz: 12bf5c5653e6521b9ed176c6195231a8c3ea5e2d1f3cf6719504793a523a2a0a997236990cf7652222ca621e2da1b6e9ae034f468e7a340060d0b01b9a7e07ea
7
+ data.tar.gz: 1001ebf456d26aeaff5062236fc793ed7fa5c10d1d0c44b7ba60a8614643f570badb945b7b54b478ade191707b645ad167f19db4776a0aa7583589725de938cc
data/.rspec_status ADDED
@@ -0,0 +1,7 @@
1
+ example_id | status | run_time |
2
+ ------------------------------------ | ------ | --------------- |
3
+ ./spec/octo_domain/base_spec.rb[1:1] | passed | 0.00056 seconds |
4
+ ./spec/octo_domain/base_spec.rb[1:2] | passed | 0.00067 seconds |
5
+ ./spec/octo_domain/base_spec.rb[1:3] | passed | 0.00007 seconds |
6
+ ./spec/octo_domain/base_spec.rb[1:4] | passed | 0.00005 seconds |
7
+ ./spec/octo_domain_spec.rb[1:1] | passed | 0.00028 seconds |
data/.standard.yml ADDED
@@ -0,0 +1,3 @@
1
+ # For available configuration options, see:
2
+ # https://github.com/standardrb/standard
3
+ ruby_version: 2.6
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2024-01-27
4
+
5
+ - Initial release
@@ -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 blake@blakewilliams.me. 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/README.md ADDED
@@ -0,0 +1,68 @@
1
+ # OctoDomain
2
+
3
+ OctoDomain is a Ruby gem that abstracts data models and allows for the easy and safe creation of domain objects when an application becomes sufficiently complex to warrant them.
4
+
5
+ Benefits:
6
+
7
+ - Creating domain objects and mapping them to data models is easy
8
+ - Domains are easy to test
9
+ - Clients can be created to attribute load to specific dependencies
10
+ - Extensible with solid defaults. Pass a custom transport to use a different messaging system e.g. JSON.
11
+ - Ensures domain arguments are primitives that could be serialized if the domain is extracted.
12
+
13
+ ## Installation
14
+
15
+ Install the gem and add to the application's Gemfile by executing:
16
+
17
+ ```
18
+ $ bundle add octodomain
19
+ ```
20
+
21
+ ## Usage
22
+
23
+ OctoDomain allows you to expose your data access in the form of domain objects that are accessed by a client.
24
+
25
+ For example, a user domain may expose methods to find and create users like the following:
26
+
27
+ ```ruby
28
+ class UserDomain < OctoDomain::Base
29
+ # Creates a MyDomain::User value object that can be used to map the result of
30
+ # domain methods to a value object
31
+ value :user do
32
+ attribute :id
33
+ attribute :email
34
+ attribute :display_name
35
+ end
36
+
37
+ # message exposes a domain method that can be called by a client
38
+ message :create_user # returns nil, because there is no serializer passed
39
+ message :get_user, serialize_with: :user # maps the returned user model object to the `user` value object defined above automatically
40
+
41
+ private
42
+
43
+ # Implements the `create_user` message behavior exposed by the message above
44
+ def create_user(name, age, addresses)
45
+ OctoDomain::BaseTest::Person.create({name: name, age: age, addresses: addresses})
46
+ nil
47
+ end
48
+
49
+ # Implements the `get_user` message behavior
50
+ def find(id)
51
+ OctoDomain::BaseTest::Person.find(id)
52
+ end
53
+ end
54
+ ```
55
+
56
+ Then to interact with the domain, you would create a client:
57
+
58
+ ```ruby
59
+ # Optionally specify a transport to use. e.g. use LocalTransport for method calls, or implement JSONTransport for JSON over HTTP
60
+ user_domain = UserDomain.client_for("auth_domain", transport: OctoDomain::LocalTransport.new)
61
+
62
+ user_domain.create_person("Fox Mulder", 30, ["123 Main St"])
63
+ user = user_domain.get_person(31) # returns a UserDomain::User value object
64
+ ```
65
+
66
+ ## Development
67
+
68
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests.
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "minitest/test_task"
5
+
6
+ Minitest::TestTask.create
7
+
8
+ require "standard/rake"
9
+
10
+ task default: %i[test standard]
@@ -0,0 +1,71 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_support"
4
+
5
+ module OctoDomain
6
+ # Base is the base class for all domain objects, which expose domain behavior
7
+ # to the application layer.
8
+ class Base
9
+ def self.inherited(subclass)
10
+ @subclasses ||= []
11
+ @subclasses << subclass
12
+ subclass.instance_variable_set(:@objects, @objects)
13
+ subclass.instance_variable_set(:@messages, @messages)
14
+ subclass.instance_variable_set(:@transport, @transport)
15
+ end
16
+
17
+ # Creates a new instance of the domain which acts as the client to the
18
+ # domain itself. It's passed a client_name which is used to identify the
19
+ # caller to the domain.
20
+ def self.client_for(client_name, transport: LocalTransport.new)
21
+ transport.domain = self
22
+ Client.new(messages, transport, middlewares, client_name)
23
+ end
24
+
25
+ def self.value(name, &block)
26
+ domain_class = Class.new
27
+ const_set(name.to_s.split("_").map(&:capitalize).join, domain_class)
28
+
29
+ values[name.to_sym] = ValueMapper.new(name, domain_class)
30
+ values[name.to_sym].instance_exec(&block)
31
+ end
32
+
33
+ def self.values
34
+ @values ||= {}
35
+ end
36
+
37
+ def self.validate
38
+ subclasses.each do |subclass|
39
+ subclass.messages.each do |_name, message|
40
+ raise ArgumentError.new("#{self.class.name} does not respond to #{name}") unless instance_methods.include?(message.name.to_sym)
41
+ raise ArgumentError.new("#{self.class.name} does not respond have a #{message.serialize_with} object") unless instance_methods.include?(message.name.to_sym)
42
+ end
43
+ end
44
+ end
45
+
46
+ def self.message(name, serialize_with: nil)
47
+ messages[name.to_sym] = Message.new(name, serialize_with: serialize_with)
48
+ end
49
+
50
+ def self.messages
51
+ @messages ||= {}
52
+ end
53
+
54
+ def self.serialize(message, result)
55
+ value_mapper = values[messages[message].serialize_with]
56
+ value_mapper&.serialize(result)
57
+ end
58
+
59
+ def self.use(middleware, opts = {})
60
+ middlewares.push(middleware.new(self, opts))
61
+ end
62
+
63
+ def self.middlewares
64
+ @middlewares ||= []
65
+ end
66
+
67
+ private
68
+
69
+ attr_reader :subclasses
70
+ end
71
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OctoDomain
4
+ # LocalTransport calls methods on the domain object directly in-process.
5
+ class BaseTransport
6
+ attr_accessor :domain
7
+ end
8
+ end
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OctoDomain
4
+ class Client
5
+ VALID_ARGUMENT_TYPES = Set.new([String, Integer, Float, TrueClass, FalseClass, NilClass]).freeze
6
+
7
+ def initialize(messages, transport, middlewares, client_name)
8
+ @domain = domain
9
+ @client_name = client_name
10
+
11
+ # Create public methods for each message in the domain
12
+ messages.each do |_name, message|
13
+ define_singleton_method(message.name) do |*args|
14
+ validate_arguments(args)
15
+ middlewares.each do |middleware|
16
+ middleware.call(message.name, args)
17
+ end
18
+
19
+ transport.call(message.name, args)
20
+ end
21
+ end
22
+ end
23
+
24
+ private
25
+
26
+ attr_reader :domain
27
+
28
+ # Ensures each argument is a primitive and not an object. e.g. Strings,
29
+ # Integers, etc are fine. Models are not.
30
+ def validate_arguments(args)
31
+ args.each do |arg|
32
+ if arg.is_a?(Array)
33
+ validate_arguments(arg)
34
+ elsif arg.is_a?(Hash)
35
+ validate_arguments(arg.values)
36
+ else
37
+ raise ArgumentError.new("Argument #{arg} is not a primitive") unless VALID_ARGUMENT_TYPES.include?(arg.class)
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OctoDomain
4
+ # LocalTransport calls methods on the domain object directly in-process.
5
+ class LocalTransport < BaseTransport
6
+ def call(message, args)
7
+ result = domain.new.send(message, *args)
8
+ domain.serialize(message, result)
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OctoDomain
4
+ class Message
5
+ attr_reader :name, :serialize_with
6
+
7
+ def initialize(name, serialize_with: nil)
8
+ @name = name
9
+ @serialize_with = serialize_with
10
+ end
11
+
12
+ def serialize(result, domain_object)
13
+ # TODO raise if result is not nil and serialize_with is nil, or vice versa
14
+ if result && serialize_with
15
+ domain_object.serialize(result)
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_support"
4
+
5
+ module OctoDomain
6
+ module Middleware
7
+ # Latency is a middleware that adds latency to a domain method. This is
8
+ # useful for testing how migrating a domain method to an RPC call will
9
+ # affect the application.
10
+ class Latency
11
+ def initialize(domain, options = {})
12
+ @domain = domain
13
+ @sleep = options[:ms] || 45
14
+ end
15
+
16
+ def call(message_name, args)
17
+ sleep(@sleep / 1000.0)
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OctoDomain
4
+ # DomainObject represents a domain object that is converted from an
5
+ # ActiveRecord model. It is used to represent the data that is passed between
6
+ # the application and the domain layer.
7
+ class ValueMapper
8
+ attr_reader :name, :value_class
9
+
10
+ def initialize(name, value_class)
11
+ @name = name
12
+ @value_class = value_class
13
+ end
14
+
15
+ def attribute(field)
16
+ attributes << field.to_sym
17
+
18
+ value_class.define_method(field) do
19
+ instance_variable_get(:"@#{field}")
20
+ end
21
+
22
+ value_class.define_method(:"#{field}=") do |value|
23
+ instance_variable_set(:"@#{field}", value)
24
+ end
25
+ end
26
+
27
+ def attributes
28
+ @attributes ||= Set.new
29
+ end
30
+
31
+ def serialize(result)
32
+ value_class.new.tap do |value|
33
+ attributes.each do |name|
34
+ value.public_send(:"#{name}=", result.public_send(name))
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OctoDomain
4
+ VERSION = "0.0.1"
5
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "octo_domain/version"
4
+ require_relative "octo_domain/base"
5
+ require_relative "octo_domain/client"
6
+ require_relative "octo_domain/value_mapper"
7
+ require_relative "octo_domain/base_transport"
8
+ require_relative "octo_domain/local_transport"
9
+ require_relative "octo_domain/message"
10
+
11
+ module OctoDomain
12
+ class Error < StandardError; end
13
+ end
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: octodomain
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Blake Williams
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2024-01-28 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: '7.1'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '7.1'
27
+ description:
28
+ email:
29
+ - blake@blakewilliams.me
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - ".rspec_status"
35
+ - ".standard.yml"
36
+ - CHANGELOG.md
37
+ - CODE_OF_CONDUCT.md
38
+ - README.md
39
+ - Rakefile
40
+ - lib/octo_domain.rb
41
+ - lib/octo_domain/base.rb
42
+ - lib/octo_domain/base_transport.rb
43
+ - lib/octo_domain/client.rb
44
+ - lib/octo_domain/local_transport.rb
45
+ - lib/octo_domain/message.rb
46
+ - lib/octo_domain/middleware/latency.rb
47
+ - lib/octo_domain/value_mapper.rb
48
+ - lib/octo_domain/version.rb
49
+ homepage: https://github.com/blakewilliams/octodomain
50
+ licenses: []
51
+ metadata:
52
+ allowed_push_host: https://rubygems.org
53
+ homepage_uri: https://github.com/blakewilliams/octodomain
54
+ source_code_uri: https://github.com/blakewilliams/octodomain
55
+ post_install_message:
56
+ rdoc_options: []
57
+ require_paths:
58
+ - lib
59
+ required_ruby_version: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: 2.6.0
64
+ required_rubygems_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ requirements: []
70
+ rubygems_version: 3.4.6
71
+ signing_key:
72
+ specification_version: 4
73
+ summary: Mapping between ActiveRecord models to domain objects.
74
+ test_files: []