rdm 0.0.5 → 0.0.6

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.
Files changed (35) hide show
  1. checksums.yaml +4 -4
  2. data/example/Rdm.packages +11 -0
  3. data/example/application/web/Package.rb +0 -2
  4. data/example/application/web/{lib → package}/web.rb +0 -0
  5. data/example/application/web/{lib → package}/web/sample_controller.rb +2 -2
  6. data/example/configs/app/default.yml +1 -0
  7. data/example/configs/app/production.yml +1 -0
  8. data/example/configs/database/default.yml +3 -0
  9. data/example/domain/core/Package.rb +0 -2
  10. data/example/domain/core/{lib → package}/core.rb +0 -0
  11. data/example/domain/core/{lib → package}/core/sample_service.rb +2 -2
  12. data/example/infrastructure/repository/Package.rb +1 -2
  13. data/example/infrastructure/repository/{lib → package}/repository.rb +0 -0
  14. data/example/infrastructure/repository/package/repository/sample_repository.rb +6 -0
  15. data/example/server/package/server.rb +0 -0
  16. data/example/server/server.rb +1 -2
  17. data/lib/rdm.rb +20 -0
  18. data/lib/rdm/config.rb +3 -0
  19. data/lib/rdm/config_manager.rb +61 -0
  20. data/lib/rdm/config_scope.rb +17 -0
  21. data/lib/rdm/package.rb +15 -4
  22. data/lib/rdm/package_importer.rb +36 -20
  23. data/lib/rdm/settings.rb +62 -0
  24. data/lib/rdm/source.rb +48 -0
  25. data/lib/rdm/source_installer.rb +1 -1
  26. data/lib/rdm/source_parser.rb +74 -12
  27. data/lib/rdm/version.rb +1 -1
  28. data/spec/fixtures/SampleSource.rb +8 -0
  29. data/spec/rdm/config_manager_spec.rb +66 -0
  30. data/spec/rdm/package_importer_spec.rb +13 -6
  31. data/spec/rdm/settings_spec.rb +45 -0
  32. data/spec/rdm/source_parser_spec.rb +21 -3
  33. data/spec/spec_helper.rb +4 -0
  34. metadata +24 -9
  35. data/example/infrastructure/repository/lib/repository/sample_repository.rb +0 -5
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ef31c5b118ac1b2f38c40dd347c8a4e7b057e92e
4
- data.tar.gz: 4a81781639d702320b0358d01166d89acb055504
3
+ metadata.gz: a229aa8792154b88ef3f94d0bddb7df1dedd04ad
4
+ data.tar.gz: e3f92eb13c9e7f06e239b8aeb0781e8cea1e5aa3
5
5
  SHA512:
6
- metadata.gz: 93e19e0a7e7b9e90095ec994f349192a05a7ccf749c1856ec1c084da437ec463aa326387b83a07408b0c224a72061a0610a36bcf5ef3c52ffd7011d3c7b1df6a
7
- data.tar.gz: 267f57df8ed739c5fb6888a365ed731d0205d4b7f0a3a82d5c2af9143fcc45d3945f2eec059011cbc7eb8c145e4efef0310cc1be7a1ab1d7d8b9dbb4e4922e95
6
+ metadata.gz: 7db4647a3b21e7db60bc3728f5f086879319b4a9cfa9cc793f3846484fab6f0e279458b0a791ca284cee49ae9c69f6ace5b3f699a8a718eacb9cf64a2811cfa8
7
+ data.tar.gz: 4879fbf0609b160ae4e309266e6b2eb953b2b42b1999b293206b4e0c5c5badb89205c52c3067bf7bab514526f7716e4e55c994345c14ea19465aa2f392bb02b4
data/example/Rdm.packages CHANGED
@@ -1,3 +1,14 @@
1
+ setup do
2
+ role "production"
3
+ configs_dir "configs"
4
+ config_path ":configs_dir/:config_name/default.yml"
5
+ role_config_path ":configs_dir/:config_name/:role.yml"
6
+ package_subdir_name "package"
7
+ end
8
+
9
+ config :database
10
+ config :app
11
+
1
12
  package "server"
2
13
  package "application/web"
3
14
  package "domain/core"
@@ -4,7 +4,5 @@ package do
4
4
  end
5
5
 
6
6
  dependency do
7
- require_file "lib/web.rb"
8
-
9
7
  import "core"
10
8
  end
File without changes
@@ -1,8 +1,8 @@
1
1
  class Web::SampleController
2
2
  #inject :sample_service
