# Modeling -- how to support multiple views of same components

**URL:** <https://www.ruby-forum.com/t/modeling-how-to-support-multiple-views-of-same-components/110384>\
**Category:** Ruby\
**Created:** [July 25, 2007, 11:11pm UTC](https://www.ruby-forum.com/t/modeling-how-to-support-multiple-views-of-same-components/110384 "2007-07-25T23:11:58Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Trans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/dfb087/32.png) [@Trans](https://www.ruby-forum.com/u/Trans)\
**Post date:** [July 25, 2007, 11:11pm UTC](https://www.ruby-forum.com/t/modeling-how-to-support-multiple-views-of-same-components/110384/1 "2007-07-25T23:11:58Z")

</div>

I have a little design conundrum. I’m not sure the best way to model  
this. Basically I have two model “parts” and I want to support two  
ways of using them. Eg.

class Part1  
attr\_accessor :a, :b, :x  
end

class Part2  
attr\_accessor :x, :y, :z  
end

Now in the first case I want effectively:

class Model1 \< Part1  
def subpart  
@subpart ||= Part2.new  
end  
end

but in the second I want the equivalent of:

class Model2 \< Part1 \< Part2

but obviously I can’t do multiple inheritance.

So I’ve been trying to figure out the best way to do it. Do I use  
modules? Do I use Forwardable? What’s the cleanest, fastest, least  
troubling way to go about it?

Thanks,  
T.

---

<div class="post-metadata">

**Author:** ![Trans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/dfb087/32.png) [@Trans](https://www.ruby-forum.com/u/Trans)\
**Post date:** [July 26, 2007, 7:56pm UTC](https://www.ruby-forum.com/t/modeling-how-to-support-multiple-views-of-same-components/110384/2 "2007-07-26T19:56:09Z")

</div>

On 25.07.2007 23:11, Trans wrote:

> end
> 
> class Model2 \< Part1 \< Part2
> 
> but obviously I can’t do multiple inheritance.
> 
> So I’ve been trying to figure out the best way to do it. Do I use  
> modules? Do I use Forwardable? What’s the cleanest, fastest, least  
> troubling way to go about it?

It’s difficult to comment without knowing the domain or your use case.  
IMHO the fact that you want different inheritance hierarchies indicates  
that there is something wrong, namely inheritance might not be the best  
approach here. It seems like composition would be better but since I do  
not know what you are doing please take this with a large grain of salt.

Can you disclose more detail?

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![Trans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/dfb087/32.png) [@Trans](https://www.ruby-forum.com/u/Trans)\
**Post date:** [July 26, 2007, 9:20pm UTC](https://www.ruby-forum.com/t/modeling-how-to-support-multiple-views-of-same-components/110384/3 "2007-07-26T21:20:19Z")

</div>

On Jul 26, 10:55 am, Robert K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Can you disclose more detail?

Sure. It’s for configuration information. On the one hand, I want to  
be able to read package settings from a file that is strictly  
dedicated to packaging. So, for instance my project might have  
a .package file that has basically:

project: foo  
version: 1.1  
dependencies: [facets]  
formats: [zip, tgz, gem]

However, I also want to support an uber-project configuration file,  
that would look like:

project: foo  
version: 1.1  
package:  
dependencies: [facets]  
formats: [zip, tgz, gem]

So I have two slightly different formats that I want to support with  
strict classes. Eg. in the first case:

pkg = Package.load(file)  
pkg.project #=\> ‘foo’  
pkg.formats #=\> [‘zip’,‘tgz’,‘gem’]

and in the other:

pkg = Project.load(file)  
pkg.project #=\> ‘foo’  
pkg.package.formats #=\> [‘zip’,‘tgz’,‘gem’]

So, the same info, but organized in the two different manners, as I  
described in the original post.

Note, by “strict class” I mean I’m not just keying off a YAML-loaded  
hash. I need a class b/c various attributes have default values,  
validation and formating applied, plus supporting methods.

T.

---

<div class="post-metadata">

**Author:** ![Trans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/dfb087/32.png) [@Trans](https://www.ruby-forum.com/u/Trans)\
**Post date:** [July 26, 2007, 10:36pm UTC](https://www.ruby-forum.com/t/modeling-how-to-support-multiple-views-of-same-components/110384/4 "2007-07-26T22:36:30Z")

</div>

On 26.07.2007 21:18, Trans wrote:

> project: foo  
> dependencies: [facets]
> 
> pkg = Project.load(file)  
> pkg.project #=\> ‘foo’  
> pkg.package.formats #=\> [‘zip’,‘tgz’,‘gem’]
> 
> So, the same info, but organized in the two different manners, as I  
> described in the original post.
> 
> Note, by “strict class” I mean I’m not just keying off a YAML-loaded  
> hash. I need a class b/c various attributes have default values,  
> validation and formating applied, plus supporting methods.

So as far as I can see you have these requirements:

1. reuse of config parameters and their defaults in multiple places

2. default values for config parameters

3. supporting functionality (whatever that is)

Some random thoughts (as I’m pretty tired already): Personally I’d limit  
3 to a bare minimum. I am sure there are multiple ways to handle  
default values (for example, using a template of nested Hashes and  
merging that with something parsed from the config file). OpenStruct or  
a similar concept might come in handy. Is validation of config values  
better done in (generic?) config classes or in application classes (I  
tend to believe the latter, because ensuring proper arguments is a task  
of the model). Hm… Probably not too useful. Maybe I have more ideas  
after sleeping. 🙂

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![Trans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/dfb087/32.png) [@Trans](https://www.ruby-forum.com/u/Trans)\
**Post date:** [July 26, 2007, 9:39pm UTC](https://www.ruby-forum.com/t/modeling-how-to-support-multiple-views-of-same-components/110384/5 "2007-07-26T21:39:21Z")

</div>

On 7/25/07, Trans [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:  
You just recently accepted a patch for snapshot, right?  
A nice one indeed I think.  
Does this ring the same bells to you than to me?

I am thinking of manipulating Hashes instead of classes, did I miss  
something or might it work?

Well from the design point of view that would recall Module Inclusion.

Cheers  
Robert
