GithubHelp home page GithubHelp logo

loopforever / dm-is-self_referential Goto Github PK

View Code? Open in Web Editor NEW

This project forked from snusnu/dm-is-self_referential

1.0 2.0 1.0 101 KB

A datamapper plugin that allows declarative specification of self referential m:m relationships.

License: MIT License

Ruby 100.00%

dm-is-self_referential's Introduction

dm-is-self_referential

A datamapper plugin that allows declarative specification of self referential m:m relationships.

Examples

class User

  include DataMapper::Resource

  property :id, Serial
  property :name, String, :nullable => false, :unique => true, :unique_index => true

  # with default options
  #
  # will define and thus be able to automigrate the UserToUser model
  # with the following relationships established between the participating models
  #
  #   has n, :user_children, 'UserToUser', :child_key => [ :source_id ]
  #   has n, :user_parents,  'UserToUser', :child_key => [ :target_id ]
  #
  # once dm-core has full support for the following m:m relationships built in,
  # these will also get established.
  #
  #   has n, :children, self, :through => :user_children, :via => :target
  #   has n, :parents , self, :through => :user_parents,  :via => :source
  #
  # In the meantime, the following helper methods get defined
  #
  #   def children
  #     children_ids = self.user_children.map { |r| r.target_id }
  #     self.class.all(:id => children_ids)
  #   end
  #
  #   def parents
  #     parent_ids = self.user_parents.map { |r| r.source_id }
  #     self.class.all(:id => parent_ids)
  #   end
  #
  is :self_referential

  # With explicit intermediate model name and default options
  #
  # Will define and thus be able to automigrate the Friendship model
  # with the following relationships established between the participating models
  #
  #   has n, :user_children, 'Friendship', :child_key => [ :source_id ]
  #   has n, :user_parents,  'Friendship', :child_key => [ :target_id ]
  #
  # Once dm-core has full support for the following m:m relationships built in,
  # these will also get established.
  #
  #   has n, :children, self, :through => :user_children, :via => :target
  #   has n, :parents , self, :through => :user_parents,  :via => :source
  #
  # In the meantime, the following helper methods get defined
  #
  #   def children
  #     children_ids = self.user_children.map { |r| r.target_id }
  #     self.class.all(:id => children_ids)
  #   end
  #
  #   def parents
  #     parent_ids = self.user_parents.map { |r| r.source_id }
  #     self.class.all(:id => parent_ids)
  #   end
  #
  is :self_referential, :through => 'Friendship'

  # With explicit intermediate model name and customized options
  #
  # Will define and thus be able to automigrate the UserRelationship model
  # with the following relationships established between the participating models
  #
  #   has n, :user_children, 'UserRelationship', :child_key => [ :parent_user_id ]
  #   has n, :user_parents,  'UserRelationship', :child_key => [ :child_user_id  ]
  #
  # Once dm-core has full support for the following m:m relationships built in,
  # these will also get established.
  #
  #   has n, :child_users,  User, :through => :user_children, :via => :child_user
  #   has n, :parent_users, User, :through => :user_parents,  :via => :parent_user
  #
  # In the meantime, the following helper methods get defined
  #
  #   def child_users
  #     children_ids = self.user_children.map { |r| r.target_id }
  #     self.class.all(:id => children_ids)
  #   end
  #
  #   def parent_users
  #     parent_ids = self.user_parents.map { |r| r.source_id }
  #     self.class.all(:id => parent_ids)
  #   end
  #
  is :self_referential, :through => 'UserRelationship',
    :children => :child_users,
    :parents  => :parent_users,
    :source   => :parent_user,
    :target   => :child_user

end

Copyright © 2009 Martin Gamsjaeger (snusnu). See LICENSE for details.

dm-is-self_referential's People

Contributors

snusnu avatar loopforever avatar

Stargazers

 avatar

Watchers

 avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.