3
3
 
4
- def create_something
5
- sample_service.create_something
4
+ def perform
5
+ sample_service.perform
6
6
  end
7
7
 
8
8
  def sample_service
@@ -0,0 +1 @@
1
+ app_name: "Sample app"
@@ -0,0 +1 @@
1
+ app_name: "Sample app (Production)"
@@ -0,0 +1,3 @@
1
+ database:
2
+ username: "root"
3
+ password: "root"
@@ -4,7 +4,5 @@ package do
4
4
  end
5
5
 
6
6
  dependency do
7
- require_file "lib/core.rb"
8
-
9
7
  import "repository"
10
8
  end
File without changes
@@ -1,8 +1,8 @@
1
1
  class Core::SampleService
2
2
  #inject :sample_repository
3
3
 
4
- def create_something
5
- sample_repository.create_something
4
+ def perform
5
+ sample_repository.perform
6
6
  end
7
7
 
8
8
  def sample_repository
@@ -4,7 +4,6 @@ package do
4
4
  end
5
5
 
6
6
  dependency do
7
- require_file "lib/repository.rb"
8
-
9
7
  require "sequel"
8
+ import_config :app
10
9
  end
@@ -0,0 +1,6 @@
1
+ class Repository::SampleRepository
2
+ def perform
3
+ config = Rdm.config
4
+ puts "WORKS! (#{config.app_name})"
5
+ end
6
+ end
File without changes
@@ -4,5 +4,4 @@ require "rdm"
4
4
 
5
5
  Rdm.init(File.expand_path("../", __FILE__))
6
6
 
7
- Web::SampleController.new.create_something
8
- #BikeManager::Container[:sample_controller].create_something
7
+ Web::SampleController.new.perform
data/lib/rdm.rb CHANGED
@@ -3,16 +3,36 @@ module Rdm
3
3
  PACKAGE_FILENAME = "Package.rb"
4
4
  PACKAGE_LOCK_FILENAME = "#{PACKAGE_FILENAME}.lock"
5
5
 
6
+ require "rdm/settings"
7
+ require "rdm/source"
6
8
  require "rdm/source_parser"
7
9
  require "rdm/source_installer"
8
10
  require "rdm/package"
9
11
  require "rdm/package_parser"
10
12
  require "rdm/package_importer"
13
+ require "rdm/config"
14
+ require "rdm/config_scope"
15
+ require "rdm/config_manager"
11
16
 
12
17
  class << self
13
18
  # Initialize current package using Package.rb
14
19
  def init(package_path, group = nil)
15
20
  Rdm::PackageImporter.import_file(package_path, group: group)
16
21
  end
22
+
23
+ # Rdm's internal settings
24
+ def settings
25
+ @settings ||= Rdm::Settings.new
26
+ end
27
+
28
+ # Rdm's managed configuration
29
+ def config
30
+ @config ||= Rdm::ConfigManager.new
31
+ end
32
+
33
+ # Setup Rdm's internal settings
34
+ def setup(&block)
35
+ settings.instance_eval(&block) if block_given?
36
+ end
17
37
  end
18
38
  end
