On Wed, 2012-09-05 at 15:15 +0200, [email protected] wrote:
> From: Michal Fojtik <[email protected]>
> 
> * This patch also moves this to CIMI::Model::Base instead
>   of schema to make sure the updated schema is the model
>   schema.
> 
> Signed-off-by: Michal fojtik <[email protected]>
> diff --git a/server/lib/cimi/models/base.rb b/server/lib/cimi/models/base.rb
> index b375ce0..b627b82 100644
> --- a/server/lib/cimi/models/base.rb
> +++ b/server/lib/cimi/models/base.rb
> @@ -94,6 +94,20 @@ class CIMI::Model::Base
>    # attribute, we also define a getter and a setter to access/change the
>    # value for that attribute
>    class << self
> +
> +    def <<(model)
> +      clone_base_schema unless base_schema_cloned?
> +      member_name = model.name.split("::").last
> +      if ::Struct.const_defined?("CIMI_#{member_name}")
> +        puts "Removing struct"
> +        ::Struct.send(:remove_const, "CIMI_#{member_name}")
> +      end
> +      member_symbol = member_name.underscore.pluralize.to_sym
> +      members = CIMI::Model::Schema::Array.new(member_symbol)
> +      members.struct.schema.attributes = model.schema.attributes
> +      base_schema.attributes << members
> +    end

I have to admit that I don't understand this code at all; how does the
assignment members.struct.schema.attributes = model.schema.attributes
make things safer ? Also, why can't the same be done in the Schema
class, since we only manipulate attributes of the Schema class in the
code above ?

In general, we should be able to get rid of all the *Collection classes
- that was part of the whole collection exercise in CIMI, to make it
possible to use the same code for all collections.

In terms of the schema DSL, I would strive towards something that lets
us describe machines like so:

class CIMI::Model::Machine < CIMI::Model::Base
  
        # Set up plumbing for the machine collection, i.e., code
        # to serialize/deserialize a collection of machines
        acts_as_root_collection

        text :state
        text :cpu
        ...
        
        # Expand to a collection with count, operations, etc.
        collection :disks, :class => CIMI::Model::Disk
end

I think we will need to have collections as a subclass of
Schema::Attribute at some point, so that we can control expansion of
collections via $expand during the serialization phase. Basically,
collections have two serializations: the unexpanded one with just a
href, and the expanded one with all the details. Which means that at
some point the to_xml/to_json methods will have to take parameters to
control expansion.

I don't think we should address $expand complications with these
patches, but that's where this code is headed.

David


Reply via email to