tanoshimu_utils 1.2.2 → 2.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0d89a89ee2a88ac4a6c6ba96c98a4b158d29fbd71ec306c1b2934d09e7cdf495
4
- data.tar.gz: d2eea7a91e7fefc67beb18d1947e9a213700d6ac168c9bcdff25596860d97646
3
+ metadata.gz: ceb19bd51af668df54c36d3a3816736fa30429d0ef102a2a2988e2fb69f683b5
4
+ data.tar.gz: bd6076c5b8bd97f004ff970f2943cb3160a7ad7f2687554f5b01f05ac209838f
5
5
  SHA512:
6
- metadata.gz: 05df987581a2076776af93969b5575ad5c379b895fbae54a5189b26944ea5c0ddc7f68ed5f4d20f15334533a494ab367143197da7020bd7d75ab21310623d62f
7
- data.tar.gz: 4b4c0787f53d8577611e256d0caff4f37d1d802a220a6703dee1f7b51a1993c04f73b46413f55f29dfd292a5ae2c56d21a42f5e6048a18aaab6da1ba97a85f4b
6
+ metadata.gz: 610bf66297ca0e5e916d1257e8c28b9ab9b4fa8969e23c0f641ac43bbd0ca7eda0c9a62edc9253b99656c1c656b3c85c6d7ab0f30fb6eb74bb3789765cc31c74
7
+ data.tar.gz: 899a1446770351dfa4a53ba89e18c0b315f8672efb497072ee866e02d056efd394f3ae42122063ccb7f609f462ef7245bf1ddd8362123aee5df6b88e29ccc6f6
@@ -0,0 +1,20 @@
1
+ Copyright 2020 Akinyele Cafe-Febrissy
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,48 @@
1
+ # YourAnime.moe utilities
2
+
3
+ Welcome to `tanoshimu_utils`! This gem is what I like to call "just a couple of utilities shared accross the apps to make development much easier."
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'tanoshimu_utils'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install tanoshimu_utils
20
+
21
+ ## Usage
22
+
23
+ ### Concerns
24
+
25
+ | `include` | Description | Example |
26
+ | ------------------------------------------ | ----------- | ------- |
27
+ | `TanoshimuUtils::Concerns::GetRecord` | Add `.record` to return a model from the table `.used_by_model` with primary key `.model_id`. | `Model.first.record` |
28
+ | `TanoshimuUtils::Concerns::Identifiable` | Fills up the `.identification` field with a unique token. | Use with classes like `Admin`, `User`, `Staff`, etc... (anyone that can log on to the app) |
29
+ | `TanoshimuUtils::Concerns::ResourceFetch` | For an ActiveStorage attachment `res`, adds `res_url` (get the url), `res_url!` (fetch from AWS), `res?` (make sure it is existant and attached) | Use classes that have attachments (like images). If `user` has `avatar`, then `user.avatar?`, `user.avatar_url`, `user.avatar_url!` are valid calls. |
30
+ | `TanoshimuUtils::Concerns::RespondToTypes` | Adds `respond_to_types` class method to add `field?` for a field `field`. | `respond_to_types [:admin, :regular, :guest]` is a valid class for a `User` class. |
31
+ | `TanoshimuUtils::Concerns::Translatable` | Depending on the current locale, adds a `translates` class method to create a field that returns the value | `translates :value, through: [:en, :fr], default: :fr` creates a method `.value` that returns the field `.fr` if the locale is French, `.en` if the locale is English. French is returned if another available locale is set. |
32
+
33
+ ### Validators
34
+
35
+ | `include` | Description | Example |
36
+ | ------------------------------------------- | ----------- | ------- |
37
+ | `TanoshimuUtils::Validators::PresenceOneOf` | Adds the class method `validate_presence_one_of` to check if at lease one of the fields are set. | `validate_presence_one_of [:field1, :field2, :field3]` will validate if `:field1` or `field2` or `field3` is set. If none are set, fails. |
38
+ | `TanoshimuUtils::Validators::UserLike` | Adds the class method `validate_like_user` to check if the user-like class (`User`, `Admin`, `Staff`, etc.) has a `name`, a `username` and a `user_type` (the latter must be in the array `user_types`). | `validate_like_user user_types: [:admin, :regular]` only allows user-like models to be either admin or regular. |
39
+
40
+ ## Development
41
+
42
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
43
+
44
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
45
+
46
+ ## Contributing
47
+
48
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/tanoshimu_utils.
@@ -0,0 +1,27 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'TanoshimuUtils'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.md')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+ require 'bundler/gem_tasks'
18
+
19
+ require 'rake/testtask'
20
+
21
+ Rake::TestTask.new(:test) do |t|
22
+ t.libs << 'test'
23
+ t.pattern = 'test/**/*_test.rb'
24
+ t.verbose = false
25
+ end
26
+
27
+ task default: :test
@@ -1,10 +1,15 @@
1
1
  require 'active_support'