data/lib/rdm/config.rb ADDED
@@ -0,0 +1,3 @@
1
+ class Rdm::Config
2
+ attr_accessor :name, :default_path, :role_path
3
+ end
@@ -0,0 +1,61 @@
1
+ require 'yaml'
2
+ class Rdm::ConfigManager
3
+
4
+ # Update configuration based on given config file
5
+ # @param config [Rdm::Config] Config entity
6
+ # @return root scope [Rdm::ConfigScope] Updated root scope
7
+ def load_config(config, source:)
8
+ if config.default_path
9
+ full_default_path = File.join(source.root_path, config.default_path)
10
+ update_using_file(full_default_path, raise_if_missing: true)
11
+ end
12
+ if config.role_path
13
+ full_role_path = File.join(source.root_path, config.role_path)
14
+ update_using_file(full_role_path, raise_if_missing: false)
15
+ end
16
+ root_scope
17
+ end
18
+
19
+ # Update configuration using given path to config file
20
+ # @param config [Hash<String: AnyValue>] Hash with configuration
21
+ # @return root scope [Rdm::ConfigScope] Updated root scope
22
+ def update_using_file(path, raise_if_missing: true)
23
+ if File.exists?(path)
24
+ hash = YAML.load_file(path)
25
+ update_using_hash(hash)
26
+ elsif raise_if_missing
27
+ raise "Config file is not found at path #{path}"
28
+ end
29
+ root_scope
30
+ end
31
+
32
+ # Update configuration based on given hash
33
+ # @param config [Hash<String: AnyValue>] Hash with configuration
34
+ # @return root scope [Rdm::ConfigScope] Updated root scope
35
+ def update_using_hash(hash, scope: nil)
36
+ scope ||= root_scope
37
+
38
+ hash.each do |key, value|
39
+ if value.is_a?(Hash)
40
+ # Try using existing scope
41
+ child_scope = scope.read_attribute(key)
42
+ if !child_scope || !child_scope.is_a?(Rdm::ConfigScope)
43
+ child_scope = Rdm::ConfigScope.new
44
+ end
45
+ update_using_hash(value, scope: child_scope)
46
+ scope.write_attribute(key, child_scope)
47
+ else
48
+ scope.write_attribute(key, value)
49
+ end
50
+ end
51
+ end
52
+
53
+ def method_missing(method_name, *args)
54
+ root_scope.send(method_name, *args)
55
+ end
56
+
57
+ private
58
+ def root_scope
59
+ @root_scope ||= Rdm::ConfigScope.new
60
+ end
61
+ end
@@ -0,0 +1,17 @@
1
+ class Rdm::ConfigScope
2
+ def initialize(attributes = {})
3
+ @attributes = {}
4
+ end
5
+
6
+ def read_attribute(key)
7
+ @attributes[key.to_s]
8
+ end
9
+
10
+ def write_attribute(key, value)
11
+ @attributes[key.to_s] = value
12
+ end
13
+
14
+ def method_missing(method_name, *args)
15
+ read_attribute(method_name)
16
+ end
17
+ end
data/lib/rdm/package.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  class Rdm::Package
2
2
  DEFAULT_GROUP = "_default_"
3
3
 
4
- attr_accessor :metadata, :local_dependencies, :external_dependencies, :file_dependencies, :path
4
+ attr_accessor :metadata, :local_dependencies, :external_dependencies, :file_dependencies, :config_dependencies, :path
5
5
 
6
6
  def local_dependencies(group = nil)
7
7
  fetch_dependencies(@local_dependencies || {}, group)
@@ -15,6 +15,10 @@ class Rdm::Package
15
15
  fetch_dependencies(@file_dependencies || {}, group)
16
16
  end
17
17
 
18
+ def config_dependencies(group = nil)
19
+ fetch_dependencies(@config_dependencies || {}, group)
20
+ end
21
+
18
22
  # Import local dependency, e.g another package
19
23
  def import(dependency)
20
24
  @local_dependencies ||= {}
@@ -36,6 +40,13 @@ class Rdm::Package
36
40
  @file_dependencies[current_group] << file
37
41
  end
38
42
 
43
+ # Import config dependency
44
+ def import_config(dependency)
45
+ @config_dependencies ||= {}
46
+ @config_dependencies[current_group] ||= []
47
+ @config_dependencies[current_group] << dependency
48
+ end
49
+
39
50
  def package
40
51
  yield
41
52
  end
@@ -70,11 +81,11 @@ class Rdm::Package
70
81
  end
71
82
 
72
83
  def exec_metadata(key, value)
73
- if value
84
+ if value.nil?
85
+ @metadata[key]
86
+ else
74
87
  @metadata ||= {}
75
88
  @metadata[key] = value
76
- else
77
- @metadata[key]
78
89
  end
79
90
  end
80
91
  end
@@ -1,6 +1,9 @@
1
1
  class Rdm::PackageImporter
2
2
  class << self
3
3
  # Initialize current package using Package.rb
4
+ # @param package_path [String] Package.rb file path
5
+ # @param group [Optional<String>] Dependency group
6
+ # @return [Rdm::Package] Current package
4
7
  def import_file(package_path, group: nil)
5
8
  if File.directory?(package_path)
6
9
  package_path = File.join(package_path, Rdm::PACKAGE_LOCK_FILENAME)
@@ -8,14 +11,16 @@ class Rdm::PackageImporter
8
11
  package_content = File.open(package_path).read
9
12
  package = package_parser.parse(package_content)
10
13
 
11
- packages = fetch_packages(package.source)
12
- import_package(package.name, packages: packages, group: group.to_s)
14
+ source = read_and_init_source(package.source)
15
+ import_package(package.name, source: source, group: group.to_s)
16
+
17
+ package
13
18
  end
14
19
 
15
20
  # Import package and initialize module
