usable 3.1.0 → 3.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 33cffc63217bfad4d6b90e50f2e1d3651e93a64f
4
- data.tar.gz: 313140dd2d024f0d2f7d3cd52c2cb3449d13d979
3
+ metadata.gz: d6635f1e16cd089ac2239170c5904b965b148f0f
4
+ data.tar.gz: a25bed9af17e5dbacefe34eb1005d38b7be5669e
5
5
  SHA512:
6
- metadata.gz: 61d7f8a61d3bbfe2caa0e7577e411596e293333cc2a2419c989c57845c53cf22973cd6476ef28aa58826586df814ce0107150d333c3b5ce59908bfd8e6ac3e92
7
- data.tar.gz: 61143864455490a58c8526e0b5f2216cdfb3dc7c829b8e1b108832c5f5b4fd714868799353f9e339d2ad76fbd9219955d4c996077557e1c1bdddd0cadc043e63
6
+ metadata.gz: b7465c315a1f2f3baca1c9210f8b9880157d5bc74a68f0b8f29aa86dffdbf0c5d16d08951bd68bdb309a9dd1bad5653a7dcaff265c47746c56d3989ab28e5de2
7
+ data.tar.gz: d4ac3cc2a46e42434d5b606471b8349600c68e50a28eec1b0c503eeab1155fed1df46e5ac897d8960ee887eaf3ff150ac9b81df214994f652582907b7bf748b1
data/CHANGELOG.md CHANGED
@@ -1,3 +1,14 @@
1
+ 3.2 (12/1/2016)
2
+ ===============
3
+
4
+ * Improve performance of reads by defining usable attributes as methods
5
+ * `usables._spec` is now `usables.spec`
6
+
7
+ 3.1 (11/30/2016)
8
+ ================
9
+
10
+ * Convert +usables+ to a hash with +to_h+
11
+
1
12
  3.0 (11/4/2016)
2
13
  ===============
3
14
 
data/README.md CHANGED
@@ -75,10 +75,10 @@ module Mixin
75
75
  config do
76
76
  country 'US'
77
77
  state 'Hawaii'
78
- spec :census, {
78
+ census({
79
79
  population: 1_400_00,
80
- daily_visitors: 218_150
81
- }
80
+ daily_visitors: 218_150
81
+ })
82
82
  end
83
83
  end
84
84
 
data/bin/console CHANGED
@@ -22,7 +22,15 @@ end
22
22
 
23
23
  module Mixin
24
24
  extend Usable
25
- usables[:max_versions] = 20
25
+ config.language = :en
26
+ config do
27
+ country 'US'
28
+ state 'Hawaii'
29
+ census({
30
+ population: 1_400_00,
31
+ daily_visitors: 218_150
32
+ })
33
+ end
26
34
 
27
35
  def name
28
36
  "defined by Mixin"
data/lib/usable/config.rb CHANGED
@@ -13,56 +13,62 @@ module Usable
13
13
  @lazy_loads = Set.new
14
14
  end
15
15
 
16
- def each(&block)
17
- @spec.to_h.each(&block)
18
- end
19
-
20
- def spec(key, value = nil)
21
- if value
22
- @spec[key.to_s.tr('=', '')] = value
23
- else
24
- # Handle the case where the value may be defined with a block, in which case it's a method
25
- @spec[key] ||= call_lazy_method(key)
26
- end
27
- end
28
-
29
- def _spec
16
+ def spec
30
17
  @spec
31
18
  end
32
19
 
33
20
  def [](key)
34
- spec key
21
+ @spec[key]
35
22
  end
36
23
 
37
24
  def []=(key, val)
38
- spec key, val
25
+ @spec[key] = val
26
+ end
27
+
28
+ def each(&block)
29
+ @spec.to_h.each(&block)
39
30
  end
40
31
 
41
32
  def to_h
42
- @lazy_loads.each { |key| spec(key) }
43
- _spec.to_h
33
+ @lazy_loads.each { |key| @spec[key] = call_spec_method(key) }
34
+ @spec.to_h
44
35
  end
45
36
 
46
37
  alias to_hash to_h
47
38
 
48
- def call_lazy_method(key)
49
- @lazy_loads.delete key
50
- @spec.public_send key.to_s.tr('=', '')
51
- end
52
-
53
- def method_missing(method_name, *args, &block)
39
+ def method_missing(key, *args, &block)
54
40
  if block
55
- _spec.define_singleton_method(method_name) { yield }
56
- @lazy_loads << method_name
41
+ @lazy_loads << key
42
+ @spec.define_singleton_method(key) { yield }
57
43
  else
58
- spec method_name, *args
44
+ key = key.to_s.tr('=', '')
45
+ if args.empty?
46
+ value = @spec[key] ||= call_spec_method(key)
47
+ define_singleton_method(key) { @spec[key] }
48
+ value
49
+ else
50
+ @spec[key] = args.first
51
+ end
59
52
  end
60
53
  rescue NoMethodError
61
54
  super
62
55
  end
63
56
 
64
57
  def respond_to_missing?(method_name, _private = false)
65
- method_name.to_s.end_with?('=') || _spec.respond_to?(method_name)
58
+ method_name.to_s.end_with?('=') || @spec.respond_to?(method_name)
59
+ end
60
+
61
+ def freeze
62
+ to_h.each { |key, value| define_singleton_method(key) { value } }
63
+ super
64
+ end
65
+
66
+ private
67
+
68
+ # @note Handles the case where the value may be defined with a block, in which case it's a method
69
+ def call_spec_method(key)
70
+ @lazy_loads.delete key
71
+ @spec.public_send key
66
72
  end
67
73
  end
68
74
  end
@@ -4,13 +4,13 @@ module Usable
4
4
  # Set block specs to nil values so it will fallback to calling the underlying singleton method defined by Config#method_missing
5
5
  def +(other)
6
6
  config = clone
7
- specs = other._spec.to_h
8
- specs.each { |key, val| config.spec key, val }
9
- methods = other._spec.singleton_methods - specs.keys
7
+ specs = other.spec.to_h
8
+ specs.each { |key, val| config[key] = val }
9
+ methods = other.spec.singleton_methods - specs.keys
10
10
  methods.each do |name|
11
- config._spec[name] = nil
12
- config._spec.define_singleton_method(name) do
13
- other._spec.public_method(name).call
11
+ config.spec[name] = nil
12
+ config.spec.define_singleton_method(name) do
13
+ other.spec.public_method(name).call
14
14
  end
15
15
  end
16
16
  config
@@ -1,3 +1,3 @@
1
1
  module Usable
2
- VERSION = "3.1.0".freeze
2
+ VERSION = "3.2.0".freeze
3
3
  end
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.1.0
4
+ version: 3.2.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: 2016-11-30 00:00:00.000000000 Z
11
+ date: 2016-12-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler