Skip to content

Commit

Permalink
Cleanup code a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
akodkod committed Jun 13, 2024
1 parent e500c7a commit b680dd7
Show file tree
Hide file tree
Showing 10 changed files with 35 additions and 39 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ group :test do

gem "rubocop", "~> 1.64", require: false
gem "rubocop-performance", "~> 1.21", require: false
gem "rubocop-rake", "~> 0.6.0", require: false
gem "rubocop-rake", "~> 0.6", require: false
gem "rubocop-rspec", "~> 2.31", require: false
end
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ DEPENDENCIES
rspec (~> 3.13)
rubocop (~> 1.64)
rubocop-performance (~> 1.21)
rubocop-rake (~> 0.6.0)
rubocop-rake (~> 0.6)
rubocop-rspec (~> 2.31)
simplecov (~> 0.21)
sqlite3 (~> 1.7)
Expand Down
22 changes: 11 additions & 11 deletions lib/light/services/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
require "light/services/messages"
require "light/services/base_with_context"

require "light/services/settings/step"
require "light/services/settings/output"
require "light/services/settings/argument"
require "light/services/items/step"
require "light/services/items/output"
require "light/services/items/argument"

require "light/services/collection/base"
require "light/services/collection/outputs"
require "light/services/collection/arguments"
require "light/services/collections/base"
require "light/services/collections/outputs"
require "light/services/collections/arguments"

require "light/services/class_based_collection/base"
require "light/services/class_based_collection/mount"
Expand All @@ -23,9 +23,9 @@ class Base
extend ClassBasedCollection::Mount

# Settings
mount_class_based_collection :steps, item_class: Settings::Step, shortcut: :step
mount_class_based_collection :outputs, item_class: Settings::Output, shortcut: :output
mount_class_based_collection :arguments, item_class: Settings::Argument, shortcut: :arg, allow_redefine: true
mount_class_based_collection :steps, item_class: Items::Step, shortcut: :step
mount_class_based_collection :outputs, item_class: Items::Output, shortcut: :output
mount_class_based_collection :arguments, item_class: Items::Argument, shortcut: :arg, allow_redefine: true

# Arguments
arg :verbose, default: false
Expand All @@ -39,8 +39,8 @@ def initialize(args = {}, config = {}, parent_service = nil)
@config = Light::Services.config.merge(self.class.class_config || {}).merge(config)
@parent_service = parent_service

@outputs = Collection::Outputs.new(self)
@arguments = Collection::Arguments.new(self, args)
@outputs = Collections::Outputs.new(self)
@arguments = Collections::Arguments.new(self, args)

@done = false
@launched_steps = []
Expand Down
2 changes: 1 addition & 1 deletion lib/light/services/class_based_collection/base.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

# Create class based collections for storing arguments settings, steps settings and outputs settings
# Create class based collections for storing arguments items, steps items and outputs items
#
# General functionality:
# 1. Collection automatically loads data from parent classes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,29 @@
# Collection to store, merge and validate arguments
module Light
module Services
module Collection
module Collections
class Arguments < Base
def extend_with_context(args)
settings_collection.each do |name, settings|
next if !settings.context || args.key?(name) || !key?(name)
items_collection.each do |name, argument|
next if !argument.context || args.key?(name) || !key?(name)

args[settings.name] = get(name)
args[argument.name] = get(name)
end

args
end

def validate!
settings_collection.each do |name, settings|
next if settings.optional && (!key?(name) || get(name).nil?)
items_collection.each do |name, argument|
next if argument.optional && (!key?(name) || get(name).nil?)

settings.validate_type!(get(name))
argument.validate_type!(get(name))
end
end

private

def settings_collection
def items_collection
@instance.class.arguments
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Collection to store arguments and outputs values
module Light
module Services
module Collection
module Collections
class Base
# Includes
extend Forwardable
Expand Down Expand Up @@ -37,7 +37,7 @@ def []=(key, value)
end

def load_defaults
settings_collection.each do |name, settings|
items_collection.each do |name, settings|
next if !settings.default_exists || key?(name)

if settings.default.is_a?(Proc)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
# Collection to store outputs values
module Light
module Services
module Collection
module Collections
class Outputs < Base
private

def settings_collection
def items_collection
@instance.class.outputs
end
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# frozen_string_literal: true

# This class defines settings for argument
# This class defines items for argument
module Light
module Services
module Settings
module Items
class Argument
# Getters
attr_reader :name, :default_exists, :default, :context, :optional, :arg_types_cache
Expand All @@ -18,8 +18,6 @@ def initialize(name, service_class, opts = {})
@default = opts.delete(:default)
@optional = opts.delete(:optional)

@arg_types_cache = {}

define_methods
end

Expand All @@ -29,7 +27,7 @@ def validate_type!(value)
when :boolean
value.is_a?(TrueClass) || value.is_a?(FalseClass)
when Symbol
arg_type(value) == type
class_to_symbol(value.class) == type
else
value.is_a?(type)
end
Expand All @@ -41,10 +39,8 @@ def validate_type!(value)

private

def arg_type(value)
klass = value.class

@arg_types_cache[klass] ||= klass
def class_to_symbol(klass)
klass
.name
.gsub("::", "/")
.gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# frozen_string_literal: true

# This class defines settings for output
# This class defines items for output
module Light
module Services
module Settings
module Items
class Output
# Getters
attr_reader :name, :default_exists, :default
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# frozen_string_literal: true

# This class defines settings for step
# This class defines items for step
module Light
module Services
module Settings
module Items
class Step
# Getters
attr_reader :name, :always
Expand Down

0 comments on commit b680dd7

Please sign in to comment.