16
- def import_package(package_name, packages:, imported_packages: [], group: nil)
21
+ def import_package(package_name, source:, imported_packages: [], imported_configs: [], group: nil)
17
22
  return if imported_packages.include?(package_name.to_s)
18
- package = packages[package_name.to_s]
23
+ package = source.packages[package_name.to_s]
19
24
 
20
25
  if package == nil
21
26
  raise "Can't find package with name: #{package_name.to_s}"
@@ -26,14 +31,25 @@ class Rdm::PackageImporter
26
31
 
27
32
  # also import local dependencies
28
33
  package.local_dependencies(group).each do |dependency|
29
- import_package(dependency, packages: packages, imported_packages: imported_packages)
34
+ import_package(dependency, source: source, imported_packages: imported_packages)
35
+ end
36
+
37
+ # also import config dependencies
38
+ package.config_dependencies(group).each do |dependency|
39
+ unless imported_configs.include?(dependency)
40
+ import_config(dependency, source: source)
41
+ end
42
+ imported_configs << dependency
30
43
  end
31
44
 
32
45
  # only after importing dependencies - require package itself
33
46
  begin
34
47
  require package_name
35
48
  rescue LoadError
36
- raise "Can't require package #{package_name}, please create file #{package_name}/lib/#{package_name}.rb"
49
+ if Rdm.settings.raises_missing_package_file_exception
50
+ package_require_path = "#{package_name}/#{package_subdir_name}/#{package_name}.rb"
51
+ raise "Can't require package #{package_name}, please create file #{package_require_path}"
52
+ end
37
53
  end
38
54
 
39
55
  imported_packages
@@ -48,8 +64,12 @@ class Rdm::PackageImporter
48
64
  Rdm::PackageParser
49
65
  end
50
66
 
67
+ def package_subdir_name
68
+ Rdm.settings.package_subdir_name.to_s
69
+ end
70
+
51
71
  def init_package(package, group:)
52
- $LOAD_PATH.push(File.join(package.path, "lib"))
72
+ $LOAD_PATH.push(File.join(package.path, package_subdir_name))
53
73
 
54
74
  package.external_dependencies(group).each do |dependency|
55
75
  require dependency
@@ -59,20 +79,16 @@ class Rdm::PackageImporter
59
79
  end
60
80
  end
61
81
 
62
- def fetch_packages(source_path)
63
- root_path = File.dirname(source_path)
64
- source_content = File.open(source_path).read
65
-
66
- packages = {}
67
- source_parser.parse(source_content).each do |package|
68
- package_path = File.join(root_path, package)
69
- package_rb_path = File.join(package_path, Rdm::PACKAGE_FILENAME)
70
- package_content = File.open(package_rb_path).read
71
- package = package_parser.parse(package_content)
72
- package.path = package_path
73
- packages[package.name] = package
82
+ def import_config(config_name, source: source)
83
+ config = source.configs[config_name.to_s]
84
+ if config == nil
85
+ raise "Can't find config with name: #{config_name.to_s}"
74
86
  end
75
- packages
87
+ Rdm.config.load_config(config, source: source)
88
+ end
89
+
90
+ def read_and_init_source(source_path)
91
+ source_parser.read_and_init_source(source_path)
76
92
  end
77
93
  end
78
94
  end
