-
Notifications
You must be signed in to change notification settings - Fork 1
Unit of work #7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Ptico
wants to merge
8
commits into
master
Choose a base branch
from
uow
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Unit of work #7
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
f9a8120
Add one to one spec [skip ci]
Ptico 52058d3
Basic associations
Ptico cfebdf2
Merge pull request #4 from Aejis/uow-associations
Ptico 700e2b8
Basic relation object
Ptico d669be7
Add relations list
Ptico 33b0e0d
Add some docs
Ptico 1119ce9
Add relations dsl
Ptico f161478
Extract persist processor
Ptico File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,2 @@ | ||
| --color | ||
| --warnings | ||
| --require spec_helper |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,3 +4,4 @@ source 'https://rubygems.org' | |
| gemspec | ||
|
|
||
| gem 'sequelize', github: 'Ptico/sequelize' | ||
| gem 'pry' | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| module Dml | ||
| class Relation | ||
| UndefinedDependencyError = Class.new(StandardError) | ||
|
|
||
| attr_reader :name | ||
|
|
||
| attr_reader :primary_key | ||
|
|
||
| attr_reader :associations | ||
|
|
||
| attr_reader :dependencies | ||
|
|
||
| def freeze | ||
| @associations.freeze | ||
| @dependencies.freeze | ||
|
|
||
| super | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def initialize(name, pk=nil, associations={}) | ||
| @name = name | ||
| @primary_key = pk.kind_of?(Array) ? pk.map(&:to_sym) : pk.to_sym | ||
| @associations = associations | ||
|
|
||
| @dependencies = Set[] | ||
|
|
||
| associations.values.each do |assoc| | ||
| dependencies.add(assoc.target_relation) | ||
| end | ||
|
|
||
| freeze | ||
| end | ||
|
|
||
| end | ||
| end | ||
|
|
||
| require 'dml/relation/list' | ||
|
|
||
| require 'dml/relation/associations/to_one' | ||
| require 'dml/relation/associations/one_to_one' | ||
| require 'dml/relation/associations/many_to_one' | ||
|
|
||
| require 'dml/relation/dsl' | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| module Dml | ||
| class Relation | ||
| module Associations | ||
|
|
||
| class ManyToOne < ToOne | ||
|
|
||
| def type | ||
| :many_to_one | ||
| end | ||
|
|
||
| end | ||
|
|
||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| module Dml | ||
| class Relation | ||
| module Associations | ||
|
|
||
| class OneToOne < ToOne | ||
|
|
||
| def type | ||
| :one_to_one | ||
| end | ||
|
|
||
| end | ||
|
|
||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,155 @@ | ||
| module Dml | ||
| class Relation | ||
| module Associations | ||
|
|
||
| class ToOne | ||
|
|
||
| ## | ||
| # Source relation name | ||
| # | ||
| # Returns: {Symbol} | ||
| # | ||
| attr_reader :source_relation | ||
|
|
||
| ## | ||
| # Association name | ||
| # | ||
| # Returns: {Symbol} | ||
| # | ||
| attr_reader :target_name | ||
|
|
||
| ## | ||
| # Associated relation name | ||
| # | ||
| # Returns: {Symbol} | ||
| # | ||
| attr_reader :target_relation | ||
|
|
||
| ## | ||
| # Source foreign key list | ||
| # | ||
| # Example: | ||
| # | ||
| # association.foreign_keys #=> [:company_id, :company_country] | ||
| # | ||
| # Returns: {Array(Symbol)} | ||
| # | ||
| attr_reader :foreign_keys | ||
|
|
||
| ## | ||
| # Associated relation primary keys list | ||
| # | ||
| # Example: | ||
| # | ||
| # association.target_keys #=> [:id, :country_id] | ||
| # | ||
| # Returns: {Array(Symbol)} | ||
| # | ||
| attr_reader :target_keys | ||
|
|
||
| ## | ||
| # Foreign key => primary key list | ||
| # | ||
| # Example: | ||
| # | ||
| # association.reference_keys #=> { :company_id => :id, :company_country => :country_id } | ||
| # | ||
| # Returns: {Hash} | ||
| # | ||
| attr_reader :reference_keys | ||
|
|
||
| ## | ||
| # Association options | ||
| # | ||
| # Returns: {Hash} | ||
| # | ||
| attr_reader :options | ||
|
|
||
| ## | ||
| # Abstract: Association type | ||
| # | ||
| # Returns: {Symbol} | ||
| # | ||
| def type | ||
| fail(NotImplementedError) | ||
| end | ||
|
|
||
| ## | ||
| # Deeply freeze object | ||
| # | ||
| def freeze | ||
| @options.freeze | ||
| @foreign_keys.freeze | ||
| @target_keys.freeze | ||
| @reference_keys.freeze | ||
|
|
||
| super | ||
| end | ||
|
|
||
| private | ||
|
|
||
| ## | ||
| # Constructor: | ||
| # | ||
| # Params: | ||
| # - source_name {Symbol} source relation name | ||
| # - target_name {Symbol} association name | ||
| # - options {Hash} | ||
| # - target_relation {Symbol|String} target relation name | ||
| # - foreign_keys {Symbol|Array} source foreign key(s) | ||
| # - target_keys {Symbol|Array} target primary key(s) | ||
| # | ||
| def initialize(source_name, target_name, options={}) | ||
| @source_relation = source_name | ||
| @target_name = target_name | ||
|
|
||
| @options = options | ||
|
|
||
| set_target_relation | ||
| set_foreign_keys | ||
| set_target_keys | ||
| set_reference_keys | ||
|
|
||
| freeze | ||
| end | ||
|
|
||
| ## | ||
| # Private: set target relation name | ||
| # | ||
| # If name given in options - take it unchanged, | ||
| # otherwise – pluralize association name | ||
| # | ||
| def set_target_relation | ||
| @target_relation = if options.has_key?(:target_relation) | ||
| options[:target_relation] | ||
| else | ||
| Inflecto.pluralize(target_name) | ||
| end.to_sym | ||
| end | ||
|
|
||
| ## | ||
| # Private: set source relation foreign key list | ||
| # | ||
| def set_foreign_keys | ||
| @foreign_keys = Array(options[:foreign_keys] || "#{target_name}_id").map(&:to_sym) | ||
| end | ||
|
|
||
| ## | ||
| # Private: set target relation primary key list | ||
| # | ||
| def set_target_keys | ||
| @target_keys = Array(options[:target_keys] || :id).map(&:to_sym) | ||
| end | ||
|
|
||
| ## | ||
| # Private: create hash where keys is a source foreign keys | ||
| # and values is a target primary keys | ||
| # | ||
| def set_reference_keys | ||
| @reference_keys = Hash[foreign_keys.zip(target_keys)] | ||
| end | ||
| end | ||
|
|
||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| module Dml | ||
| class Relation | ||
|
|
||
| class DSL | ||
| class Relation | ||
| attr_reader :associations | ||
|
|
||
| def key(*names) | ||
| @key = names | ||
| end | ||
|
|
||
| def belongs_to(association, &block) | ||
| assoc_name = association.to_sym | ||
| @associations[assoc_name] = BelongsTo.new(@name, assoc_name, &block).association | ||
| end | ||
|
|
||
| def belongs_to_one(association, &block) | ||
| assoc_name = association.to_sym | ||
| @associations[assoc_name] = BelongsToOne.new(@name, assoc_name, &block).association | ||
| end | ||
|
|
||
| def relation | ||
| Dml::Relation.new(@name, @key, @associations) | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def initialize(name, &block) | ||
| @name = name.to_sym | ||
| @key = [:id] | ||
| @associations = {} | ||
|
|
||
| instance_eval(&block) if block_given? | ||
| end | ||
| end | ||
|
|
||
| class BelongsTo | ||
| def relation(name) | ||
| @options[:target_relation] = name | ||
| end | ||
|
|
||
| def foreign_key(*keys) | ||
| @options[:foreign_keys] = keys | ||
| end | ||
|
|
||
| def reference_key(*keys) | ||
| @options[:target_keys] = keys | ||
| end | ||
|
|
||
| def association | ||
| klass.new(@source_name, @target_name, @options) | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def initialize(source_name, target_name, &block) | ||
| @source_name = source_name | ||
| @target_name = target_name | ||
|
|
||
| @options = {} | ||
|
|
||
| instance_eval(&block) if block_given? | ||
| end | ||
|
|
||
| def klass | ||
| Dml::Relation::Associations::ManyToOne | ||
| end | ||
| end | ||
|
|
||
| class BelongsToOne < BelongsTo | ||
| def klass | ||
| Dml::Relation::Associations::OneToOne | ||
| end | ||
| end | ||
|
|
||
| attr_reader :result | ||
|
|
||
| def relation(name, &block) | ||
| @relations << Relation.new(name, &block).relation | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def initialize(&block) | ||
| @relations = [] | ||
| instance_eval(&block) if block_given? | ||
| @result = Dml::Relation::List.new(@relations) | ||
| end | ||
| end | ||
|
|
||
| end | ||
| end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess it's better for this var to has type Array in any situation.