2
-
3
2
  require 'tanoshimu_utils/version'
4
- require_relative 'tanoshimu_utils/concerns'
5
- require_relative 'tanoshimu_utils/validators'
6
3
 
7
4
  module TanoshimuUtils
8
- class Error < StandardError; end
9
- # Your code goes here...
5
+ class Error < StandardError
6
+ def initialize(message = nil)
7
+ message ||= 'Unknow error.'
8
+ super("[TanoshimuUtils-#{VERSION}]: #{message}")
9
+ end
10
+ end
10
11
  end
12
+
13
+ require 'tanoshimu_utils/railtie'
14
+ require_relative 'tanoshimu_utils/concerns'
15
+ require_relative 'tanoshimu_utils/validators'
@@ -3,3 +3,4 @@ require_relative 'concerns/identifiable'
3
3
  require_relative 'concerns/resource_fetch'
4
4
  require_relative 'concerns/respond_to_types'
5
5
  require_relative 'concerns/translatable'
6
+ require_relative 'concerns/has_translatable_field'
@@ -0,0 +1,36 @@
1
+ module TanoshimuUtils
2
+ module Concerns
3
+ module HasTranslatableField
4
+ extend ActiveSupport::Concern
5
+
6
+ class_methods do
7
+ def has_translatable_field(name, foreign_key: :model_id)
8
+ p "USING METHOD"
9
+ set_translatable_field = :"#{name}="
10
+ get_translatable_field = :"#{name}"
11
+ record_field = :"#{name}_record"
12
+ record_class = Object.const_get(name.capitalize)
13
+
14
+ send(:define_method, get_translatable_field) do
15
+ instance_variable_get("@#{name}") ||
16
+ instance_variable_set("@#{name}", send(record_field)&.value)
17
+ end
18
+
19
+ send(:define_method, set_translatable_field) do |new_record_field|
20
+ return unless new_record_field.kind_of?(record_class)
21
+
22
+ new_record_field.used_by_model = record_class.table_name
23
+ new_record_field.send(:"#{foreign_key}=", self.id)
24
+ self.send(:"#{record_field}=", new_record_field)
25
+ end
26
+
27
+ has_one(
28
+ record_field,
29
+ class_name: record_class.to_s,
30
+ foreign_key: foreign_key,
31
+ )
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -2,13 +2,13 @@ module TanoshimuUtils
2
2
  module Concerns
3
3
  module ResourceFetch
4
4
  extend ActiveSupport::Concern
5
-
6
- class ResourceNotAttachable < StandardError
5
+
6
+ class ResourceNotAttachable < TanoshimuUtils::Error
7
7
  def initialize(resource_name)
8
8
  super("Resource #{resource_name} not attachable.")
9
9
  end
10
10
  end
11
-
11
+
12
12
  class_methods do
13
13
  def has_resource(resource_name, default_url: '/', expiry: 1.day)
14
14
  resource_url = :"#{resource_name}_url"
@@ -23,8 +23,8 @@ module TanoshimuUtils
23
23
  send(:define_method, "#{resource_url}!") do
24
24
  attachment = resource_for(resource_name).attachment
25
25
  return default_url if attachment.nil?
26
-
27
- if Config.uses_disk_storage?
26
+
27
+ if Rails.configuration.uses_disk_storage
28
28
  Rails.application.routes.url_helpers.rails_blob_url(attachment, only_path: true)
29
29
  else
30
30
  attachment.service_url(expires_in: expiry)
@@ -33,7 +33,9 @@ module TanoshimuUtils
33
33
  send(:define_method, "#{resource_name}?") do
34
34
  (fetch!(resource_name, default_url)&.attached?).present?
35
35
  end
36
- send(:define_method, "generate_#{resource_name}_url!") do
36
+ send(:define_method, "generate_#{resource_name}_url!") do |**options|
37
+ force = options[:force] || false
38
+ return nil unless persisted?
37
39
  return true if send("#{resource_url}?") && !force