@@ -0,0 +1,62 @@
1
+ class Rdm::Settings
2
+ SETTING_KEYS = [
3
+ :role, :raises_missing_package_file_exception, :package_subdir_name,
4
+ :configs_dir, :config_path, :role_config_path
5
+ ]
6
+
7
+ SETTING_VARIABLES = [:role, :configs_dir, :config_path, :role_config_path]
8
+
9
+ # Default settings
10
+ def initialize
11
+ raises_missing_package_file_exception(true)
12
+ package_subdir_name("package")
13
+ configs_dir('configs')
14
+ end
15
+
16
+ SETTING_KEYS.each do |key|
17
+ define_method(key) do |value = nil|
18
+ fetch_setting key, value
19
+ end
20
+ end
21
+
22
+ def fetch_setting(key, value = nil)
23
+ if value.nil?
24
+ read_setting(key)
25
+ else
26
+ write_setting(key, value)
27
+ end
28
+ end
29
+
30
+ def read_setting(key, vars: {})
31
+ value = @settings[key.to_s]
32
+ if value.is_a?(Proc)
33
+ value.call
34
+ elsif value.is_a?(String)
35
+ replace_variables(value, except: key, additional_vars: vars)
36
+ else
37
+ value
38
+ end
39
+ end
40
+
41
+ private
42
+ def write_setting(key, value)
43
+ @settings ||= {}
44
+ @settings[key.to_s] = value
45
+ end
46
+
47
+ def replace_variables(value, except: nil, additional_vars: {})
48
+ variables_keys = SETTING_VARIABLES - [except.to_sym]
49
+ new_value = value
50
+ additional_vars.each do |key, variable|
51
+ if new_value.match(":#{key.to_s}")
52
+ new_value = new_value.gsub(":#{key.to_s}", variable)
53
+ end
54
+ end
55
+ variables_keys.each do |key|
56
+ if new_value.match(":#{key}")
57
+ new_value = new_value.gsub(":#{key}", read_setting(key))
58
+ end
59
+ end
60
+ new_value
61
+ end
62
+ end
data/lib/rdm/source.rb ADDED
@@ -0,0 +1,48 @@
1
+ class Rdm::Source
2
+ attr_reader :setup_block, :config_names, :package_paths, :root_path
3
+
4
+ def initialize(root_path:)
5
+ @root_path = root_path
6
+ end
7
+
8
+ # Set setup block for source
9
+ # @param block [Proc] Setup block
10
+ def setup(&block)
11
+ @setup_block = block
12
+ end
13
+
14
+ # Add config to list of known configs
15
+ # @param config_name [String] Config name
16
+ def config(config_name)
17
+ @config_names ||= []
18
+ @config_names << config_name.to_s
19
+ end
20
+
21
+ # Add package to list of known packages
22
+ # @param package_path [String] Package path
23
+ def package(package_path)
24
+ @package_paths ||= []
25
+ @package_paths << package_path
26
+ end
27
+
28
+ # Init source by adding read packages and configs
29
+ # @param value [Hash<String: Rdm::Package>] Hash of packages by it's name
30
+ # @param value [Hash<String: Rdm::Config>] Hash of configs by it's name
31
+ # @return [Hash<String: Rdm::Package>] Hash of packages by it's name
32
+ def init_with(packages:, configs:)
33
+ @packages = packages
34
+ @configs = configs
35
+ end
36
+
37
+ # Read initialized packages
38
+ # @return [Hash<String: Rdm::Package>] Hash of packages by it's name
39
+ def packages
40
+ @packages || {}
41
+ end
42
+
43
+ # Read initialized configs
44
+ # @return [Hash<String: Rdm::Config>] Hash of configs by it's name
45
+ def configs
46
+ @configs || {}
47
+ end
48
+ end
@@ -3,7 +3,7 @@ class Rdm::SourceInstaller
3
3
  # Install source by locking all it's specs
4
4
  def install(source_path)
5
5
  source_content = File.open(source_path).read
6
- source_parser.parse(source_content).each do |package_path|
6
+ source_parser.parse(source_content).package_paths.each do |package_path|
7
7
  full_path = File.join(File.dirname(source_path), package_path, Rdm::PACKAGE_FILENAME)
8
8
  lock(full_path, source_path: source_path, package_path: package_path)
9
9
  end
@@ -1,19 +1,81 @@
1
1
  class Rdm::SourceParser
2
- class Scope
3
- attr_accessor :package_paths
4
-
5
- def package(package_path)
6
- self.package_paths ||= []
7
- self.package_paths << package_path
8
- end
2
+ class SourceValidationError < StandardError
9
3
  end
10
4
 
11
5
  class << self
12
- # Parse source file and return package paths
13
- def parse(source_content)
14
- scope = Scope.new
15
- scope.instance_eval(source_content)
16
- scope.package_paths
6
+
7
+ # Read source file, parse and init it's packages and configs
8
+ # @param source_path [String] Source file path
9
+ # @return [Rdm::Source] Source
10
+ def read_and_init_source(source_path)
11
+ root_path = File.dirname(source_path)
12
+ source_content = File.open(source_path).read
13
+ source = parse(source_content, root_path: root_path)
14
+
15
+ # Setup Rdm
16
+ if block = source.setup_block
17
+ Rdm.setup(&block)
18
+ end
19
+ validate_rdm_settings!
20
+
21
+ # Init and set packages
22
+ packages = {}
23
+ source.package_paths.each do |package_path|
24
+ package_full_path = File.join(root_path, package_path)
25
+ package_rb_path = File.join(package_full_path, Rdm::PACKAGE_FILENAME)
26
+ package_content = File.open(package_rb_path).read
27
+ package = package_parser.parse(package_content)
28
+ package.path = package_full_path
29
+ packages[package.name] = package
30
+ end
31
+
32
+ # Init and set configs
33
+ configs = {}
34
+ source.config_names.each do |config_name|
35
+ default_path = settings.read_setting(:config_path, vars: {config_name: config_name})
36
+ role_path = settings.read_setting(:role_config_path, vars: {config_name: config_name})
37
+
38
+ config = Rdm::Config.new
39
+ config.default_path = default_path
40
+ config.role_path = role_path
41
+ config.name = config_name
42
+ configs[config_name] = config
43
+ end
44
+
45
+ source.init_with(packages: packages, configs: configs)
46
+ source
47
+ end
48
+
49
+ # Parse source file and return Source object
50
+ # @param source_content [String] Source file content
51
+ # @return [Rdm::Source] Source
52
+ def parse(source_content, root_path: nil)
53
+ source = Rdm::Source.new(root_path: root_path)
54
+ source.instance_eval(source_content)
55
+ source
17
56
  end
