Skip to content

Commit

Permalink
re-add nesting
Browse files Browse the repository at this point in the history
  • Loading branch information
msimonborg committed Jul 20, 2017
1 parent 6f44fec commit 5ad08c6
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 4 deletions.
1 change: 1 addition & 0 deletions lib/lazy_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
require 'lazy_record/collections'
require 'lazy_record/dynamic_modules'
require 'lazy_record/methods'
require 'lazy_record/nesting'
require 'lazy_record/scopes'
require 'lazy_record/validations'
require 'lazy_record/relation'
Expand Down
6 changes: 5 additions & 1 deletion lib/lazy_record/associations.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
# frozen_string_literal: true

require 'lazy_record/nesting'

module LazyRecord
# Set up in-memory one-to-one associations between POROs.
module Associations
include LazyRecord::Nesting

ASSOCIATION_MODULE_NAME = :Associations

def lr_has_one(*args)
Expand Down Expand Up @@ -36,7 +40,7 @@ def define_has_one_associations_to_s
end

def define_association_setter(assoc)
klass = const_get(assoc.to_s.camelize)
klass = lazily_get_class(assoc.to_s.camelize).call
define_method("#{assoc}=") do |val|
return instance_variable_set("@#{assoc}", val) if val.is_a? klass
raise ArgumentError, "Argument must be a #{klass}"
Expand Down
10 changes: 7 additions & 3 deletions lib/lazy_record/collections.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
# frozen_string_literal: true

require 'lazy_record/nesting'

module LazyRecord
# Set up in-memory one-to-many relationships between objects
module Collections
include LazyRecord::Nesting

COLLECTION_MODULE_NAME = :Collections
NESTED_ATTRS_MODULE_NAME = :NestedAttributes

def _define_collection_getter(collection, options)
klass = const_get(options[:class_name])
klass = lazily_get_class(options[:class_name]).call
module_eval <<-RUBY, __FILE__, __LINE__ + 1
def #{collection}
@#{collection} ||= Relation.new(klass: #{klass})
Expand All @@ -16,7 +20,7 @@ def #{collection}
end

def _define_collection_setter(collection, options)
klass = const_get(options[:class_name])
klass = lazily_get_class(options[:class_name]).call
module_eval <<-RUBY, __FILE__, __LINE__ + 1
def #{collection}=(coll)
@#{collection} = Relation.new(klass: #{klass}, collection: coll)
Expand Down Expand Up @@ -76,7 +80,7 @@ def _add_collection_methods(*collections)
end

def define_collection_attributes_setter(collection, options)
class_name = const_get(options.fetch(:class_name))
class_name = lazily_get_class(options.fetch(:class_name)).call
module_eval <<-RUBY, __FILE__, __LINE__ + 1
def #{collection}_attributes=(collection_attributes)
collection_attributes.values.each do |attributes|
Expand Down
20 changes: 20 additions & 0 deletions lib/lazy_record/nesting.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# frozen_string_literal: true

module LazyRecord
# Apply the same namespace nesting as self to another object.
module Nesting
def apply_nesting(class_name)
"#{to_s.split('::')[0..-2].join('::')}::#{class_name}"
end

def lazily_get_class(class_name)
lambda do
begin
const_get(class_name)
rescue NameError
const_get apply_nesting(class_name)
end
end
end
end
end
31 changes: 31 additions & 0 deletions spec/lazy_record/nesting_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# frozen_string_literal: true

describe LazyRecord::Nesting do
module One
class Two
module Three
extend LazyRecord::Nesting
end

module Four; end
end
end

module Five; end

class Top; end

describe 'Three' do
it 'can find One' do
expect(One::Two::Three.lazily_get_class('One').call). to be One
end

it 'can find Four' do
expect(One::Two::Three.lazily_get_class('Four').call). to be One::Two::Four
end

it 'can find Five' do
expect(One::Two::Three.lazily_get_class('Five').call). to be One::Two::Five
end
end
end

0 comments on commit 5ad08c6

Please sign in to comment.