Emmanuel (and everybody else):

I wasn't a big fan of the idea of needing to put everything in a
specific order, so in the end I took the lazy way. Which ended up
being the least lazy way since I needed to learn a bunch more ruby. So
I ended up with:

  mappings = {
    DataMapper::Property::Text => 'text',
    DataMapper::Property::BCryptHash => 'password',
    DataMapper::Property::FilePath => 'string',
    .
    .
    .
    DataMapper::Property::Serial => 'integer',
    DataMapper::Property::Enum => 'enum',
    DataMapper::Property::Flag => 'flag',
  }
  mappings[mappings.map{|k, v| k if k ===
property}.collect.compact.sort[0]] rescue raise "Unknown property
type"

All the possibilities are selected and sorted from most specific to
least specific and the most specific is used. Seems to work even if it
is a bit more expensive computationally. Anyway, it was your idea
essentially, so thanks. If to_s is made to work for all the types,
I'll just switch over later.

On a similar topic, is there a way to use model definitions without
actually having an underlying database model? If so, I could use what
I'm doing as a DSL for all my forms.

Joe

On Aug 16, 8:47 pm, Emmanuel Gomez <[email protected]> wrote:
> 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