needs_resources 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,8 +1,8 @@
1
1
  module NeedsResources
2
2
  class RequiredAttributeError < StandardError
3
3
 
4
- def initialize(clazz, name)
5
- super "Missing resource in config file: #{clazz} (in #{name})"
4
+ def initialize(obj, name)
5
+ super "Missing resource in config file: #{name} (in #{obj.respond_to?(:name) ? obj.name : obj.to_s})"
6
6
  end
7
7
 
8
8
  end
@@ -2,7 +2,7 @@ module NeedsResources
2
2
  module ResourceContainer
3
3
 
4
4
  def [](name)
5
- resources[name] or raise MissingResourceError.new(child_resource_name name)
5
+ resources[name.to_sym] or raise MissingResourceError.new(child_resource_name name)
6
6
  end
7
7
 
8
8
  def child_resource_name(child_name)
@@ -14,7 +14,7 @@ module NeedsResources
14
14
  end
15
15
 
16
16
  def resources
17
- @resources ||= {}.with_indifferent_access
17
+ @resources ||= {}
18
18
  end
19
19
 
20
20
  def resources_needed
@@ -39,4 +39,4 @@ module NeedsResources
39
39
  end
40
40
 
41
41
  end
42
- end
42
+ end
@@ -2,35 +2,51 @@ module NeedsResources
2
2
  module ResourceType
3
3
 
4
4
  def self.included(base)
5
- children[base.name.demodulize.underscore] = base
5
+ children[underscore(base.name)] = base
6
+
6
7
  base.instance_eval do
7
8
  def attr(*names)
8
9
  options = names.last.is_a?(Hash) ? names.pop : {}
9
10
  names.flatten.each do |n|
10
- attributes[n] = options.with_indifferent_access
11
+ attributes[n.to_sym] = options
11
12
  attr_reader n
12
13
  end
13
14
  end
14
15
 
16
+ def provide_top_level_instance(name=nil, args={})
17
+ name ||= ResourceType.underscore(self.name)
18
+ TopLevelResources.instance.add_default(name, args)
19
+ end
20
+
15
21
  def attributes
16
- @attributes ||= {}.with_indifferent_access
22
+ @attributes ||= {}
17
23
  end
18
24
  end
25
+
19
26
  base.attr :name, :required => true
20
27
  end
21
28
 
29
+ def self.underscore(name)
30
+ name = name.to_s.dup
31
+ name.gsub!(/^.*::/, '')
32
+ name.gsub!(/([A-Z]+)([A-Z][a-z])/,'\1_\2')
33
+ name.gsub!(/([a-z\d])([A-Z])/,'\1_\2')
34
+ name.tr!("-", "_")
35
+ name.downcase!
36
+ name
37
+ end
38
+
22
39
  def self.children
23
40
  @children ||= {}
24
41
  end
25
42
 
26
43
  def initialize(args={})
27
- args = args.with_indifferent_access
44
+ args = args.dup
28
45
  self.class.attributes.each do |name, options|
29
- value = args.delete(name) || options[:default]
46
+ value = args.delete(name.to_s) || args.delete(name.to_sym) || options[:default]
30
47
  if options[:required] && value.nil?
31
48
  raise RequiredAttributeError.new(self, name)
32
49
  end
33
- value = value.with_indifferent_access if value.is_a? Hash
34
50
  instance_variable_set "@#{name}", value
35
51
  end
36
52
  args.each do |k, v|
@@ -39,4 +55,4 @@ module NeedsResources
39
55
  end
40
56
 
41
57
  end
42
- end
58
+ end
@@ -7,10 +7,18 @@ module NeedsResources
7
7
  include Singleton
8
8
  include ResourceContainer
9
9
 
10
+ def add_default(name, args={})
11
+ defaults[name.to_sym] = args
12
+ end
13
+
10
14
  private
11
15
 
16
+ def defaults
17
+ @defaults ||= {}
18
+ end
19
+
12
20
  def resources
13
- @resources ||= parse(resource_config_file)
21
+ @resources ||= parse_resources(defaults).merge parse(resource_config_file)
14
22
  end
15
23
 
16
24
  def resource_config_file
