Skip to content

Commit

Permalink
Evaluate form class lazily
Browse files Browse the repository at this point in the history
  • Loading branch information
pyromaniac committed Dec 20, 2023
1 parent 230b50a commit 45ba326
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 16 deletions.
12 changes: 3 additions & 9 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2023-03-25 07:19:50 UTC using RuboCop version 1.44.1.
# on 2023-12-20 10:30:23 UTC using RuboCop version 1.57.2.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
Expand All @@ -9,26 +9,20 @@
# Offense count: 3
# Configuration parameters: EnforcedStyle, AllowedGems, Include.
# SupportedStyles: Gemfile, gems.rb, gemspec
# AllowedGems: bundler
# Include: **/*.gemspec, **/Gemfile, **/gems.rb
Gemspec/DevelopmentDependencies:
Exclude:
- 'operations.gemspec'

# Offense count: 5
# Configuration parameters: AllowedMethods, AllowedPatterns, IgnoredMethods, CountRepeatedAttributes.
# Configuration parameters: AllowedMethods, AllowedPatterns, CountRepeatedAttributes.
Metrics/AbcSize:
Max: 28

# Offense count: 2
# Configuration parameters: CountComments, CountAsOne.
Metrics/ClassLength:
Max: 160

# Offense count: 7
# Configuration parameters: CountComments, CountAsOne, ExcludedMethods, AllowedMethods, AllowedPatterns, IgnoredMethods.
Metrics/MethodLength:
Max: 17
Max: 161

# Offense count: 1
# Configuration parameters: CountKeywordArgs, MaxOptionalParameters.
Expand Down
7 changes: 4 additions & 3 deletions lib/operations/command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ def sentry_context
Operations::Types::String
), default: proc { {} }
option :form_base, Operations::Types::Class, default: proc { ::Operations::Form }
option :form_class, Operations::Types::Class, default: proc { build_form }
option :form_class, Operations::Types::Class.optional, default: proc {}
option :form_hydrator, Operations::Types.Interface(:call), default: proc { FORM_HYDRATOR }
option :configuration, Operations::Configuration, default: proc { Operations.default_config }

Expand Down Expand Up @@ -203,6 +203,7 @@ def initialize(

preconditions.push(precondition) if precondition.present?
super(operation, preconditions: preconditions, on_success: after, **options)
@form_class ||= build_form_class
end

# Instantiates a new command with the given fields updated.
Expand Down Expand Up @@ -264,7 +265,7 @@ def possible(params = EMPTY_HASH, **context)
# These 3 methods added for convenience. They return boolean result
# instead of Operations::Result. True on success and false on failure.
%i[callable allowed possible].each do |method|
define_method "#{method}?" do |**kwargs|
define_method :"#{method}?" do |**kwargs|
public_send(method, **kwargs).success?
end
end
Expand Down Expand Up @@ -395,7 +396,7 @@ def unwrap_monad(result)
end
end

def build_form
def build_form_class
::Operations::Form::Builder
.new(base_class: form_base)
.build(
Expand Down
4 changes: 2 additions & 2 deletions lib/operations/convenience.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def contract(prefix = nil, from: OperationContract, &block)
contract = Class.new(from)
contract.config.messages.namespace = name.underscore
contract.class_eval(&block)
const_set("#{prefix.to_s.camelize}Contract", contract)
const_set(:"#{prefix.to_s.camelize}Contract", contract)
end

%w[policy precondition callback].each do |kind|
Expand All @@ -96,7 +96,7 @@ def contract(prefix = nil, from: OperationContract, &block)

klass.define_method(:call, &block) if block

const_set("#{prefix.to_s.camelize}#{kind.camelize}", klass)
const_set(:"#{prefix.to_s.camelize}#{kind.camelize}", klass)
end
end
end
2 changes: 1 addition & 1 deletion lib/operations/form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ def cached_attribute(name)
name = name.to_sym
return unless self.class.attributes.key?(name)

nested_name = "#{name}_attributes".to_sym
nested_name = :"#{name}_attributes"
value = data.key?(nested_name) ? data[nested_name] : data[name]

(@attributes_cache ||= {})[name] ||= yield(value, self.class.attributes[name])
Expand Down
2 changes: 1 addition & 1 deletion lib/operations/form/builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def traverse_hash(form, hash_key, model_map, path)
nested_attributes_suffix,
nested_attributes_collection
)
form.define_method "#{hash_key.name}=", proc { |attributes| attributes } if nested_attributes_suffix
form.define_method :"#{hash_key.name}=", proc { |attributes| attributes } if nested_attributes_suffix

key_path = path + [name]
nested_form = traverse(members, form, name.underscore.camelize, model_map, key_path)
Expand Down

0 comments on commit 45ba326

Please sign in to comment.