Let me try to explain what I need, then maybe someone may tell me how to do it.
I have a series of entities in my system. They are listed in an array
@entities = [ :component, :email, :form, :library, :page, :project, :resource, :template ]
I parse a command line using Slop gem, then I get all parsed options in the opts hash and the remaining command line parameters in the args array.
The first element of args array is always the entity I need to create to haldle the command line inputs. Let's assume, for the sake of this reasoning, that I'm creating a :project.
Now, I have a file project.rb, which contains:
entity_block = lambda { |ent| ent.entity_type = :project # All other initialization options . . .
}
And in my main module I have the following method:
def self.start(args,opts) # Saves arguments and options @arguments = args @options = opts # Isolates the entity which will handle the command line request @entity = @arguments[0] exit_error(103,@entites) if not @entities.include?(@entity.to_sym) # Requires the correct block for the entity type require "entities/#{@entity}" # Creates the entity class EmeraldFW::Entity.new(@arguments,@options) do entity_block end
end
As you may see, what I intend to do is requiring among the <entity_name>.rb
files, the file which contains the specific initial values for that specific entity.
It happens that this is not working. If it were, I wouldn't be here bothering you, of course.
I tried do use #tap
to inject the newly created Entity object in the lambda block defined by entity_block
, like this:
EmeraldFW::Entity.new(@arguments,@options).tap do entity_block
end
And I also tried to do &entity_block
like this?
EmeraldFW::Entity.new(@arguments,@options).tap do &entity_block
end
Summary: My problem, then, is how to create an entity dinamically?
Of course I considered the possibility of doing something like
entity_class = "EmeraldFW::#{@entity.capitalize}".split('::').inject(Object) {|o,c| o.const_get c}.new(@arguments,@options)
bt first I'd like to know if this other way is possible and what I am missing here.