shamu 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.codeclimate.yml +26 -0
- data/.gitignore +2 -1
- data/.rubocop.yml +89 -30
- data/.yardopts +4 -5
- data/Gemfile +24 -12
- data/Guardfile +5 -0
- data/LABELS.md +22 -0
- data/README.md +41 -0
- data/Rakefile +12 -0
- data/circle.yml +7 -3
- data/config.ru +7 -0
- data/lib/shamu/active_record.rb +7 -0
- data/lib/shamu/attributes/assignment.rb +114 -0
- data/lib/shamu/attributes/equality.rb +40 -0
- data/lib/shamu/attributes/fluid_assignment.rb +49 -0
- data/lib/shamu/attributes/validation.rb +74 -0
- data/lib/shamu/attributes.rb +255 -0
- data/lib/shamu/auditing/README.md +0 -0
- data/lib/shamu/auditing/audit_record.rb +32 -0
- data/lib/shamu/auditing/auditing_service.rb +32 -0
- data/lib/shamu/auditing/list_scope.rb +22 -0
- data/lib/shamu/auditing/logging_auditing_service.rb +16 -0
- data/lib/shamu/auditing/support.rb +75 -0
- data/lib/shamu/auditing/transaction.rb +58 -0
- data/lib/shamu/auditing.rb +12 -0
- data/lib/shamu/entities/README.md +1 -0
- data/lib/shamu/entities/active_record.rb +123 -0
- data/lib/shamu/entities/active_record_soft_destroy.rb +91 -0
- data/lib/shamu/entities/entity.rb +196 -0
- data/lib/shamu/entities/entity_path.rb +87 -0
- data/lib/shamu/entities/identity_cache.rb +64 -0
- data/lib/shamu/entities/list.rb +54 -0
- data/lib/shamu/entities/list_scope/dates.rb +57 -0
- data/lib/shamu/entities/list_scope/paging.rb +51 -0
- data/lib/shamu/entities/list_scope/scoped_paging.rb +65 -0
- data/lib/shamu/entities/list_scope/sorting.rb +76 -0
- data/lib/shamu/entities/list_scope.rb +105 -0
- data/lib/shamu/entities/null_entity.rb +88 -0
- data/lib/shamu/entities.rb +11 -0
- data/lib/shamu/error.rb +23 -5
- data/lib/shamu/events/README.md +0 -0
- data/lib/shamu/events/active_record/channel.rb +36 -0
- data/lib/shamu/events/active_record/message.rb +52 -0
- data/lib/shamu/events/active_record/migration.rb +49 -0
- data/lib/shamu/events/active_record/runner.rb +28 -0
- data/lib/shamu/events/active_record/service.rb +174 -0
- data/lib/shamu/events/active_record.rb +13 -0
- data/lib/shamu/events/channel_stats.rb +23 -0
- data/lib/shamu/events/error.rb +24 -0
- data/lib/shamu/events/events_service.rb +136 -0
- data/lib/shamu/events/in_memory/async_service.rb +48 -0
- data/lib/shamu/events/in_memory/service.rb +97 -0
- data/lib/shamu/events/in_memory.rb +10 -0
- data/lib/shamu/events/message.rb +38 -0
- data/lib/shamu/events/support.rb +60 -0
- data/lib/shamu/events.rb +12 -0
- data/lib/shamu/features/README.md +0 -0
- data/lib/shamu/features/conditions/condition.rb +39 -0
- data/lib/shamu/features/conditions/env.rb +37 -0
- data/lib/shamu/features/conditions/hosts.rb +25 -0
- data/lib/shamu/features/conditions/matching.rb +16 -0
- data/lib/shamu/features/conditions/not_matching.rb +16 -0
- data/lib/shamu/features/conditions/percentage.rb +44 -0
- data/lib/shamu/features/conditions/proc.rb +54 -0
- data/lib/shamu/features/conditions/roles.rb +23 -0
- data/lib/shamu/features/conditions/schedule_at.rb +27 -0
- data/lib/shamu/features/conditions.rb +18 -0
- data/lib/shamu/features/config_service.rb +10 -0
- data/lib/shamu/features/context.rb +80 -0
- data/lib/shamu/features/env_store.rb +88 -0
- data/lib/shamu/features/errors.rb +29 -0
- data/lib/shamu/features/features_service.rb +168 -0
- data/lib/shamu/features/list_scope.rb +30 -0
- data/lib/shamu/features/selector.rb +50 -0
- data/lib/shamu/features/support.rb +51 -0
- data/lib/shamu/features/toggle.rb +149 -0
- data/lib/shamu/features/toggle_codec.rb +69 -0
- data/lib/shamu/features.rb +16 -0
- data/lib/shamu/locale/en.yml +22 -2
- data/lib/shamu/logger.rb +13 -0
- data/lib/shamu/rack/README.md +0 -0
- data/lib/shamu/rack/cookies.rb +115 -0
- data/lib/shamu/rack/cookies_middleware.rb +26 -0
- data/lib/shamu/rack/query_params.rb +41 -0
- data/lib/shamu/rack/query_params_middleware.rb +24 -0
- data/lib/shamu/rack.rb +12 -0
- data/lib/shamu/rails/controller.rb +131 -0
- data/lib/shamu/rails/entity.rb +168 -0
- data/lib/shamu/rails/features.rb +13 -0
- data/lib/shamu/rails/railtie.rb +30 -0
- data/lib/shamu/rails.rb +10 -0
- data/lib/shamu/rspec/matchers.rb +44 -0
- data/lib/shamu/rspec.rb +1 -0
- data/lib/shamu/security/README.md +0 -0
- data/lib/shamu/security/active_record_policy.rb +106 -0
- data/lib/shamu/security/error.rb +65 -0
- data/lib/shamu/security/hashed_value.rb +71 -0
- data/lib/shamu/security/no_policy.rb +15 -0
- data/lib/shamu/security/policy.rb +289 -0
- data/lib/shamu/security/policy_refinement.rb +50 -0
- data/lib/shamu/security/policy_rule.rb +59 -0
- data/lib/shamu/security/principal.rb +72 -0
- data/lib/shamu/security/roles.rb +62 -0
- data/lib/shamu/security/roles_service.rb +30 -0
- data/lib/shamu/security/support.rb +83 -0
- data/lib/shamu/security.rb +43 -0
- data/lib/shamu/services/README.md +2 -0
- data/lib/shamu/services/active_record.rb +58 -0
- data/lib/shamu/services/active_record_crud.rb +378 -0
- data/lib/shamu/services/error.rb +24 -0
- data/lib/shamu/services/lazy_association.rb +31 -0
- data/lib/shamu/services/lazy_transform.rb +97 -0
- data/lib/shamu/services/request.rb +122 -0
- data/lib/shamu/services/request_support.rb +124 -0
- data/lib/shamu/services/result.rb +75 -0
- data/lib/shamu/services/service.rb +355 -0
- data/lib/shamu/services.rb +12 -0
- data/lib/shamu/sessions/README.md +2 -0
- data/lib/shamu/sessions/cookie_store.rb +79 -0
- data/lib/shamu/sessions/session_store.rb +42 -0
- data/lib/shamu/sessions.rb +8 -0
- data/lib/shamu/to_bool_extension.rb +57 -0
- data/lib/shamu/to_model_id_extension.rb +50 -0
- data/lib/shamu/version.rb +10 -4
- data/lib/shamu.rb +18 -6
- data/shamu.gemspec +21 -10
- data/spec/internal/README.md +4 -0
- data/spec/internal/config/database.yml +3 -0
- data/spec/internal/config/routes.rb +3 -0
- data/spec/internal/db/schema.rb +3 -0
- data/spec/internal/log/.gitignore +1 -0
- data/spec/internal/public/favicon.ico +0 -0
- data/spec/lib/shamu/active_record_support.rb +32 -0
- data/spec/lib/shamu/attributes/assignment_spec.rb +129 -0
- data/spec/lib/shamu/attributes/equality_spec.rb +63 -0
- data/spec/lib/shamu/attributes/fluid_assignment_spec.rb +31 -0
- data/spec/lib/shamu/attributes/validation_spec.rb +53 -0
- data/spec/lib/shamu/attributes_spec.rb +331 -0
- data/spec/lib/shamu/auditing/logging_auditing_service_spec.rb +18 -0
- data/spec/lib/shamu/auditing/support_spec.rb +41 -0
- data/spec/lib/shamu/entities/active_record_soft_destroy_spec.rb +82 -0
- data/spec/lib/shamu/entities/active_record_spec.rb +66 -0
- data/spec/lib/shamu/entities/entity_path_spec.rb +40 -0
- data/spec/lib/shamu/entities/entity_spec.rb +56 -0
- data/spec/lib/shamu/entities/identity_cache_spec.rb +69 -0
- data/spec/lib/shamu/entities/list_scope/dates_spec.rb +47 -0
- data/spec/lib/shamu/entities/list_scope/paging_spec.rb +41 -0
- data/spec/lib/shamu/entities/list_scope/scoped_paging_spec.rb +40 -0
- data/spec/lib/shamu/entities/list_scope/sorting_spec.rb +59 -0
- data/spec/lib/shamu/entities/list_scope_spec.rb +127 -0
- data/spec/lib/shamu/entities/list_spec.rb +60 -0
- data/spec/lib/shamu/entities/null_entity_spec.rb +94 -0
- data/spec/lib/shamu/events/active_record/migration_spec.rb +11 -0
- data/spec/lib/shamu/events/active_record/service_spec.rb +139 -0
- data/spec/lib/shamu/events/events_service_spec.rb +57 -0
- data/spec/lib/shamu/events/in_memory/async_service_spec.rb +37 -0
- data/spec/lib/shamu/events/in_memory/service_spec.rb +36 -0
- data/spec/lib/shamu/events/message_spec.rb +7 -0
- data/spec/lib/shamu/events/support_spec.rb +44 -0
- data/spec/lib/shamu/features/conditions/condition_spec.rb +8 -0
- data/spec/lib/shamu/features/conditions/env_spec.rb +29 -0
- data/spec/lib/shamu/features/conditions/hosts_spec.rb +21 -0
- data/spec/lib/shamu/features/conditions/matching_spec.rb +23 -0
- data/spec/lib/shamu/features/conditions/percentage_spec.rb +71 -0
- data/spec/lib/shamu/features/conditions/proc_spec.rb +28 -0
- data/spec/lib/shamu/features/env_store_spec.rb +48 -0
- data/spec/lib/shamu/features/features.yml +34 -0
- data/spec/lib/shamu/features/features_service_spec.rb +109 -0
- data/spec/lib/shamu/features/secondary.yml +5 -0
- data/spec/lib/shamu/features/selector_spec.rb +17 -0
- data/spec/lib/shamu/features/support_spec.rb +45 -0
- data/spec/lib/shamu/features/toggle_codec_spec.rb +28 -0
- data/spec/lib/shamu/features/toggle_spec.rb +42 -0
- data/spec/lib/shamu/rack/cookies_middleware_spec.rb +33 -0
- data/spec/lib/shamu/rack/cookies_spec.rb +43 -0
- data/spec/lib/shamu/rack/query_params_middleware_spec.rb +33 -0
- data/spec/lib/shamu/rack/query_params_spec.rb +23 -0
- data/spec/lib/shamu/rails/controller_spec.rb +74 -0
- data/spec/lib/shamu/rails/entity_spec.rb +150 -0
- data/spec/lib/shamu/rails/features.yml +13 -0
- data/spec/lib/shamu/rails/features_spec.rb +45 -0
- data/spec/lib/shamu/security/active_record_policy_spec.rb +38 -0
- data/spec/lib/shamu/security/hashed_value_spec.rb +41 -0
- data/spec/lib/shamu/security/policy_refinement_spec.rb +61 -0
- data/spec/lib/shamu/security/policy_rule_spec.rb +60 -0
- data/spec/lib/shamu/security/policy_spec.rb +158 -0
- data/spec/lib/shamu/security/roles_spec.rb +46 -0
- data/spec/lib/shamu/services/active_record_crud_spec.rb +460 -0
- data/spec/lib/shamu/services/active_record_spec.rb +92 -0
- data/spec/lib/shamu/services/lazy_association_spec.rb +31 -0
- data/spec/lib/shamu/services/lazy_transform_spec.rb +96 -0
- data/spec/lib/shamu/services/request_spec.rb +58 -0
- data/spec/lib/shamu/services/request_support_spec.rb +129 -0
- data/spec/lib/shamu/services/result_spec.rb +37 -0
- data/spec/lib/shamu/services/service_spec.rb +307 -0
- data/spec/lib/shamu/sessions/cookie_store_spec.rb +44 -0
- data/spec/lib/shamu/to_bool_extension_spec.rb +67 -0
- data/spec/lib/shamu/to_model_id_extension_spec.rb +54 -0
- data/spec/rails_helper.rb +13 -0
- data/spec/spec_helper.rb +17 -12
- data/spec/support/active_record.rb +17 -0
- data/spec/support/database.rb +14 -0
- data/spec/support/logger.rb +0 -0
- metadata +383 -9
- data/spec/lib/shamu_spec.rb +0 -5
- /data/{spec/internal/log/test.log → lib/shamu/attributes/README.md} +0 -0
@@ -0,0 +1,91 @@
|
|
1
|
+
module Shamu
|
2
|
+
module Entities
|
3
|
+
|
4
|
+
# Add the ability to "soft-delete" a record. Marking it as deleted so it is
|
5
|
+
# no longer present in the default scope but without actually removing the
|
6
|
+
# record from the database.
|
7
|
+
#
|
8
|
+
# > **Note** You must add a column `destroyed_at` to the model.
|
9
|
+
module ActiveRecordSoftDestroy
|
10
|
+
extend ActiveSupport::Concern
|
11
|
+
|
12
|
+
included do
|
13
|
+
|
14
|
+
# ============================================================================
|
15
|
+
# @!group Attributes
|
16
|
+
#
|
17
|
+
|
18
|
+
# @!attribute destroyed_at
|
19
|
+
# @return [DateTime] when the record was destroyed.
|
20
|
+
|
21
|
+
#
|
22
|
+
# @!endgroup Attributes
|
23
|
+
|
24
|
+
# ============================================================================
|
25
|
+
# @!group Scopes
|
26
|
+
#
|
27
|
+
|
28
|
+
# Limit the records to those that have been soft destroyed.
|
29
|
+
# @return [ActiveRecord::Relation]
|
30
|
+
scope :destroyed, ->() { unscope( where: :destroyed_at ).where( arel_table[ :destroyed_at ].not_eq( nil ) ) }
|
31
|
+
|
32
|
+
# Include live and soft destroyed records.
|
33
|
+
# @return [ActiveRecord::Relation]
|
34
|
+
scope :including_destroyed, ->() { unscope( where: :destroyed_at ) }
|
35
|
+
|
36
|
+
# Exclude destroyed records by dfault.
|
37
|
+
default_scope { where( destroyed_at: nil ) }
|
38
|
+
|
39
|
+
#
|
40
|
+
# @!endgroup Scopes
|
41
|
+
|
42
|
+
end
|
43
|
+
|
44
|
+
# Mark the record as deleted.
|
45
|
+
# @overload destroy
|
46
|
+
# @return [Boolean] true if the record was destroyed.
|
47
|
+
def destroy( options = nil )
|
48
|
+
if destroyed_at || ( options && options[:obliterate] )
|
49
|
+
super()
|
50
|
+
else
|
51
|
+
update_attribute :destroyed_at, Time.now.utc
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
# Really destroy the record.
|
56
|
+
# @return [Boolean] true if the record was destroyed.
|
57
|
+
def obliterate
|
58
|
+
destroy( obliterate: true )
|
59
|
+
end
|
60
|
+
|
61
|
+
# Really destroy! the record.
|
62
|
+
# @overload destroy!
|
63
|
+
# @return [Boolean] true if the record was destroyed.
|
64
|
+
def destroy!( options = nil )
|
65
|
+
if destroyed_at || ( options && options[:obliterate] )
|
66
|
+
super()
|
67
|
+
else
|
68
|
+
update_attribute :destroyed_at, Time.now.utc
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
# Really destroy! the record.
|
73
|
+
# @return [Boolean] true if the record was destroyed.
|
74
|
+
def obliterate!
|
75
|
+
destroy!( obliterate: true )
|
76
|
+
end
|
77
|
+
|
78
|
+
# Mark the record as no longer destroyed.
|
79
|
+
# @return [Boolean] true if the record was restored.
|
80
|
+
def undestroy
|
81
|
+
update_attribute :destroyed_at, nil
|
82
|
+
end
|
83
|
+
|
84
|
+
# @return [Boolean] true if the record has been soft destroyed.
|
85
|
+
def soft_destroyed?
|
86
|
+
!!destroyed_at
|
87
|
+
end
|
88
|
+
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
@@ -0,0 +1,196 @@
|
|
1
|
+
require "shamu/attributes"
|
2
|
+
require "shamu/to_model_id_extension"
|
3
|
+
require "active_model"
|
4
|
+
|
5
|
+
module Shamu
|
6
|
+
module Entities
|
7
|
+
|
8
|
+
# An entity is an abstract set of data returned from a {Services::Service}
|
9
|
+
# describing the current state of an object.
|
10
|
+
#
|
11
|
+
# Entities are **immutable**. They will not change for the duration of the
|
12
|
+
# request. Instead use a {Services::Service} to mutate the underlying data
|
13
|
+
# and request an updated copy of the Entity.
|
14
|
+
#
|
15
|
+
# See {Shamu::Entities} for more on using entities.
|
16
|
+
#
|
17
|
+
# ## Helper Methods
|
18
|
+
#
|
19
|
+
# Entities can define helper methods to perform simple calculations or
|
20
|
+
# projections of it's data. They only rely on state available by other
|
21
|
+
# attribute projections. This makes the entity cacheable and serializable.
|
22
|
+
#
|
23
|
+
# ### Why class instead of module?
|
24
|
+
#
|
25
|
+
# The Entity class is ridiculously simple. It just mixes in a few modules
|
26
|
+
# and adds a few helper methods. It could just as easily been implemented
|
27
|
+
# as a module to mixin with POROs.
|
28
|
+
#
|
29
|
+
# While modules are generally preferred for non-domain specific behaviors,
|
30
|
+
# in this case the purpose is to intentionally make it harder to mix the
|
31
|
+
# responsibilities of an Entity class with another object in your project.
|
32
|
+
# This tends to lead to better separation in your design.
|
33
|
+
#
|
34
|
+
# @example
|
35
|
+
#
|
36
|
+
# class LiveAccount < Shamu::Entities::Entity
|
37
|
+
#
|
38
|
+
# # Use an ActiveRecord User model for the actual data. Not accessible
|
39
|
+
# # to callers.
|
40
|
+
# model :user
|
41
|
+
#
|
42
|
+
# # Simple projections
|
43
|
+
# attribute :name, on: :user
|
44
|
+
# attribute :email, on: :user
|
45
|
+
#
|
46
|
+
# # Computed projections. Only calculated once, then cached in the
|
47
|
+
# # entity instance.
|
48
|
+
# attribute :signed_up_on do
|
49
|
+
# I18n.localize( user.created_at )
|
50
|
+
# end
|
51
|
+
#
|
52
|
+
# attribute :last_login_at do
|
53
|
+
# user.login_reccords.last
|
54
|
+
# end
|
55
|
+
#
|
56
|
+
# # Project another model
|
57
|
+
# model :contact
|
58
|
+
#
|
59
|
+
# # Project a JSON object using another entity class
|
60
|
+
# attribute :address, AddressEntity, on: :contact
|
61
|
+
#
|
62
|
+
# # Helper method
|
63
|
+
# def new_user?
|
64
|
+
# signed_up_on > 3.days.ago
|
65
|
+
# end
|
66
|
+
# end
|
67
|
+
#
|
68
|
+
class Entity
|
69
|
+
include Shamu::Attributes
|
70
|
+
include Shamu::Attributes::Equality
|
71
|
+
include Shamu::ToModelIdExtension::Models
|
72
|
+
|
73
|
+
# @!attribute
|
74
|
+
# @return [Object] id of the entity.
|
75
|
+
#
|
76
|
+
# Shamu makes the assumption that all entities will have a single unique
|
77
|
+
# identifier that can be used to distinguish the entity from other
|
78
|
+
# instances of the same class.
|
79
|
+
#
|
80
|
+
# While not strictly necessary in an purely abstract service, this
|
81
|
+
# invariant significantly reduces the complexity of caching, lookups and
|
82
|
+
# other helpful conventions and is almost always true in most modern
|
83
|
+
# architectures.
|
84
|
+
#
|
85
|
+
# If an Entity does not have a natural primary key id, an id may be
|
86
|
+
# generated by joining the values of the composite key that is used to
|
87
|
+
# identify the resource.
|
88
|
+
def id
|
89
|
+
fail "No id attribute defined. Add `attribute :id, on: :record` to #{ self.class.name }"
|
90
|
+
end
|
91
|
+
|
92
|
+
# @return [false] real entities are not empty. See {NullEntity}.
|
93
|
+
def empty?
|
94
|
+
false
|
95
|
+
end
|
96
|
+
alias_method :blank?, :empty?
|
97
|
+
|
98
|
+
# @return [true] the entity is present. See {NullEntity}.
|
99
|
+
def present?
|
100
|
+
!empty?
|
101
|
+
end
|
102
|
+
|
103
|
+
# Entities are always immutable - so they are considered persisted. Use a
|
104
|
+
# {Services::ChangeRequest} to back a form instead.
|
105
|
+
def persisted?
|
106
|
+
true
|
107
|
+
end
|
108
|
+
|
109
|
+
# @return [self]
|
110
|
+
def to_entity
|
111
|
+
self
|
112
|
+
end
|
113
|
+
|
114
|
+
private
|
115
|
+
|
116
|
+
def serialize_attribute?( name, options )
|
117
|
+
super && !options[:model]
|
118
|
+
end
|
119
|
+
|
120
|
+
class << self
|
121
|
+
|
122
|
+
# @return [ActiveModel::Name] used by url_helpers etc when generating
|
123
|
+
# model specific names for this entity.
|
124
|
+
def model_name
|
125
|
+
@model_name ||= begin
|
126
|
+
base_name = name.sub /(::)?Entity$/, ""
|
127
|
+
parts = base_name.split "::"
|
128
|
+
parts[-1] = parts[-1].singularize
|
129
|
+
base_name = parts.join "::"
|
130
|
+
|
131
|
+
::ActiveModel::Name.new( self, nil, base_name )
|
132
|
+
end
|
133
|
+
end
|
134
|
+
|
135
|
+
# Define custom default attributes for a {NullEntity} for this class.
|
136
|
+
# @return [Class] the {NullEntity} class for the entity.
|
137
|
+
#
|
138
|
+
# @example
|
139
|
+
#
|
140
|
+
# class Users::UserEntity < Shamu::Entities::Entity
|
141
|
+
# attribute :id
|
142
|
+
# attribute :name
|
143
|
+
# attribute :level
|
144
|
+
#
|
145
|
+
# null_entity do
|
146
|
+
# attribute :level do
|
147
|
+
# "Guest"
|
148
|
+
# end
|
149
|
+
# end
|
150
|
+
# end
|
151
|
+
def null_entity( &block )
|
152
|
+
null_class = ::Shamu::Entities::NullEntity.for( self )
|
153
|
+
null_class.class_eval( &block ) if block_given?
|
154
|
+
null_class
|
155
|
+
end
|
156
|
+
|
157
|
+
private
|
158
|
+
|
159
|
+
# @!visibility public
|
160
|
+
# Define a model attribute that the entity will project. Use additional
|
161
|
+
# {.attribute} calls to define the actual projections.
|
162
|
+
#
|
163
|
+
# Model attributes are _private_ and should never be exposed to any
|
164
|
+
# client from another domain. Instead project only the properties needed
|
165
|
+
# for the Entity's clients.
|
166
|
+
#
|
167
|
+
# @param (see Shamu::Attributes::DSL#attribute)
|
168
|
+
# @return [self]
|
169
|
+
#
|
170
|
+
# @example
|
171
|
+
#
|
172
|
+
# class Account < Shamu::Entities::Entity
|
173
|
+
# model :user
|
174
|
+
#
|
175
|
+
# attribute :username, on: :user
|
176
|
+
# attribute :email, on: :user
|
177
|
+
# end
|
178
|
+
def model( name, **args, &block )
|
179
|
+
attribute( name, **args, &block )
|
180
|
+
attributes[name][:model] = true
|
181
|
+
private name
|
182
|
+
end
|
183
|
+
|
184
|
+
# Redefined to prevent creating mutable attributes.
|
185
|
+
def attr_accessor( * )
|
186
|
+
fail "Remember, an Entity is immutable. Use a Services::Service to mutate the underlying data."
|
187
|
+
end
|
188
|
+
|
189
|
+
# Redefined to prevent creating mutable attributes.
|
190
|
+
def attr_writer( * )
|
191
|
+
fail "Remember, an Entity is immutable. Use a Services::Service to mutate the underlying data."
|
192
|
+
end
|
193
|
+
end
|
194
|
+
end
|
195
|
+
end
|
196
|
+
end
|
@@ -0,0 +1,87 @@
|
|
1
|
+
module Shamu
|
2
|
+
module Entities
|
3
|
+
|
4
|
+
# An entity path descrives one or more levels of parent/child relationships
|
5
|
+
# that can be used to navigate from the root entity to a target entity.
|
6
|
+
#
|
7
|
+
# Entity paths can be used to identify polymorphic relationships between
|
8
|
+
# entities managed by difference services.
|
9
|
+
module EntityPath
|
10
|
+
module_function
|
11
|
+
|
12
|
+
|
13
|
+
# Composes an array of entities describing the path from the root entity
|
14
|
+
# to the leaf into a string.
|
15
|
+
#
|
16
|
+
# @example
|
17
|
+
# path = compose_entity_path([
|
18
|
+
# [ "User", "45" ],
|
19
|
+
# [ "Calendar", "567" ],
|
20
|
+
# [ "Event", "1" ]
|
21
|
+
# ])
|
22
|
+
# path # => "User[45]/Calendar[567]/Event[1]"
|
23
|
+
#
|
24
|
+
# path = compose_entity_path( user )
|
25
|
+
# path # => "User[45]"
|
26
|
+
#
|
27
|
+
# @param [Array<Entities::Entity>] entities
|
28
|
+
# @return [String]
|
29
|
+
def compose_entity_path( entities )
|
30
|
+
return unless entities.present?
|
31
|
+
|
32
|
+
entities.each_with_object( "" ) do |entity, path|
|
33
|
+
path << "/" if path.present?
|
34
|
+
path << compose_single_entity( entity )
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
|
39
|
+
# Decompose an entity path into an array of arrays of entity classes with
|
40
|
+
# their ids.
|
41
|
+
#
|
42
|
+
# @example
|
43
|
+
# entities = decompose_entity_path( "User[45]/Calendar[567]/Event[1]" )
|
44
|
+
# entities # => [
|
45
|
+
# # [ "User", "45" ],
|
46
|
+
# # [ "Calendar", "567" ],
|
47
|
+
# # [ "Event", "1" ]
|
48
|
+
# # ]
|
49
|
+
#
|
50
|
+
# @param [String] path the composed entity path.
|
51
|
+
# @return [Array<Array<String,String>>] the entities with their ids.
|
52
|
+
def decompose_entity_path( path )
|
53
|
+
return unless path.present?
|
54
|
+
|
55
|
+
path.split( "/" ).map do |node|
|
56
|
+
entity, id = node.split "["
|
57
|
+
|
58
|
+
[ entity, id[ 0..-2 ] ]
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
private
|
63
|
+
|
64
|
+
def compose_single_entity( entity )
|
65
|
+
case entity
|
66
|
+
when Entities::Entity then build_composed_entity_path( entity.class.model_name.name, entity.id )
|
67
|
+
when Array then build_composed_entity_path( entity_path_name( entity.first ), entity.last )
|
68
|
+
when /([A-Z][a-z0-9]*)+/ then entity
|
69
|
+
else fail "Don't know how to compose #{ entity }"
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
def entity_path_name( entity )
|
74
|
+
case entity
|
75
|
+
when String then entity.sub( /Entity$/, "" )
|
76
|
+
when Class then Class.model_name.name
|
77
|
+
else fail "Don't know how to compose #{ entity }"
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
def build_composed_entity_path( name, id )
|
82
|
+
"#{ name }[#{ id }]"
|
83
|
+
end
|
84
|
+
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
@@ -0,0 +1,64 @@
|
|
1
|
+
module Shamu
|
2
|
+
module Entities
|
3
|
+
|
4
|
+
# Keeps a cache of {Entity} instances in memory for quick retrieval. Since
|
5
|
+
# entities are immutable, the cache
|
6
|
+
class IdentityCache
|
7
|
+
|
8
|
+
# Provide a block to automatically coerce keys to a known type. For
|
9
|
+
# example converting numeric strings ("123") to Integer values.
|
10
|
+
#
|
11
|
+
# @param [Symbol] coercion method to call on keys instead of providing a
|
12
|
+
# block.
|
13
|
+
# @yield (key)
|
14
|
+
# @yieldparam [Object] key to coerce
|
15
|
+
# @yieldreturn [Object] the coerced value of the key.
|
16
|
+
def initialize( coercion = nil, &coercion_block )
|
17
|
+
@cache = {}
|
18
|
+
@coercion = block_given? ? coercion_block : ( coercion && coercion.to_proc )
|
19
|
+
end
|
20
|
+
|
21
|
+
# Fetch an entity with the given key.
|
22
|
+
# @param [Object] key of the entity. Typically the {Entity#id}.
|
23
|
+
# @return [Entity] the entity if found, otherwise `nil`.
|
24
|
+
def fetch( key )
|
25
|
+
cache.fetch( coerce_key( key ), nil )
|
26
|
+
end
|
27
|
+
|
28
|
+
# Filter the list of `keys` to those that haven't been cached yet.
|
29
|
+
# @param [Array] keys of the {Entity entities} that are about to be
|
30
|
+
# {#fetch fetched}.
|
31
|
+
# @return [Array] the uncached keys.
|
32
|
+
def uncached_keys( keys )
|
33
|
+
uncached = Array( keys ).map { |k| coerce_key( k ) }
|
34
|
+
uncached.reject! { |k| cache.key?( k ) }
|
35
|
+
uncached
|
36
|
+
end
|
37
|
+
|
38
|
+
# Add a new entity to the cache.
|
39
|
+
# @param [Object] key of the entity. Typically the {Entity#id}.
|
40
|
+
# @param [Entity] entity to cache
|
41
|
+
# @return [entity]
|
42
|
+
def add( key, entity )
|
43
|
+
cache[ coerce_key( key ) ] = entity
|
44
|
+
end
|
45
|
+
|
46
|
+
# Invalidate the cached entry for the {Entity} with the given `key`.
|
47
|
+
# @param [Object] key of the entity. Typically the {Entity#id}.
|
48
|
+
# @return [Entity] the entity that was at the given key if present.
|
49
|
+
def invalidate( key )
|
50
|
+
cache.delete( key )
|
51
|
+
end
|
52
|
+
|
53
|
+
private
|
54
|
+
|
55
|
+
attr_reader :cache
|
56
|
+
attr_reader :coercion
|
57
|
+
|
58
|
+
def coerce_key( key )
|
59
|
+
return key unless coercion
|
60
|
+
coercion.call( key )
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
module Shamu
|
2
|
+
module Entities
|
3
|
+
|
4
|
+
# A list of {Entities::Entity} records.
|
5
|
+
class List
|
6
|
+
include Enumerable
|
7
|
+
|
8
|
+
# @param [Enumerable] entities the raw list of entities.
|
9
|
+
def initialize( entities )
|
10
|
+
fail ArgumentError, "missing entities" if entities.nil?
|
11
|
+
@raw_entities = entities
|
12
|
+
end
|
13
|
+
|
14
|
+
# Enumerate through each of the entities in the list.
|
15
|
+
def each( &block )
|
16
|
+
entities.each( &block )
|
17
|
+
end
|
18
|
+
|
19
|
+
delegate :first, :count, :empty?, to: :raw_entities
|
20
|
+
|
21
|
+
alias_method :size, :count
|
22
|
+
alias_method :length, :count
|
23
|
+
|
24
|
+
# Get an entity by it's primary key.
|
25
|
+
# @param [Object] key the primary key to look for.
|
26
|
+
# @param [Symbol] field to use as the primary key. Default :id.
|
27
|
+
# @return [Entities::Entity] the found entity.
|
28
|
+
# @raise [Shamu::NotFoundError] if the entity cannot be found.
|
29
|
+
def get( key, field: key_attribute )
|
30
|
+
entity =
|
31
|
+
if field == :id
|
32
|
+
entities.find { |e| e.id == key }
|
33
|
+
else
|
34
|
+
entities.find { |e| e.send( field ) == key }
|
35
|
+
end
|
36
|
+
entity || fail( Shamu::NotFoundError )
|
37
|
+
end
|
38
|
+
|
39
|
+
private
|
40
|
+
|
41
|
+
attr_reader :raw_entities
|
42
|
+
|
43
|
+
def entities
|
44
|
+
# Array and others do not implement #lazy so allow anything that
|
45
|
+
# doesn't support lazy to just enumerate directly.
|
46
|
+
@entities ||= raw_entities.lazy || raw_entities
|
47
|
+
end
|
48
|
+
|
49
|
+
def key_attribute
|
50
|
+
:id
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
module Shamu
|
2
|
+
module Entities
|
3
|
+
class ListScope
|
4
|
+
|
5
|
+
# Include paging parsing and attributes. Adds the following attributes
|
6
|
+
# to the list scope:
|
7
|
+
#
|
8
|
+
# ```
|
9
|
+
# class UsersListScope < Shamu::Entities::ListScope
|
10
|
+
# include Shamu::Entities::ListScope::Paging
|
11
|
+
# end
|
12
|
+
#
|
13
|
+
# scope = UsersListScope.coerce!( params )
|
14
|
+
# scope.page # => 1
|
15
|
+
# scope.per_page # => 25
|
16
|
+
# ```
|
17
|
+
module Dates
|
18
|
+
|
19
|
+
# ============================================================================
|
20
|
+
# @!group Attributes
|
21
|
+
#
|
22
|
+
|
23
|
+
# @!attribute since
|
24
|
+
# @return [Time] include only records added since the given timestamp.
|
25
|
+
|
26
|
+
# @!attribute until
|
27
|
+
# @return [Time] include only records up until the given timestamp.
|
28
|
+
|
29
|
+
# @!attribute default_since
|
30
|
+
# @return [Time] default {#since} if not specified.
|
31
|
+
|
32
|
+
# @!attribute default_until
|
33
|
+
# @return [Time] default {#until} if not specified.
|
34
|
+
|
35
|
+
#
|
36
|
+
# @!endgroup Attributes
|
37
|
+
|
38
|
+
def self.included( base )
|
39
|
+
super
|
40
|
+
|
41
|
+
coerce = Time.instance_method( :to_time ) ? :to_time : nil
|
42
|
+
|
43
|
+
base.attribute :since, coerce: coerce, default: ->() { default_since }
|
44
|
+
base.attribute :default_since, coerce: coerce, serialize: false
|
45
|
+
base.attribute :until, coerce: coerce, default: ->() { default_until }
|
46
|
+
base.attribute :default_until, coerce: coerce, serialize: false
|
47
|
+
end
|
48
|
+
|
49
|
+
# @return [Boolean] true if the scope is dated.
|
50
|
+
def dated?
|
51
|
+
!!self.since || !!self.until # rubocop:disable Style/RedundantSelf
|
52
|
+
end
|
53
|
+
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
module Shamu
|
2
|
+
module Entities
|
3
|
+
class ListScope
|
4
|
+
|
5
|
+
# Include paging parsing and attributes. Adds the following attributes
|
6
|
+
# to the list scope:
|
7
|
+
#
|
8
|
+
# ```
|
9
|
+
# class UsersListScope < Shamu::Entities::ListScope
|
10
|
+
# include Shamu::Entities::ListScope::Paging
|
11
|
+
# end
|
12
|
+
#
|
13
|
+
# scope = UsersListScope.coerce!( params )
|
14
|
+
# scope.page # => 1
|
15
|
+
# scope.per_page # => 25
|
16
|
+
# ```
|
17
|
+
module Paging
|
18
|
+
|
19
|
+
# ============================================================================
|
20
|
+
# @!group Attributes
|
21
|
+
#
|
22
|
+
|
23
|
+
# @!attribute page
|
24
|
+
# @return [Integer] the current page number. 1 based.
|
25
|
+
|
26
|
+
# @!attribute per_page
|
27
|
+
# @return [Integer] number of records per page.
|
28
|
+
|
29
|
+
# @!attribute default_per_page
|
30
|
+
# @return [Integer] default number of records per page if not provided.
|
31
|
+
|
32
|
+
#
|
33
|
+
# @!endgroup Attributes
|
34
|
+
|
35
|
+
def self.included( base )
|
36
|
+
super
|
37
|
+
|
38
|
+
base.attribute :page, coerce: :to_i
|
39
|
+
base.attribute :per_page, coerce: :to_i, default: ->() { default_per_page }
|
40
|
+
base.attribute :default_per_page, coerce: :to_i, default: 25, serialize: false
|
41
|
+
end
|
42
|
+
|
43
|
+
# @return [Boolean] true if the scope is paged.
|
44
|
+
def paged?
|
45
|
+
!!page
|
46
|
+
end
|
47
|
+
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
@@ -0,0 +1,65 @@
|
|
1
|
+
module Shamu
|
2
|
+
module Entities
|
3
|
+
class ListScope
|
4
|
+
|
5
|
+
# Include paging parsing and attributes exposed as a nested page object.
|
6
|
+
# Adds the following attributes to the list scope:
|
7
|
+
#
|
8
|
+
# ```
|
9
|
+
# class UsersListScope < Shamu::Entities::ListScope
|
10
|
+
# include Shamu::Entities::ListScope::ScopedPaging
|
11
|
+
# end
|
12
|
+
#
|
13
|
+
# scope = UsersListScope.coerce!( page: { number: 5, size: 50 } )
|
14
|
+
# scope.page.number # => 5
|
15
|
+
# scope.page.size # => 50
|
16
|
+
# ```
|
17
|
+
module ScopedPaging
|
18
|
+
|
19
|
+
# ============================================================================
|
20
|
+
# @!group Attributes
|
21
|
+
#
|
22
|
+
|
23
|
+
# @!attribute page
|
24
|
+
# @return [PageScope] the paging scope.
|
25
|
+
|
26
|
+
#
|
27
|
+
# @!endgroup Attributes
|
28
|
+
|
29
|
+
def self.included( base )
|
30
|
+
super
|
31
|
+
|
32
|
+
base.attribute :page, build: PageScope, default: PageScope.new
|
33
|
+
end
|
34
|
+
|
35
|
+
# @return [Boolean] true if the scope is paged.
|
36
|
+
def scoped_page?
|
37
|
+
!!page.number
|
38
|
+
end
|
39
|
+
|
40
|
+
# The scope of a [ScopedPaging] list scope.
|
41
|
+
class PageScope
|
42
|
+
include Shamu::Attributes
|
43
|
+
|
44
|
+
# @!attribute
|
45
|
+
# @return [Integer] the page number.
|
46
|
+
attribute :number, coerce: :to_i
|
47
|
+
|
48
|
+
# @!attribute
|
49
|
+
# @return [Integer] the size of each page.
|
50
|
+
attribute :size, coerce: :to_i, default: ->() { default_size }
|
51
|
+
|
52
|
+
# @!attribute
|
53
|
+
# @return [Integer] the default page size if not specified.
|
54
|
+
attribute :default_size, coerce: :to_i, default: 25, serialize: false
|
55
|
+
|
56
|
+
# @return [Hash] the scope as a params hash.
|
57
|
+
def params
|
58
|
+
to_attributes
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|