57
+
58
+ private
59
+ # Make sure that all required settings are in place
60
+ def validate_rdm_settings!
61
+ if Rdm.settings.read_setting(:role).nil?
62
+ raise SourceValidationError.new(
63
+ "Please set `role` value in Rdm.packages. E.g. \r\n setup do\r\n role { ENV['RAILS_ENV'] }\r\n end"
64
+ )
65
+ end
66
+ if Rdm.settings.read_setting(:config_path).nil?
67
+ raise SourceValidationError.new(
68
+ "Please set `config_path` value in Rdm.packages. E.g. \r\n setup do\r\n config_path :configs_dir/:config_name/default.yml'\r\n end"
69
+ )
70
+ end
71
+ end
72
+
73
+ def settings
74
+ Rdm.settings
75
+ end
76
+
77
+ def package_parser
78
+ Rdm::PackageParser
79
+ end
18
80
  end
19
81
  end
data/lib/rdm/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Rdm
2
- VERSION = "0.0.5"
2
+ VERSION = "0.0.6"
3
3
  end
@@ -0,0 +1,8 @@
1
+ setup do
2
+ role "example"
3
+ end
4
+
5
+ config :database
6
+
7
+ package "application/web"
8
+ package "domain/core"
@@ -0,0 +1,66 @@
1
+ require 'spec_helper'
2
+
3
+ describe Rdm::ConfigManager do
4
+ subject { Rdm::ConfigManager.new }
5
+
6
+ describe "#update_using_hash" do
7
+ before :each do
8
+ subject.update_using_hash(
9
+ database: {
10
+ username: "foo",
11
+ password: "bar"
12
+ },
13
+ lib_name: "rdm",
14
+ version: 1,
15
+ published: true,
16
+ draft: false,
17
+ features: ["dependency_manager", "config_manager"]
18
+ )
19
+ end
20
+
21
+ it "returns given value for string" do
22
+ expect(subject.lib_name).to eq("rdm")
23
+ end
24
+
25
+ it "returns given value for int" do
26
+ expect(subject.version).to eq(1)
27
+ end
28
+
29
+ it "returns given value for true bool" do
30
+ expect(subject.published).to eq(true)
31
+ end
32
+
33
+ it "returns given value for false bool" do
34
+ expect(subject.draft).to eq(false)
35
+ end
36
+
37
+ it "returns given value for array" do
38
+ expect(subject.features).to eq(["dependency_manager", "config_manager"])
39
+ end
40
+
41
+ it "creates another child scope for nested hash" do
42
+ expect(subject.database).to be_instance_of(Rdm::ConfigScope)
43
+ expect(subject.database.username).to eq("foo")
44
+ expect(subject.database.password).to eq("bar")
45
+ end
46
+
47
+ context "when already has config" do
48
+ before :each do
49
+ subject.update_using_hash(
50
+ database: {
51
+ username: "new_username",
52
+ password: "new_password"
53
+ }
54
+ )
55
+ end
56
+
57
+ it "keeps old configs" do
58
+ expect(subject.lib_name).to eq('rdm')
59
+ end
60
+
61
+ it "rewrites new configs" do
62
+ expect(subject.database.username).to eq('new_username')
63
+ end
64
+ end
65
+ end
66
+ end
@@ -4,12 +4,19 @@ describe Rdm::PackageImporter do
4
4
  def build_package(name, dependencies: [])
5
5
  package = Rdm::Package.new
6
6
  package.name(name)
7
+ package.path = name
7
8
  dependencies.each do |dependency|
