tanoshimu_utils 1.2.2 → 2.0.0

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: e12e51d76c696e382e40ef43dc59ad97eb5479441925a8d8f638cf771344708e
4
+ data.tar.gz: 885046472e52bcb839e2e77e76d623815a3de6b4b144ef9bff72c17ac3e5fa53
5
5
  SHA512:
6
- metadata.gz: 05df987581a2076776af93969b5575ad5c379b895fbae54a5189b26944ea5c0ddc7f68ed5f4d20f15334533a494ab367143197da7020bd7d75ab21310623d62f
7
- data.tar.gz: 4b4c0787f53d8577611e256d0caff4f37d1d802a220a6703dee1f7b51a1993c04f73b46413f55f29dfd292a5ae2c56d21a42f5e6048a18aaab6da1ba97a85f4b
6
+ metadata.gz: 41ea0070f7a5e72c96b9e42ca810d65b5feeb1efb874081428287919e673e192c655278df64a7b299d710446c0a8b4aa09d93c448ffb8000ef8c342542325b73
7
+ data.tar.gz: 4dc444e942cc31c5837d366b8b16436e46f5fec5390cdfecab297ae788d37fdd44231856e3c5be7822e35117c8053f80b13d1a73e3f2af399686393ef5da089a
data/MIT-LICENSE ADDED
@@ -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.
data/README.md ADDED
@@ -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.
data/Rakefile ADDED
@@ -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
@@ -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,7 +23,7 @@ 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
-
26
+
27
27
  if Config.uses_disk_storage?
28
28
  Rails.application.routes.url_helpers.rails_blob_url(attachment, only_path: true)
29
29
  else
@@ -34,6 +34,7 @@ module TanoshimuUtils
34
34
  (fetch!(resource_name, default_url)&.attached?).present?
35
35
  end
36
36
  send(:define_method, "generate_#{resource_name}_url!") do
37
+ return nil unless persisted?
37
38
  return true if send("#{resource_url}?") && !force
38
39
 
39
40
  new_url = send("#{resource_url}!")
@@ -41,87 +42,9 @@ module TanoshimuUtils
41
42
  end
42
43
  end
43
44
  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
-
45
+
123
46
  private
124
-
47
+
125
48
  def resource_for(resource_name)
126
49
  method(resource_name).call
127
50
  end
@@ -161,6 +84,6 @@ module TanoshimuUtils
161
84
  rescue NameError
162
85
  raise ResourceFetch::ResourceNotAttachable.new(resource_name)#, resource unless resource.respond_to?(:attached?)
163
86
  end
164
- end
87
+ end
165
88
  end
166
89
  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.0'
3
3
  end
@@ -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'
@@ -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.0
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-01-10 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: 6.0.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: 6.0.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,6 +60,9 @@ 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
@@ -67,12 +70,15 @@ files:
67
70
  - lib/tanoshimu_utils/concerns/resource_fetch.rb
68
71
  - lib/tanoshimu_utils/concerns/respond_to_types.rb
69
72
  - lib/tanoshimu_utils/concerns/translatable.rb
73
+ - lib/tanoshimu_utils/railtie.rb
70
74
  - lib/tanoshimu_utils/validators.rb
71
75
  - lib/tanoshimu_utils/validators/presence_one_of.rb
72
76
  - lib/tanoshimu_utils/validators/user_like.rb
73
77
  - lib/tanoshimu_utils/version.rb
74
- homepage: https://github.com/thedrummeraki/tanoshimu/tree/heroku/tanoshimu_utils
75
- licenses: []
78
+ - lib/tasks/tanoshimu_utils_tasks.rake
79
+ homepage: https://github.com/thedrummeraki/tanoshimu_utils
80
+ licenses:
81
+ - MIT
76
82
  metadata: {}
77
83
  post_install_message:
78
84
  rdoc_options: []