needs_resources 0.4.0 → 0.5.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -1,5 +1,6 @@
1
1
  *.gem
2
2
  *.rbc
3
+ *.sw[op]
3
4
  .bundle
4
5
  .config
5
6
  coverage
@@ -2,8 +2,12 @@ require 'set'
2
2
 
3
3
  module NeedsResources
4
4
 
5
+ def container
6
+ is_a?(ResourceContainer) ? self : TopLevelResources.instance
7
+ end
8
+
5
9
  def needs_resources(*names)
6
- container = is_a?(ResourceContainer) ? self : TopLevelResources.instance
10
+ container = self.container
7
11
  names.flatten.each do |name|
8
12
  if is_a? Class
9
13
  define_method name, lambda { container[name] }
@@ -32,6 +36,10 @@ module NeedsResources
32
36
  TopLevelResources.instance.each(&block)
33
37
  end
34
38
 
39
+ def self.to_hash
40
+ TopLevelResources.instance.to_hash
41
+ end
42
+
35
43
  end
36
44
 
37
45
  Dir[File.expand_path('../needs_resources/**/*.rb', __FILE__)].sort.each do |f|
@@ -5,6 +5,14 @@ module NeedsResources
5
5
  resources[name.to_sym] or raise MissingResourceError.new(child_resource_name name)
6
6
  end
7
7
 
8
+ def []=(name, hash)
9
+ resources.merge! TopLevelResources.instance.send(:parse_resources, name.to_sym => hash)
10
+ end
11
+
12
+ def has_resource?(name)
13
+ resources.has_key? name.to_sym
14
+ end
15
+
8
16
  def child_resource_name(child_name)
9
17
  if respond_to?(:name)
10
18
  "#{self.name}.#{child_name}"
@@ -21,6 +29,15 @@ module NeedsResources
21
29
  @resources_needed ||= Set.new
22
30
  end
23
31
 
32
+ def resources_hash
33
+ hash = {}
34
+ resources.sort_by { |k, v| "#{v.class}___#{k}" }.each do |k, v|
35
+ next if k.to_s.starts_with? 'default_'
36
+ hash[k.to_s] = v.respond_to?(:to_hash) ? v.to_hash : v
37
+ end
38
+ hash
39
+ end
40
+
24
41
  def missing_resources
25
42
  __missing_resources__(missing = {})
26
43
  missing.map do |k, v|
@@ -8,7 +8,7 @@ module NeedsResources
8
8
  def attr(*names)
9
9
  options = names.last.is_a?(Hash) ? names.pop : {}
10
10
  names.flatten.each do |n|
11
- attributes[n.to_sym] = options
11
+ attributes[n.to_sym] = options.dup
12
12
  attr_reader n
13
13
  end
14
14
  end
@@ -43,7 +43,7 @@ module NeedsResources
43
43
  def initialize(args={})
44
44
  args = args.dup
45
45
  self.class.attributes.each do |name, options|
46
- value = args.delete(name.to_s) || args.delete(name.to_sym) || options[:default]
46
+ value = args.delete(name.to_s) || args.delete(name.to_sym) || options[:default].dup
47
47
  if options[:required] && value.nil?
48
48
  raise RequiredAttributeError.new(self, name)
49
49
  end
@@ -54,5 +54,18 @@ module NeedsResources
54
54
  end
55
55
  end
56
56
 
57
+ def to_hash
58
+ hash = {}
59
+ simple_atts = self.class.attributes.keys - [:name, :resources]
60
+ simple_atts.each do |a|
61
+ v = instance_variable_get("@#{a}")
62
+ hash[a.to_s] = v.respond_to?(:to_hash) ? v.to_hash : v
63
+ end
64
+ if respond_to?(:resources_hash)
65
+ hash.merge! "resources" => resources_hash
66
+ end
67
+ hash
68
+ end
69
+
57
70
  end
58
71
  end
@@ -16,6 +16,8 @@ module NeedsResources
16
16
  resources.each(&block)
17
17
  end
18
18
 
19
+ alias :to_hash :resources_hash
20
+
19
21
  private
20
22
 
21
23
  def defaults
@@ -31,7 +33,7 @@ module NeedsResources
31
33
  end
32
34
 
33
35
  def parse(file)
34
- return {} unless File.exist? file
36
+ return {} unless file && File.exist?(file)
35
37
 
36
38
  yaml = YAML.load_file(file)
37
39
  raise InvalidOrCorruptedResources unless yaml.is_a? Hash
@@ -1,3 +1,3 @@
1
1
  module NeedsResources
2
- VERSION = '0.4.0'
2
+ VERSION = '0.5.1'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: needs_resources
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-11-01 00:00:00.000000000 Z
12
+ date: 2013-11-05 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: Ruby gem to provide lightweight inversion of control type resources for
15
15
  an application