tanoshimu_utils 1.2.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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 94d8a2972a0f374b1444ddfa75ac5fb17f37476b1f4e9358a0224dc90c59292c
4
+ data.tar.gz: 8c75231567546dc72f66f2555c7f9f11342bc4e077aeb30647f1ea793bbfc18c
5
+ SHA512:
6
+ metadata.gz: 0c0e33da8f5af2787be93c424416339f3961d6750634e70bddd52ef3df381dd1fa77d37e8ea6abc5a74f7c132f985a9784ffb1d9463f3beee9c6a32b66c2b575
7
+ data.tar.gz: '0692d2bdab66af736da4a30208c12fa618651e677dc9f94a9cf6dbbb5aa840998bc99ff015177a577dfef0b6a18ae9cd2d583be40dcdbc46c74e56761fcdf687'
@@ -0,0 +1,9 @@
1
+ require 'active_support'
2
+
3
+ require 'tanoshimu_utils/version'
4
+ require_relative 'tanoshimu_utils/concerns'
5
+
6
+ module TanoshimuUtils
7
+ class Error < StandardError; end
8
+ # Your code goes here...
9
+ end
@@ -0,0 +1,5 @@
1
+ require_relative 'concerns/get_record'
2
+ require_relative 'concerns/identifiable'
3
+ require_relative 'concerns/resource_fetch'
4
+ require_relative 'concerns/respond_to_types'
5
+ require_relative 'concerns/translatable'
@@ -0,0 +1,13 @@
1
+ module TanoshimuUtils
2
+ module Concerns
3
+ module GetRecord
4
+ extend ActiveSupport::Concern
5
+
6
+ included do
7
+ def record
8
+ used_by_model.classify.constantize.find(model_id)
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,25 @@
1
+ module TanoshimuUtils
2
+ module Concerns
3
+ module Identifiable
4
+ extend ActiveSupport::Concern
5
+
6
+ included do
7
+ before_validation :ensure_identification
8
+
9
+ validates :identification, presence: true
10
+ end
11
+
12
+ private
13
+
14
+ def ensure_identification
15
+ raise ArgumentError, 'must respond to `identification`' unless respond_to?(:identification)
16
+
17
+ self.identification = SecureRandom.hex
18
+
19
+ until self.class.where(identification: self.identification)
20
+ self.identification = SecureRandom.hex
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,160 @@
1
+ module TanoshimuUtils
2
+ module Concerns
3
+ module ResourceFetch
4
+ extend ActiveSupport::Concern
5
+
6
+ class ResourceNotAttachable < StandardError
7
+ def initialize(resource_name)
8
+ super("Resource #{resource_name} not attachable.")
9
+ end
10
+ end
11
+
12
+ class_methods do
13
+ def has_resource(resource_name, default_url: '/', expiry: 1.day)
14
+ resource_url = :"#{resource_name}_url"
15
+ db_resource_url = :"db_#{resource_url}?"
16
+ send(:define_method, db_resource_url) do
17
+ @db_resource_url ||= self.class.column_names.include?(resource_url.to_s)
18
+ end
19
+ send(:define_method, resource_url) do
20
+ ensure_fetchable_resource!(resource_name)
21
+ resource_url_for(resource_name, default_url: default_url, expiry: expiry)
22
+ end
23
+ send(:define_method, "#{resource_url}!") do
24
+ attachment = resource_for(resource_name).attachment
25
+ return default_url if attachment.nil?
26
+
27
+ if Config.uses_disk_storage?
28
+ Rails.application.routes.url_helpers.rails_blob_url(attachment, only_path: true)
29
+ else
30
+ attachment.service_url(expires_in: expiry)
31
+ end
32
+ end
33
+ send(:define_method, "#{resource_name}?") do
34
+ (fetch!(resource_name, default_url)&.attached?).present?
35
+ end
36
+ end
37
+ end
38
+
39
+ private
40
+
41
+ def resource_for(resource_name)
42
+ method(resource_name).call
43
+ end
44
+
45
+ def resource_url_for(resource_name, default_url: '/', expiry: 1.day)
46
+ ensure_attachable_resource!(resource_name)
47
+ resource_url = :"#{resource_name}_url"
48
+ if try(:"db_#{resource_url}?")
49
+ self[resource_url] || default_url
50
+ else
51
+ resource = resource_for(resource_name)
52
+ if resource.attached?
53
+ resource.service_url(expires_in: expiry)
54
+ else
55
+ default_url
56
+ end
57
+ end
58
+ end
59
+
60
+ def fetch!(resource_name, default_url)
61
+ ensure_attachable_resource!(resource_name)
62
+ resource = resource_for(resource_name)
63
+ #return resource if resource.attached?
64
+
65
+ #resource.attach(io: File.open("./public/#{default_url}"), filename: "episode-#{id}")
66
+
67
+ rescue ResourceFetch::ResourceNotAttachable, Errno::ENOENT
68
+ nil
69
+ end
70
+
71
+ def ensure_fetchable_resource!(resource_name)
72
+ raise NoMethodError, "Don't know how to fetch #{resource_name}" unless respond_to?(resource_name)
73
+ end
74
+
75
+ def ensure_attachable_resource!(resource_name)
76
+ resource_for(resource_name)
77
+ rescue NameError
78
+ raise ResourceFetch::ResourceNotAttachable.new(resource_name)#, resource unless resource.respond_to?(:attached?)
79
+ end
80
+ end
81
+ module ResourceFetch
82
+ extend ActiveSupport::Concern
83
+
84
+ class ResourceNotAttachable < StandardError
85
+ def initialize(resource_name)
86
+ super("Resource #{resource_name} not attachable.")
87
+ end
88
+ end
89
+
90
+ class_methods do
91
+ def has_resource(resource_name, default_url: '/', expiry: 1.day)
92
+ resource_url = :"#{resource_name}_url"
93
+ db_resource_url = :"db_#{resource_url}?"
94
+ send(:define_method, db_resource_url) do
95
+ @db_resource_url ||= self.class.column_names.include?(resource_url.to_s)
96
+ end
97
+ send(:define_method, resource_url) do
98
+ ensure_fetchable_resource!(resource_name)
99
+ resource_url_for(resource_name, default_url: default_url, expiry: expiry)
100
+ end
101
+ send(:define_method, "#{resource_url}!") do
102
+ attachment = resource_for(resource_name).attachment
103
+ return default_url if attachment.nil?
104
+
105
+ if Config.uses_disk_storage?
106
+ Rails.application.routes.url_helpers.rails_blob_url(attachment, only_path: true)
107
+ else
108
+ attachment.service_url(expires_in: expiry)
109
+ end
110
+ end
111
+ send(:define_method, "#{resource_name}?") do
112
+ (fetch!(resource_name, default_url)&.attached?).present?
113
+ end
114
+ end
115
+ end
116
+
117
+ private
118
+
119
+ def resource_for(resource_name)
120
+ method(resource_name).call
121
+ end
122
+
123
+ def resource_url_for(resource_name, default_url: '/', expiry: 1.day)
124
+ ensure_attachable_resource!(resource_name)
125
+ resource_url = :"#{resource_name}_url"
126
+ if try(:"db_#{resource_url}?")
127
+ self[resource_url] || default_url
128
+ else
129
+ resource = resource_for(resource_name)
130
+ if resource.attached?
131
+ resource.service_url(expires_in: expiry)
132
+ else
133
+ default_url
134
+ end
135
+ end
136
+ end
137
+
138
+ def fetch!(resource_name, default_url)
139
+ ensure_attachable_resource!(resource_name)
140
+ resource = resource_for(resource_name)
141
+ #return resource if resource.attached?
142
+
143
+ #resource.attach(io: File.open("./public/#{default_url}"), filename: "episode-#{id}")
144
+
145
+ rescue ResourceFetch::ResourceNotAttachable, Errno::ENOENT
146
+ nil
147
+ end
148
+
149
+ def ensure_fetchable_resource!(resource_name)
150
+ raise NoMethodError, "Don't know how to fetch #{resource_name}" unless respond_to?(resource_name)
151
+ end
152
+
153
+ def ensure_attachable_resource!(resource_name)
154
+ resource_for(resource_name)
155
+ rescue NameError
156
+ raise ResourceFetch::ResourceNotAttachable.new(resource_name)#, resource unless resource.respond_to?(:attached?)
157
+ end
158
+ end
159
+ end
160
+ end
@@ -0,0 +1,19 @@
1
+ module TanoshimuUtils
2
+ module Concerns
3
+ module RespondToTypes
4
+ extend ActiveSupport::Concern
5
+
6
+ class_methods do
7
+ def respond_to_types(types)
8
+ raise ArgumentError, 'Expected types Array' unless types.class == Array
9
+
10
+ types.each do |type|
11
+ send(:define_method, :"#{type}?") do
12
+ self.user_type == type
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,46 @@
1
+ module TanoshimuUtils
2
+ module Concerns
3
+ module Translatable
4
+ extend ActiveSupport::Concern
5
+
6
+ def metaclass
7
+ class << self
8
+ self
9
+ end
10
+ end
11
+
12
+ class_methods do
13
+ def translates(field, through: [], default:)
14
+ raise 'Field must be present' unless field.present?
15
+ raise 'Through must be present' unless through.present?
16
+
17
+ if column_names.include?(field.to_s)
18
+ raise 'Field must not be defined on the model'
19
+ end
20
+
21
+ through = [through] unless through.kind_of?(Array)
22
+ default = default.to_sym
23
+
24
+ unless I18n.available_locales.include?(default)
25
+ raise "#{default} is not available on your system as a locale"
26
+ end
27
+
28
+ send(:define_method, field.to_sym) do
29
+ through.each do |possible_locale|
30
+ possible_locale = possible_locale.to_sym
31
+
32
+ unless I18n.available_locales.include?(possible_locale)
33
+ next
34
+ end
35
+ end
36
+
37
+ return nil unless through.include?(I18n.locale)
38
+ value = send(I18n.locale)
39
+ value = send(default) if value.nil? && default
40
+ value
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,2 @@
1
+ require_relative 'validators/presence_one_of'
2
+ require_relative 'validators/user_like'
@@ -0,0 +1,21 @@
1
+ module TanoshimuUtils
2
+ module Validators
3
+ module PresenceOneOf
4
+ extend ActiveSupport::Concern
5
+
6
+ class AnyPresenceValidator < ActiveModel::Validator
7
+ def validate(record)
8
+ unless options[:fields].any?{|attr| record[attr].present?}
9
+ record.errors.add(:title, 'must be present (at lease one of en, fr, or jp)')
10
+ end
11
+ end
12
+ end
13
+
14
+ class_methods do
15
+ def validate_presence_one_of(one_of)
16
+ validates_with AnyPresenceValidator, fields: one_of
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,17 @@
1
+ module TanoshimuUtils
2
+ module Validators
3
+ module UserLike
4
+ extend ActiveSupport::Concern
5
+
6
+ class_methods do
7
+ def validate_like_user(user_types:)
8
+ raise ArgumentError, 'Expected users types' unless user_types.class == Array
9
+
10
+ validates :username, presence: true, uniqueness: true
11
+ validates :name, presence: true
12
+ validates :user_type, inclusion: { in: user_types }
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,3 @@
1
+ module TanoshimuUtils
2
+ VERSION = '1.2.0'
3
+ end
metadata ADDED
@@ -0,0 +1,96 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tanoshimu_utils
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.2.0
5
+ platform: ruby
6
+ authors:
7
+ - Akinyele Cafe-Febrissy
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-01-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.17'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.17'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: activesupport
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Just a couple of utilities shared accross the apps to make development
56
+ much easier.
57
+ email:
58
+ - me@akinyele.ca
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - lib/tanoshimu_utils.rb
64
+ - lib/tanoshimu_utils/concerns.rb
65
+ - lib/tanoshimu_utils/concerns/get_record.rb
66
+ - lib/tanoshimu_utils/concerns/identifiable.rb
67
+ - lib/tanoshimu_utils/concerns/resource_fetch.rb
68
+ - lib/tanoshimu_utils/concerns/respond_to_types.rb
69
+ - lib/tanoshimu_utils/concerns/translatable.rb
70
+ - lib/tanoshimu_utils/validators.rb
71
+ - lib/tanoshimu_utils/validators/presence_one_of.rb
72
+ - lib/tanoshimu_utils/validators/user_like.rb
73
+ - lib/tanoshimu_utils/version.rb
74
+ homepage: https://github.com/thedrummeraki
75
+ licenses: []
76
+ metadata: {}
77
+ post_install_message:
78
+ rdoc_options: []
79
+ require_paths:
80
+ - lib
81
+ required_ruby_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ required_rubygems_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ requirements: []
92
+ rubygems_version: 3.0.3
93
+ signing_key:
94
+ specification_version: 4
95
+ summary: YourAnime.moe utilities
96
+ test_files: []