On Aug 16, 2011, at 5:33 AM, joe wrote:
> Hi All,

Hi Joe!

> I'm auto building some HTML forms from DM models

Cool! If you come up with something reusable, please consider sharing it!

> require 'rubygems'
> require 'dm-core'
> require 'dm-types'
> 
> class T
>  include DataMapper::Resource
…
>  property :enum,   Enum[:a, :b, :c]
>  property :flag,   Flag[:a, :b, :c]
> end
… 
> #<Class:0x1015a1d68>
> #<Class:0x10158b540>
> 
> The last two (Enum & Flag) obviously didn't come out as expected. If I
> use T.enum.kind_of?(::DataMapper::Property::Enum) then it will show
> true.

Enum.[] and Flag.[] both generate anonymous subclasses and then initialize an 
instance for use. Personally, I question the need for an anonymous subclass in 
this case, but I'm not ready to rework the Enum and Flag Properties, so… take 
that for what it is: a whiny complaint about someone else's code that I have 
little intention of addressing :P.

> So the question, I suppose, is there a consistent way to look at all
> the properties and determine their type?

You could enumerate the list of supported Property subclasses (types) in a case 
statement, which provides the same semantics as #kind_of? (by way of 
Class#===). One thing to consider with this approach is the inheritance 
hierarchy among types, eg. Text inherits from String, and Slug from Text (I 
think), so you would want to order the Property tests from most specific to 
least (subclasses before superclasses). Ex:

case property
when DataMapper::Property::Slug; # … do Slug stuff …
when DataMapper::Property::Text; # … do Text stuff …
when DataMapper::Property::String; # … do String stuff …
… 
when DataMapper::Property::Enum; # … do Enum stuff… 
when DataMapper::Property::Flag; # … do Flag stuff… 
end


Hope that helps,
Emmanuel

-- 
You received this message because you are subscribed to the Google Groups 
"DataMapper" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/datamapper?hl=en.

Reply via email to