Show
Ignore:
Timestamp:
02/07/08 15:57:55 (4 years ago)
Author:
ged
Message:
  • Added an alternate (nicer) syntax to define synonym properties. This syntax
    has a more limited scope, except that it can refer to properties defined in
    a parent entity. This is based on a patch from Alexandre da Silva.
Files:
1 modified

Legend:

Unmodified
Added
Removed
  • elixir/trunk/elixir/properties.py

    r269 r303  
    44properties such as fields and relationships (for those, please consult the  
    55corresponding modules), but also provides some more specialized properties,  
    6 such as `ColumnProperty`. It also provides the GenericProperty class which  
    7 allows you to wrap any SQLAlchemy property, and its DSL-syntax equivalent:  
    8 has_property_. 
     6such as `ColumnProperty` and `Synonym`. It also provides the GenericProperty  
     7class which allows you to wrap any SQLAlchemy property, and its DSL-syntax  
     8equivalent: has_property_. 
    99 
    1010`has_property` 
     
    3232 
    3333from elixir.statements import PropertyStatement 
    34 from sqlalchemy.orm import column_property 
     34from sqlalchemy.orm import column_property, synonym 
    3535 
    3636__doc_all__ = ['EntityBuilder', 'Property', 'GenericProperty',  
     
    150150    def evaluate_property(self, prop): 
    151151        return prop 
     152 
    152153 
    153154class ColumnProperty(GenericProperty): 
     
    179180        return column_property(prop.label(self.name)) 
    180181 
     182 
     183class Synonym(GenericProperty): 
     184    ''' 
     185    This class represents a synonym property of another property (column, ...) 
     186    of an entity.  As opposed to the `synonym` kwarg to the Field class (which  
     187    share the same goal), this class can be used to define a synonym of a  
     188    property defined in a parent class (of the current class). On the other 
     189    hand, it cannot define a synonym for the purpose of using a standard python  
     190    property in queries. See the Field class for details on that usage. 
     191 
     192    .. sourcecode:: python 
     193 
     194    class Person(Entity): 
     195        name = Field(String(30)) 
     196        primary_email = Field(String(100)) 
     197        email_address = Synonym('primary_email') 
     198 
     199    class User(Person): 
     200        user_name = Synonym('name') 
     201        password = Field(String(20)) 
     202    ''' 
     203 
     204    def evaluate_property(self, prop): 
     205        return synonym(prop) 
     206 
    181207#class Composite(GenericProperty): 
    182208#    def __init__(self, prop):