8
9
  package.import(dependency)
9
10
  end
10
11
  package
11
12
  end
12
13
 
14
+ def build_source(packages:)
15
+ source = Rdm::Source.new(root_path: nil)
16
+ source.init_with(packages: packages, configs: {})
17
+ source
18
+ end
19
+
13
20
  describe "#import_package" do
14
21
  subject { Rdm::PackageImporter }
15
22
 
@@ -17,9 +24,9 @@ describe Rdm::PackageImporter do
17
24
  it "imports all depended global packages" do
18
25
  web_pack = build_package("web", dependencies: ["core"])
19
26
  core_pack = build_package("core")
20
- packages = {"web" => web_pack, "core" => core_pack}
27
+ source = build_source(packages: {"web" => web_pack, "core" => core_pack})
21
28
 
22
- imported = subject.import_package("web", packages: packages)
29
+ imported = subject.import_package("web", source: source)
23
30
  expect(imported).to include("core")
24
31
  end
25
32
  end
@@ -33,9 +40,9 @@ describe Rdm::PackageImporter do
33
40
  core_pack = build_package("core")
34
41
  factory_pack = build_package("factory")
35
42
 
36
- packages = {"web" => web_pack, "core" => core_pack, "factory" => factory_pack}
43
+ source = build_source(packages: {"web" => web_pack, "core" => core_pack, "factory" => factory_pack})
37
44
 
38
- imported = subject.import_package("web", packages: packages, group: "test")
45
+ imported = subject.import_package("web", source: source, group: "test")
39
46
  expect(imported).to include("factory")
40
47
  end
41
48
 
@@ -47,9 +54,9 @@ describe Rdm::PackageImporter do
47
54
  core_pack = build_package("core")
48
55
  factory_pack = build_package("factory")
49
56
 
50
- packages = {"web" => web_pack, "core" => core_pack, "factory" => factory_pack}
57
+ source = build_source(packages: {"web" => web_pack, "core" => core_pack, "factory" => factory_pack})
51
58
 
52
- imported = subject.import_package("web", packages: packages)
59
+ imported = subject.import_package("web", source: source)
53
60
  expect(imported).to_not include("factory")
54
61
  end
55
62
  end
@@ -0,0 +1,45 @@
1
+ require 'spec_helper'
2
+
3
+ describe Rdm::Settings do
4
+ subject { Rdm::Settings.new }
5
+
6
+ describe "#fetch_setting" do
7
+ it "writes setting if value provided" do
8
+ expect(subject.send(:read_setting, :test)).to be_nil
9
+ subject.fetch_setting(:test, "test value")
10
+ expect(subject.send(:read_setting, :test)).to eq("test value")
11
+ end
12
+
13
+ it "reads setting if no value provided" do
14
+ subject.send(:write_setting, :test, "test value")
15
+ expect(subject.fetch_setting(:test)).to eq("test value")
16
+ end
17
+ end
18
+
19
+ describe "#read_setting" do
20
+ it "returns setting if it's boolean" do
21
+ subject.send(:write_setting, :bool_value, true)
22
+ expect(subject.read_setting(:bool_value)).to eq(true)
23
+ end
24
+
25
+ it "returns result of proc if it's a proc" do
26
+ proc_value = proc {
27
+ "proc value"
28
+ }
29
+ subject.send(:write_setting, :proc_value, proc_value)
30
+ expect(subject.read_setting(:proc_value)).to eq("proc value")
31
+ end
32
+
33
+ it "replaces variables if it's a string" do
34
+ subject.role("test")
35
+ subject.fetch_setting("some-path", "/path/:role.yml")
36
+ expect(subject.read_setting("some-path")).to eq("/path/test.yml")
37
+ end
38
+
39
+ it "replaces additional variables if it's a string" do
40
+ subject.role("foo")
41
+ subject.fetch_setting("some-path", "/path/:role/:myvar.yml")
42
+ expect(subject.read_setting("some-path", vars: {myvar: "bar"})).to eq("/path/foo/bar.yml")
43
+ end
44
+ end
45
+ end
@@ -4,15 +4,33 @@ describe Rdm::SourceParser do
4
4
  describe "#parse" do
5
5
  subject { Rdm::SourceParser }
6
6
 
7
+ let(:fixtures_path) {
8
+ File.join(File.expand_path("../../", __FILE__), 'fixtures')
9
+ }
10
+
7
11
  let(:source_content) {
8
- %Q{package "application/web"\r\npackage "domain/core"}
12
+ File.read(File.join(fixtures_path, "SampleSource.rb"))
9
13
  }
