From e3e53843298b45fda45c4a13928c5687c74a9ba9 Mon Sep 17 00:00:00 2001 From: Gabriel Naiman Date: Fri, 16 Sep 2016 18:03:32 -0300 Subject: [PATCH] Constantize using Consty gem --- lib/rasti/db.rb | 1 + lib/rasti/db/collection.rb | 2 +- lib/rasti/db/model.rb | 4 ++++ lib/rasti/db/relations.rb | 2 +- rasti-db.gemspec | 1 + 5 files changed, 8 insertions(+), 2 deletions(-) diff --git a/lib/rasti/db.rb b/lib/rasti/db.rb index 433dee4..fae88fc 100644 --- a/lib/rasti/db.rb +++ b/lib/rasti/db.rb @@ -1,4 +1,5 @@ require 'sequel' +require 'consty' require_relative 'db/version' require_relative 'db/collection' diff --git a/lib/rasti/db/collection.rb b/lib/rasti/db/collection.rb index de55841..53e02a0 100644 --- a/lib/rasti/db/collection.rb +++ b/lib/rasti/db/collection.rb @@ -15,7 +15,7 @@ def primary_key end def model - @model ||= constantize(singularize(name)) + @model ||= Consty.get(demodulize(singularize(name)), self) end def relations diff --git a/lib/rasti/db/model.rb b/lib/rasti/db/model.rb index 83c9c44..670f25b 100644 --- a/lib/rasti/db/model.rb +++ b/lib/rasti/db/model.rb @@ -19,6 +19,10 @@ class << self def [](*attributes) Class.new(self) do attributes.each { |name| attribute name } + + def self.inherited(subclass) + subclass.instance_variable_set :@attributes, attributes + end end end diff --git a/lib/rasti/db/relations.rb b/lib/rasti/db/relations.rb index 650f8e8..6334fe0 100644 --- a/lib/rasti/db/relations.rb +++ b/lib/rasti/db/relations.rb @@ -37,7 +37,7 @@ def initialize(name, source_collection_class, options={}) end def target_collection_class - @target_collection_class ||= @options[:collection].is_a?(Class) ? @options[:collection] : constantize(@options[:collection] || camelize(pluralize(name))) + @target_collection_class ||= @options[:collection].is_a?(Class) ? @options[:collection] : Consty.get(@options[:collection] || camelize(pluralize(name)), self.class) end private diff --git a/rasti-db.gemspec b/rasti-db.gemspec index 0919fe5..80f47d5 100644 --- a/rasti-db.gemspec +++ b/rasti-db.gemspec @@ -19,6 +19,7 @@ Gem::Specification.new do |spec| spec.require_paths = ['lib'] spec.add_runtime_dependency 'sequel', '~> 4.38' + spec.add_runtime_dependency 'consty', '~> 1.0' spec.add_development_dependency 'bundler', '~> 1.12' spec.add_development_dependency 'rake', '~> 11.0'