38
40
 
39
41
  new_url = send("#{resource_url}!")
@@ -41,87 +43,9 @@ module TanoshimuUtils
41
43
  end
42
44
  end
43
45
  end
44
-
45
- private
46
-
47
- def resource_for(resource_name)
48
- method(resource_name).call
49
- end
50
-
51
- def resource_url_for(resource_name, default_url: '/', expiry: 1.day)
52
- ensure_attachable_resource!(resource_name)
53
- resource_url = :"#{resource_name}_url"
54
- if try(:"db_#{resource_url}?")
55
- self[resource_url] || default_url
56
- else
57
- resource = resource_for(resource_name)
58
- if resource.attached?
59
- resource.service_url(expires_in: expiry)
60
- else
61
- default_url
62
- end
63
- end
64
- end
65
-
66
- def fetch!(resource_name, default_url)
67
- ensure_attachable_resource!(resource_name)
68
- resource = resource_for(resource_name)
69
- #return resource if resource.attached?
70
-
71
- #resource.attach(io: File.open("./public/#{default_url}"), filename: "episode-#{id}")
72
-
73
- rescue ResourceFetch::ResourceNotAttachable, Errno::ENOENT
74
- nil
75
- end
76
-
77
- def ensure_fetchable_resource!(resource_name)
78
- raise NoMethodError, "Don't know how to fetch #{resource_name}" unless respond_to?(resource_name)
79
- end
80
-
81
- def ensure_attachable_resource!(resource_name)
82
- resource_for(resource_name)
83
- rescue NameError
84
- raise ResourceFetch::ResourceNotAttachable.new(resource_name)#, resource unless resource.respond_to?(:attached?)
85
- end
86
- end
87
- module ResourceFetch
88
- extend ActiveSupport::Concern
89
-
90
- class ResourceNotAttachable < StandardError
91
- def initialize(resource_name)
92
- super("Resource #{resource_name} not attachable.")
93
- end
94
- end
95
-
96
- class_methods do
97
- def has_resource(resource_name, default_url: '/', expiry: 1.day)
98
- resource_url = :"#{resource_name}_url"
99
- db_resource_url = :"db_#{resource_url}?"
100
- send(:define_method, db_resource_url) do
101
- @db_resource_url ||= self.class.column_names.include?(resource_url.to_s)
102
- end
103
- send(:define_method, resource_url) do
104
- ensure_fetchable_resource!(resource_name)
105
- resource_url_for(resource_name, default_url: default_url, expiry: expiry)
106
- end
107
- send(:define_method, "#{resource_url}!") do
108
- attachment = resource_for(resource_name).attachment
109
- return default_url if attachment.nil?
110
-
111
- if Config.uses_disk_storage?
112
- Rails.application.routes.url_helpers.rails_blob_url(attachment, only_path: true)
113
- else
114
- attachment.service_url(expires_in: expiry)
115
- end
116
- end
117
- send(:define_method, "#{resource_name}?") do
118
- (fetch!(resource_name, default_url)&.attached?).present?
119
- end
120
- end
121
- end
122
-
46
+
123
47
  private
124
-
48
+
125
49
  def resource_for(resource_name)
126
50
  method(resource_name).call
127
51
  end
@@ -161,6 +85,6 @@ module TanoshimuUtils
161
85
  rescue NameError
162
86
  raise ResourceFetch::ResourceNotAttachable.new(resource_name)#, resource unless resource.respond_to?(:attached?)
163
87
  end
164
- end
88
+ end
165
89
  end
166
- end
90
+ end
@@ -2,39 +2,39 @@ module TanoshimuUtils
2
2
  module Concerns
3
3
  module Translatable
4
4
  extend ActiveSupport::Concern
5
-
5
+
6
6
  def metaclass
7
7
  class << self
8
8
  self
9
9
  end
10
10
  end
11
-
11
+
12
12
  class_methods do
13
13
  def translates(field, through: [], default:)
14
14
  raise 'Field must be present' unless field.present?
15
15
  raise 'Through must be present' unless through.present?
16
-
16
+
17
17
  if column_names.include?(field.to_s)
18
18
  raise 'Field must not be defined on the model'
19
19
  end
20
-
21
- through = [through] unless through.kind_of?(Array)
20
+
21
+ through = [through] unless through.is_a?(Array)
22
22
  default = default.to_sym