10
14
 
11
- it "returns all packages paths" do
12
- paths = subject.parse(source_content)
15
+ before :each do
16
+ @source = subject.parse(source_content)
17
+ end
18
+
19
+ it "returns Source object" do
20
+ expect(@source.is_a?(Rdm::Source)).to be_truthy
21
+ end
22
+
23
+ it "parses all packages paths" do
24
+ paths = @source.package_paths
13
25
  expect(paths.count).to be(2)
14
26
  expect(paths).to include("application/web")
15
27
  expect(paths).to include("domain/core")
16
28
  end
29
+
30
+ it "parses all config names" do
31
+ names = @source.config_names
32
+ expect(names.count).to be(1)
33
+ expect(names).to include("database")
34
+ end
17
35
  end
18
36
  end
data/spec/spec_helper.rb CHANGED
@@ -1,5 +1,9 @@
1
1
  require 'rdm'
2
2
 
3
+ Rdm.setup do
4
+ raises_missing_package_file_exception(false)
5
+ end
6
+
3
7
  RSpec.configure do |config|
4
8
  config.color = true
5
9
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rdm
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Droid Labs
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-03-03 00:00:00.000000000 Z
11
+ date: 2016-03-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -83,26 +83,38 @@ files:
83
83
  - example/Rdm.packages
84
84
  - example/Readme.md
85
85
  - example/application/web/Package.rb
86
- - example/application/web/lib/web.rb
87
- - example/application/web/lib/web/sample_controller.rb
86
+ - example/application/web/package/web.rb
87
+ - example/application/web/package/web/sample_controller.rb
88
+ - example/configs/app/default.yml
89
+ - example/configs/app/production.yml
90
+ - example/configs/database/default.yml
88
91
  - example/domain/core/Package.rb
89
- - example/domain/core/lib/core.rb
90
- - example/domain/core/lib/core/sample_service.rb
92
+ - example/domain/core/package/core.rb
93
+ - example/domain/core/package/core/sample_service.rb
91
94
  - example/infrastructure/repository/Package.rb
92
- - example/infrastructure/repository/lib/repository.rb
93
- - example/infrastructure/repository/lib/repository/sample_repository.rb
95
+ - example/infrastructure/repository/package/repository.rb
96
+ - example/infrastructure/repository/package/repository/sample_repository.rb
94
97
  - example/server/Package.rb
98
+ - example/server/package/server.rb
95
99
  - example/server/server.rb
96
100
  - lib/rdm.rb
101
+ - lib/rdm/config.rb
102
+ - lib/rdm/config_manager.rb
103
+ - lib/rdm/config_scope.rb
97
104
  - lib/rdm/package.rb
98
105
  - lib/rdm/package_importer.rb
99
106
  - lib/rdm/package_parser.rb
107
+ - lib/rdm/settings.rb
108
+ - lib/rdm/source.rb
100
109
  - lib/rdm/source_installer.rb
101
110
  - lib/rdm/source_parser.rb
102
111
  - lib/rdm/version.rb
103
112
  - rdm.gemspec
113
+ - spec/fixtures/SampleSource.rb
114
+ - spec/rdm/config_manager_spec.rb
104
115
  - spec/rdm/package_importer_spec.rb
105
116
  - spec/rdm/package_parser_spec.rb
117
+ - spec/rdm/settings_spec.rb
106
118
  - spec/rdm/source_parser_spec.rb
107
119
  - spec/spec_helper.rb
108
120
  homepage:
@@ -124,12 +136,15 @@ required_rubygems_version: !ruby/object:Gem::Requirement
124
136
  version: '0'
125
137
  requirements: []
126
138
  rubyforge_project:
127
- rubygems_version: 2.2.2
139
+ rubygems_version: 2.4.3
128
140
  signing_key:
129
141
  specification_version: 4
130
142
  summary: Ruby Dependency Manager
131
143
  test_files:
144
+ - spec/fixtures/SampleSource.rb
145
+ - spec/rdm/config_manager_spec.rb
132
146
  - spec/rdm/package_importer_spec.rb
133
147
  - spec/rdm/package_parser_spec.rb
148
+ - spec/rdm/settings_spec.rb
134
149
  - spec/rdm/source_parser_spec.rb
135
150
  - spec/spec_helper.rb
@@ -1,5 +0,0 @@
1
- class Repository::SampleRepository
2
- def create_something
3
- puts "WORKS!"
4
- end
5
- end