Module: DataMapper::Is::AwesomeSet::ClassMethods
- Defined in:
- lib/dm-is-awesome_set.rb
Overview
def is_awesome_set
Instance Method Summary collapse
-
#adjust_gap!(scoped_set, at, adjustment) ⇒ Object
:nodoc:.
-
#check_scope(hash) ⇒ Object
Raises an error if the scope is not valid.
- #child_keys ⇒ Object
-
#extract_scope(hash) ⇒ Object
Return only the attributes that deal with the scope, will raise an error on invalid scope.
-
#full_set(scope = {}) ⇒ Object
Gets the full set with scope behavior like @root.
-
#get_class ⇒ Object
Since DataMapper looks for all records in a table when using discriminators when using the parent model , we’ll look for the earliest ancestor class that is a nested set.
-
#ias_options ⇒ Object
:nodoc:.
-
#is_nested_set? ⇒ Boolean
:nodoc:.
-
#leaves(scope = {}) ⇒ Object
Retrieves all nodes that do not have children.
-
#root(scope = {}) ⇒ Object
Get the root with no args if there is no scope Pass the scope or an object with scope to get the first root.
-
#root_hash ⇒ Object
Return a hash that gets the roots.
-
#roots(scope = {}) ⇒ Object
Same as @root, but gets all roots.
- #scope_keys ⇒ Object
-
#set_options(options) ⇒ Object
:nodoc:.
-
#valid_scope?(hash) ⇒ Boolean
Checks to see if the hash or object contains a valid scope by checking attributes or keys.
Instance Method Details
#adjust_gap!(scoped_set, at, adjustment) ⇒ Object
:nodoc:
92 93 94 95 |
# File 'lib/dm-is-awesome_set.rb', line 92 def adjust_gap!(scoped_set, at, adjustment) #:nodoc: scoped_set.all(:rgt.gt => at).adjust!({:rgt => adjustment},true) scoped_set.all(:lft.gt => at).adjust!({:lft => adjustment},true) end |
#check_scope(hash) ⇒ Object
Raises an error if the scope is not valid
72 73 74 |
# File 'lib/dm-is-awesome_set.rb', line 72 def check_scope(hash) raise 'Invalid scope: ' + hash.inspect if !valid_scope?(hash) end |
#child_keys ⇒ Object
57 |
# File 'lib/dm-is-awesome_set.rb', line 57 def child_keys; [:child_key]; end |
#extract_scope(hash) ⇒ Object
Return only the attributes that deal with the scope, will raise an error on invalid scope
77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/dm-is-awesome_set.rb', line 77 def extract_scope(hash) check_scope(hash) ret = {} send_to_obj = hash.is_a?(self) scope_keys.each do |sk| if send_to_obj && self.public_method_defined?(name = sk) ret[sk] = hash.__send__(sk) else ret[sk] = hash[sk] end end ret end |
#full_set(scope = {}) ⇒ Object
Gets the full set with scope behavior like @root
119 120 121 122 |
# File 'lib/dm-is-awesome_set.rb', line 119 def full_set(scope = {}) scope = extract_scope(scope) get_class.all(scope.merge(:order => [:lft.asc])) end |
#get_class ⇒ Object
Since DataMapper looks for all records in a table when using discriminators when using the parent model , we’ll look for the earliest ancestor class that is a nested set.
134 135 136 137 138 |
# File 'lib/dm-is-awesome_set.rb', line 134 def get_class #:nodoc: klass = self klass = klass.superclass while klass.superclass.respond_to?(:is_nested_set?) && klass.superclass.is_nested_set? klass end |
#ias_options ⇒ Object
:nodoc:
55 |
# File 'lib/dm-is-awesome_set.rb', line 55 def ; @ias_options || superclass. end |
#is_nested_set? ⇒ Boolean
:nodoc:
59 60 61 |
# File 'lib/dm-is-awesome_set.rb', line 59 def is_nested_set? #:nodoc: true end |
#leaves(scope = {}) ⇒ Object
Retrieves all nodes that do not have children. This needs to be refactored for more of a DM style, if possible.
126 127 128 129 |
# File 'lib/dm-is-awesome_set.rb', line 126 def leaves(scope = {}) scope = extract_scope(scope) get_class.all(scope.merge(:order => [:lft.asc], :conditions => ["`rgt` - `lft` = 1"])) end |
#root(scope = {}) ⇒ Object
Get the root with no args if there is no scope Pass the scope or an object with scope to get the first root
107 108 109 110 |
# File 'lib/dm-is-awesome_set.rb', line 107 def root(scope = {}) scope = extract_scope(scope) get_class.first(scope.merge(root_hash.merge(:order => [:lft.asc]))) end |
#root_hash ⇒ Object
Return a hash that gets the roots
98 99 100 101 102 |
# File 'lib/dm-is-awesome_set.rb', line 98 def root_hash ret = {} child_keys.each { |ck| ret[ck] = nil } ret end |
#roots(scope = {}) ⇒ Object
Same as @root, but gets all roots
113 114 115 116 |
# File 'lib/dm-is-awesome_set.rb', line 113 def roots(scope = {}) scope = extract_scope(scope) get_class.all(scope.merge(root_hash.merge(:order => [:lft.asc]))) end |
#scope_keys ⇒ Object
58 |
# File 'lib/dm-is-awesome_set.rb', line 58 def scope_keys; [:scope]; end |
#set_options(options) ⇒ Object
:nodoc:
51 52 53 |
# File 'lib/dm-is-awesome_set.rb', line 51 def () #:nodoc: @ias_options = { :child_key => [:parent_id], :scope => [] }.merge() end |
#valid_scope?(hash) ⇒ Boolean
Checks to see if the hash or object contains a valid scope by checking attributes or keys
64 65 66 67 68 69 |
# File 'lib/dm-is-awesome_set.rb', line 64 def valid_scope?(hash) return true if hash.is_a?(self) return false unless hash.is_a?(Hash) scope_keys.each { |sk| return false unless hash.keys.include?(sk) } true end |