Re: Extend an object with module stored
- From: Joel VanderWerf <vjoel@xxxxxxxxxxxxxxxxx>
- Date: Wed, 1 Feb 2006 04:09:31 +0900
Andrea Reginato wrote:
> I would like extend dynamically an object with one or more Module that I
> save into an Hash (I now only this way).
>
> -----------------------------
> categorie << Categoria.new('Definizioni Testuali', {:name_module =>
> ListaDefinizioni, })
> categorie << Categoria.new('Definizioni Testuali', {:name_module =>
> ListaDefinizioni, :name_module => ListaDefinizioniEditor})
> -----------------------------
>
> Then I have store this information in a YAML file, but when I dump the
> hash I lost the info about the module.
>
> -----------------------------
> --- !ruby/array:Categorie
> - !ruby/object:Categoria
> descrizione: Definizioni Testuali
> estensioni: # MY HASH
> :name_module: !ruby/object:Module {}
> - !ruby/object:Categoria
> descrizione: Definizioni Testuali
> estensioni: # MY HASH
> :name_module: !ruby/object:Module {}
> -----------------------------
>
> So when I try to extend, nothing to do.
> This code do not give me errors, but it do not extend my object.
>
> -----------------------------
> # @categoria.extension is the hash with the module
> @categoria.estensioni.each_value { |estensione| @denotato.send('extend',
> estensione) }
> -----------------------------
>
> Do you know some tecnique to extend an Object with module stored in YAML
> file?
> Thanks so much to all for the help.
>
> --Reis
>
I had some code to serialize module and class identities (without any of
their instance variables or class variables, though) with 1.8.2 (see
below) but it seems not to work with 1.8.4. I will take a look at it
later today.
class Module
def is_complex_yaml?
false
end
def to_yaml( opts = {} )
YAML::quick_emit( nil, opts ) { |out|
out << "!ruby/module "
self.name.to_yaml( :Emitter => out )
}
end
end
YAML.add_ruby_type(/^module/) do |type, val|
subtype, subclass = YAML.read_type_class(type, Module)
val.split(/::/).inject(Object) { |p, n| p.const_get(n)}
end
class Class
def to_yaml( opts = {} )
YAML::quick_emit( nil, opts ) { |out|
out << "!ruby/class "
self.name.to_yaml( :Emitter => out )
}
end
end
YAML.add_ruby_type(/^class/) do |type, val|
subtype, subclass = YAML.read_type_class(type, Class)
val.split(/::/).inject(Object) { |p, n| p.const_get(n)}
end
--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407
.
- Follow-Ups:
- YAML-ing classes and modules in 1.8.4 -- Was: Re: Extend an object with module stored
- From: Joel VanderWerf
- YAML-ing classes and modules in 1.8.4 -- Was: Re: Extend an object with module stored
- References:
- Extend an object with module stored
- From: Andrea Reginato
- Extend an object with module stored
- Prev by Date: Re: Mongrel HTTP Library 0.2.0 (Fast And RubyForgified)
- Next by Date: XML, WebService and Character Encoding issue
- Previous by thread: Extend an object with module stored
- Next by thread: YAML-ing classes and modules in 1.8.4 -- Was: Re: Extend an object with module stored
- Index(es):
Relevant Pages
|