@@ -18,21 +26,26 @@ module NeedsResources
18
26
  end
19
27
 
20
28
  def parse(file)
21
- resources = {}.with_indifferent_access
22
- return resources unless File.exist? file
29
+ return {} unless File.exist? file
23
30
 
24
31
  yaml = YAML.load_file(file)
25
32
  raise InvalidOrCorruptedResources unless yaml.is_a? Hash
26
33
 
27
- yaml.each do |name, value|
34
+ parse_resources(yaml)
35
+ end
36
+
37
+ def parse_resources(hash)
38
+ resources = {}
39
+
40
+ hash.each do |name, value|
28
41
  prefix, type = parse_resource_name(name)
29
- resources[name] = parse_resource_value(name, type, value)
42
+ resources[name.to_sym] = parse_resource_value(name, type, value)
30
43
 
31
44
  # Make sure both default_xyz and xyz are aliases
32
45
  if prefix == 'default_'
33
- resources[type] ||= name.to_sym
46
+ resources[type.to_sym] ||= name.to_sym
34
47
  elsif prefix.nil?
35
- resources[type] ||= "default_#{type}".to_sym
48
+ resources[type.to_sym] ||= "default_#{type}".to_sym
36
49
  end
37
50
  end
38
51
 
@@ -58,9 +71,13 @@ module NeedsResources
58
71
 
59
72
  def parse_resource_value(name, type, value)
60
73
  case value
61
- when Symbol
74
+ when Symbol, String, Fixnum
62
75
  value
63
76
  when Hash
77
+ if value.has_key?("resources")
78
+ raise InvalidResourceValueError.new(name, value) unless value.is_a? Hash
79
+ value["resources"] = parse_resources value["resources"]
80
+ end
64
81
  ResourceType.children[type].new(value.merge :name => name)
65
82
  else
66
83
  raise InvalidResourceValueError.new(name, value)
@@ -82,4 +99,4 @@ module NeedsResources
82
99
  end
83
100
 
84
101
  end
85
- end
102
+ end
@@ -1,3 +1,3 @@
1
1
  module NeedsResources
2
- VERSION = '0.1.0'
2
+ VERSION = '0.2.0'
3
3
  end
@@ -1,6 +1,4 @@
1
1
  require 'set'
2
- require 'active_support'
3
- require 'active_support/core_ext'
4
2
 
5
3
  module NeedsResources
6
4
 
@@ -18,7 +16,7 @@ module NeedsResources
18
16
  alias :needs_resource :needs_resources
19
17
 
20
18
  def self.ensure_resources
21
- if (missing = missing_resources).present?
19
+ if (missing = missing_resources).any?
22
20
  # TODO: maybe prompt user if missing?
23
21
  raise MissingResourceError.new(*missing)
24
22
  end
@@ -31,5 +29,5 @@ module NeedsResources
31
29
  end
32
30
 
33
31
  Dir[File.expand_path('../needs_resources/**/*.rb', __FILE__)].sort.each do |f|
34
- require f
35
- end
32
+ load f
33
+ end
@@ -14,6 +14,4 @@ Gem::Specification.new do |gem|
14
14
  gem.name = "needs_resources"
15
15
  gem.require_paths = ["lib"]
16
16
  gem.version = NeedsResources::VERSION
17
-
18
- gem.add_dependency('active_support', '> 2.0')
19
17
  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.1.0
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,24 +9,8 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-10-28 00:00:00.000000000 Z
13
- dependencies:
14
- - !ruby/object:Gem::Dependency
15
- name: active_support
16
- requirement: !ruby/object:Gem::Requirement
17
- none: false
18
- requirements:
19
- - - ! '>'
20
- - !ruby/object:Gem::Version
21
- version: '2.0'
22
- type: :runtime
23
- prerelease: false
24
- version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
- requirements:
27
- - - ! '>'
28
- - !ruby/object:Gem::Version
29
- version: '2.0'
12
+ date: 2013-10-29 00:00:00.000000000 Z
13
+ dependencies: []
30
14
  description: Ruby gem to provide lightweight inversion of control type resources for
31
15
  an application
32
16
  email: