tanoshimu_utils 1.1.1 → 1.1.2

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2fa9138c8e28117c49b6e805c37de6639e6f02e7324d7bdc89ecd3fc239dbd30
4
- data.tar.gz: 61a8778cf35cfbaecbaae0ed52113f9603189d19970362629f5e6a706e19afaf
3
+ metadata.gz: 33432d9dccfa1494661fa1a9e1f6ce1975cfeb7c71a7caff9ecd27808e5470bd
4
+ data.tar.gz: 83a9140a6a5ac35e423f0e7669d2c162f6b709ce811bc5bbcc4fd59196dc1fe7
5
5
  SHA512:
6
- metadata.gz: 22c14f7b0dd7a66253e9c4f765e70ed6503ebd6cf27a2cd05d16396e82857af3e835fd6f28fea604b1131456012df403bbee616c66edc1b240dc7c1a0b19b39f
7
- data.tar.gz: 4b850194a097f7dc55f05649ad5569067b42e84389982afb85a542a69ef9844ee3dcb207b303e7f57a798bba048f23e6b5c0d91a3d9058cfe748974688d9d53e
6
+ metadata.gz: b061dae5c2094bead2a467c2f0da0a2ca46496182588c7a468d100f81a336c432c4417366b1659ddcb34bb7bf92906b2e909655b60054a5f2ad4486c9d033259
7
+ data.tar.gz: 9421268d549d6cd574853e8b6af5d7c3cc68a3f24c6a8059de68d8455a15e4d95579c3365e165893338628fd0cadec3a547ea8a48c935e456a9ce22f680516a6
@@ -0,0 +1,2 @@
1
+ require_relative 'concerns/resource_fetch'
2
+
@@ -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
@@ -1,3 +1,3 @@
1
1
  module TanoshimuUtils
2
- VERSION = '1.1.1'
2
+ VERSION = '1.1.2'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tanoshimu_utils
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.1
4
+ version: 1.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Akinyele Cafe-Febrissy
@@ -60,15 +60,10 @@ executables: []
60
60
  extensions: []
61
61
  extra_rdoc_files: []
62
62
  files:
63
- - ".gitignore"
64
- - Gemfile
65
- - README.md
66
- - Rakefile
67
- - bin/console
68
- - bin/setup
69
63
  - lib/tanoshimu_utils.rb
64
+ - lib/tanoshimu_utils/concerns.rb
65
+ - lib/tanoshimu_utils/concerns/resource_fetch.rb
70
66
  - lib/tanoshimu_utils/version.rb
71
- - tanoshimu_utils.gemspec
72
67
  homepage: https://github.com/thedrummeraki
73
68
  licenses: []
74
69
  metadata: {}
data/.gitignore DELETED
@@ -1,8 +0,0 @@
1
- /.bundle/
2
- /.yardoc
3
- /_yardoc/
4
- /coverage/
5
- /doc/
6
- /pkg/
7
- /spec/reports/
8
- /tmp/
data/Gemfile DELETED
@@ -1,6 +0,0 @@
1
- source "https://rubygems.org"
2
-
3
- git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
-
5
- # Specify your gem's dependencies in tanoshimu_utils.gemspec
6
- gemspec
data/README.md DELETED
@@ -1,35 +0,0 @@
1
- # TanoshimuUtils
2
-
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/tanoshimu_utils`. To experiment with that code, run `bin/console` for an interactive prompt.
4
-
5
- TODO: Delete this and the text above, and describe your gem
6
-
7
- ## Installation
8
-
9
- Add this line to your application's Gemfile:
10
-
11
- ```ruby
12
- gem 'tanoshimu_utils'
13
- ```
14
-
15
- And then execute:
16
-
17
- $ bundle
18
-
19
- Or install it yourself as:
20
-
21
- $ gem install tanoshimu_utils
22
-
23
- ## Usage
24
-
25
- TODO: Write usage instructions here
26
-
27
- ## Development
28
-
29
- 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.
30
-
31
- 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).
32
-
33
- ## Contributing
34
-
35
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/tanoshimu_utils.
data/Rakefile DELETED
@@ -1,2 +0,0 @@
1
- require "bundler/gem_tasks"
2
- task :default => :spec
@@ -1,14 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- require "bundler/setup"
4
- require "tanoshimu_utils"
5
-
6
- # You can add fixtures and/or initialization code here to make experimenting
7
- # with your gem easier. You can also use a different console, if you like.
8
-
9
- # (If you use this, don't forget to add pry to your Gemfile!)
10
- # require "pry"
11
- # Pry.start
12
-
13
- require "irb"
14
- IRB.start(__FILE__)
data/bin/setup DELETED
@@ -1,8 +0,0 @@
1
- #!/usr/bin/env bash
2
- set -euo pipefail
3
- IFS=$'\n\t'
4
- set -vx
5
-
6
- bundle install
7
-
8
- # Do any other automated setup that you need to do here
@@ -1,45 +0,0 @@
1
-
2
- lib = File.expand_path("../lib", __FILE__)
3
- $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
- require "tanoshimu_utils/version"
5
-
6
- Gem::Specification.new do |spec|
7
- spec.name = 'tanoshimu_utils'
8
- spec.version = TanoshimuUtils::VERSION
9
- spec.authors = ['Akinyele Cafe-Febrissy']
10
- spec.email = ['me@akinyele.ca']
11
- spec.date = '2020-01-01'
12
-
13
- spec.summary = 'YourAnime.moe utilities'
14
- spec.description = 'Just a couple of utilities shared accross the apps to make development much easier.'
15
- spec.homepage = 'https://github.com/thedrummeraki'
16
-
17
- =begin
18
- # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
19
- # to allow pushing to a single host or delete this section to allow pushing to any host.
20
- if spec.respond_to?(:metadata)
21
- spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
22
-
23
- spec.metadata["homepage_uri"] = spec.homepage
24
- spec.metadata["source_code_uri"] = "TODO: Put your gem's public repo URL here."
25
- spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."
26
- else
27
- raise "RubyGems 2.0 or newer is required to protect against " \
28
- "public gem pushes."
29
- end
30
- =end
31
-
32
- # Specify which files should be added to the gem when it is released.
33
- # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
34
-
35
- spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
36
- `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
37
- end
38
- spec.bindir = "exe"
39
- spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
40
- spec.require_paths = ['lib']
41
-
42
- spec.add_development_dependency "bundler", "~> 1.17"
43
- spec.add_development_dependency "rake", "~> 10.0"
44
- spec.add_dependency 'activesupport'
45
- end