usable 3.8.0 → 3.9.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/usable.rb +7 -3
- data/lib/usable/config.rb +6 -2
- data/lib/usable/persistence.rb +81 -0
- data/lib/usable/struct.rb +12 -0
- data/lib/usable/version.rb +1 -1
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fa4a0c548f6ca951a0db1d9e24da33e518a92533
|
4
|
+
data.tar.gz: 11696a77417b8d5ecdae9aed37eb85193d9242c4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 067d4568b1fe8ab89c69416f9a6b3df5b887d2365350de9a29d3812a8d0069e52b631c8b8d683c6693a7a39a7d87b31178115737861bc60d2fbd02968ac7bfcb
|
7
|
+
data.tar.gz: ab73c2429972136e8eb628e449b7f283bdb01c594eab8b829b34d61b50b45cf50489942e1d3bb779a09b4cacc876b671a7fd821326da469d6ef1c4a009da0b6e
|
data/lib/usable.rb
CHANGED
@@ -90,8 +90,10 @@ module Usable
|
|
90
90
|
|
91
91
|
def define_usable_accessors
|
92
92
|
usables.to_h.keys.each do |key|
|
93
|
-
define_singleton_method(key) { usables
|
94
|
-
define_singleton_method("#{key}=") { |new_val| usables
|
93
|
+
define_singleton_method(key) { usables.send(key) }
|
94
|
+
define_singleton_method("#{key}=") { |new_val| usables.send("#{key}=", new_val) }
|
95
|
+
define_method(key) { usables.send(key) }
|
96
|
+
define_method("#{key}=") { |new_val| usables.send("#{key}=", new_val) }
|
95
97
|
end
|
96
98
|
end
|
97
99
|
|
@@ -120,8 +122,10 @@ module Usable
|
|
120
122
|
# @return self
|
121
123
|
def usable(*args, &block)
|
122
124
|
options = args.last.is_a?(Hash) ? args.pop : {}
|
125
|
+
only = options.delete(:only)
|
126
|
+
extension_method = options.delete(:method)
|
123
127
|
args.each do |mod|
|
124
|
-
ModExtender.new(mod, only:
|
128
|
+
ModExtender.new(mod, only: only, method: extension_method).call self
|
125
129
|
# Define settings on @usables and on the scoped @usables
|
126
130
|
scope = Config.new
|
127
131
|
if mod.name
|
data/lib/usable/config.rb
CHANGED
@@ -51,6 +51,10 @@ module Usable
|
|
51
51
|
self
|
52
52
|
end
|
53
53
|
|
54
|
+
def include?(key)
|
55
|
+
!!@spec[key] || @lazy_loads.include?(key.to_sym)
|
56
|
+
end
|
57
|
+
|
54
58
|
def method_missing(key, *args, &block)
|
55
59
|
if block
|
56
60
|
@lazy_loads << key
|
@@ -63,10 +67,10 @@ module Usable
|
|
63
67
|
# Cleanup, just in case we loaded it another way (e.g. combining with another usable config)
|
64
68
|
@lazy_loads.delete key
|
65
69
|
else
|
66
|
-
@spec[key] = call_spec_method(key)
|
70
|
+
@spec[key] = call_spec_method(key) unless frozen?
|
67
71
|
end
|
68
72
|
# Define method so we don't hit method missing again
|
69
|
-
define_singleton_method(key) { @spec[key] }
|
73
|
+
define_singleton_method(key) { @spec[key] } unless frozen?
|
70
74
|
@spec[key]
|
71
75
|
else
|
72
76
|
@spec[key] = args.first
|
@@ -0,0 +1,81 @@
|
|
1
|
+
require 'fileutils'
|
2
|
+
require 'yaml'
|
3
|
+
|
4
|
+
module Usable
|
5
|
+
module Persistence
|
6
|
+
# = Stores usables in a yaml file
|
7
|
+
|
8
|
+
extend Usable
|
9
|
+
|
10
|
+
config do
|
11
|
+
dir { defined?(Rails) ? Rails.root.join('tmp') : File.expand_path('..', __FILE__) }
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.extended(base)
|
15
|
+
base.extend Usable
|
16
|
+
base.usable self
|
17
|
+
end
|
18
|
+
|
19
|
+
# Automatically copied over by +usable+
|
20
|
+
module InstanceMethods
|
21
|
+
# Accessor to read and write default ActiveRecord objects
|
22
|
+
# When assigning a config, a reference to it is saved to tmp/<mod_name>.yml
|
23
|
+
# and loaded in subsequent console sessions
|
24
|
+
def method_missing(name, *args, &block)
|
25
|
+
if block
|
26
|
+
usables.public_send(name, &block)
|
27
|
+
elsif name.to_s.end_with?('=') && args.length == 1
|
28
|
+
_save name.to_s.tr('=', '').to_sym, args.pop
|
29
|
+
elsif usables[name]
|
30
|
+
usables[name]
|
31
|
+
elsif _config[name]
|
32
|
+
usables[name] = if _config[name].is_a?(Hash)
|
33
|
+
_config[name].fetch(:class).constantize.find(_config[name].fetch(:id))
|
34
|
+
else
|
35
|
+
_config[name]
|
36
|
+
end
|
37
|
+
else
|
38
|
+
usables.public_send(name) || super
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def has?(setting)
|
43
|
+
!!send(setting)
|
44
|
+
rescue NoMethodError => e
|
45
|
+
if e.message.include?("method `#{setting}'")
|
46
|
+
false
|
47
|
+
else
|
48
|
+
raise
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
#
|
53
|
+
# Private
|
54
|
+
#
|
55
|
+
|
56
|
+
def _save(key, val = nil)
|
57
|
+
if val.nil?
|
58
|
+
_config.delete(key)
|
59
|
+
elsif val.respond_to?(:persisted?) && val.persisted?
|
60
|
+
_config[key] = { class: val.class.name, id: val.id }
|
61
|
+
else
|
62
|
+
_config[key] = val
|
63
|
+
end
|
64
|
+
File.open(_config_file, 'wb') { |f| f.puts _config.to_yaml }
|
65
|
+
usables[key] = val
|
66
|
+
end
|
67
|
+
|
68
|
+
def _config_file
|
69
|
+
return @_config_file if @_config_file
|
70
|
+
FileUtils.mkdir_p(usables.dir) unless File.directory?(usables.dir)
|
71
|
+
@_config_file = File.join(usables.dir, "#{self.class.name.downcase.gsub('::', '_')}.yml")
|
72
|
+
FileUtils.touch(@_config_file) unless File.exists?(@_config_file)
|
73
|
+
@_config_file
|
74
|
+
end
|
75
|
+
|
76
|
+
def _config
|
77
|
+
@_config ||= YAML.load_file(_config_file) || {}
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
data/lib/usable/struct.rb
CHANGED
data/lib/usable/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: usable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 3.
|
4
|
+
version: 3.9.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ryan Buckley
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2018-08-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -71,6 +71,7 @@ files:
|
|
71
71
|
- lib/usable/config_register.rb
|
72
72
|
- lib/usable/eager_load.rb
|
73
73
|
- lib/usable/mod_extender.rb
|
74
|
+
- lib/usable/persistence.rb
|
74
75
|
- lib/usable/railtie.rb
|
75
76
|
- lib/usable/struct.rb
|
76
77
|
- lib/usable/version.rb
|
@@ -95,7 +96,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
95
96
|
version: '0'
|
96
97
|
requirements: []
|
97
98
|
rubyforge_project:
|
98
|
-
rubygems_version: 2.6.
|
99
|
+
rubygems_version: 2.6.13
|
99
100
|
signing_key:
|
100
101
|
specification_version: 4
|
101
102
|
summary: Mounts and configures modules
|