23
-
23
+
24
24
  unless I18n.available_locales.include?(default)
25
- raise "#{default} is not available on your system as a locale"
25
+ warn "#{default} is not available on your system as a locale"
26
26
  end
27
-
27
+
28
28
  send(:define_method, field.to_sym) do
29
29
  through.each do |possible_locale|
30
30
  possible_locale = possible_locale.to_sym
31
-
31
+
32
32
  unless I18n.available_locales.include?(possible_locale)
33
33
  next
34
34
  end
35
35
  end
36
-
37
36
  return nil unless through.include?(I18n.locale)
37
+
38
38
  value = send(I18n.locale)
39
39
  value = send(default) if value.nil? && default
40
40
  value
@@ -43,4 +43,4 @@ module TanoshimuUtils
43
43
  end
44
44
  end
45
45
  end
46
- end
46
+ end
@@ -0,0 +1,4 @@
1
+ module TanoshimuUtils
2
+ class Railtie < ::Rails::Railtie
3
+ end
4
+ end
@@ -1,3 +1,3 @@
1
1
  module TanoshimuUtils
2
- VERSION = '1.2.2'
2
+ VERSION = '2.0.2'
3
3
  end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :tanoshimu_utils do
3
+ # # Task goes here
4
+ # end
metadata CHANGED
@@ -1,51 +1,51 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tanoshimu_utils
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.2
4
+ version: 2.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Akinyele Cafe-Febrissy
8
8
  autorequire:
9
- bindir: exe
9
+ bindir: bin
10
10
  cert_chain: []
11
- date: 2020-01-01 00:00:00.000000000 Z
11
+ date: 2020-08-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: bundler
14
+ name: rails
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '1.17'
20
- type: :development
19
+ version: '0'
20
+ type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - "~>"
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: '1.17'
26
+ version: '0'
27
27
  - !ruby/object:Gem::Dependency
28
- name: rake
28
+ name: sqlite3
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - "~>"
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
- version: '10.0'
33
+ version: '0'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - "~>"
38
+ - - ">="
39
39
  - !ruby/object:Gem::Version
40
- version: '10.0'
40
+ version: '0'
41
41
  - !ruby/object:Gem::Dependency
42
- name: activesupport
42
+ name: pry
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
45
  - - ">="
46
46
  - !ruby/object:Gem::Version
47
47
  version: '0'
48
- type: :runtime
48
+ type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
@@ -60,19 +60,26 @@ executables: []
60
60
  extensions: []
61
61
  extra_rdoc_files: []
62
62
  files:
63
+ - MIT-LICENSE
64
+ - README.md
65
+ - Rakefile
63
66
  - lib/tanoshimu_utils.rb
64
67
  - lib/tanoshimu_utils/concerns.rb
65
68
  - lib/tanoshimu_utils/concerns/get_record.rb
69
+ - lib/tanoshimu_utils/concerns/has_translatable_field.rb
66
70
  - lib/tanoshimu_utils/concerns/identifiable.rb
67
71
  - lib/tanoshimu_utils/concerns/resource_fetch.rb
68
72
  - lib/tanoshimu_utils/concerns/respond_to_types.rb
69
73
  - lib/tanoshimu_utils/concerns/translatable.rb
74
+ - lib/tanoshimu_utils/railtie.rb
70
75
  - lib/tanoshimu_utils/validators.rb
71
76
  - lib/tanoshimu_utils/validators/presence_one_of.rb
72
77
  - lib/tanoshimu_utils/validators/user_like.rb
73
78
  - lib/tanoshimu_utils/version.rb
74
- homepage: https://github.com/thedrummeraki/tanoshimu/tree/heroku/tanoshimu_utils
75
- licenses: []
79
+ - lib/tasks/tanoshimu_utils_tasks.rake
80
+ homepage: https://github.com/thedrummeraki/tanoshimu_utils
81
+ licenses:
82
+ - MIT
76
83
  metadata: {}
77
84
  post_install_message:
78
85
  rdoc_options: []
@@ -89,7 +96,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
89
96
  - !ruby/object:Gem::Version
90
97
  version: '0'
91
98
  requirements: []
92
- rubygems_version: 3.0.3
99
+ rubygems_version: 3.1.2
93
100
  signing_key:
94
101
  specification_version: 4
95
102
  summary: YourAnime.moe utilities