Module: DataMapper::Is::AwesomeSet::ClassMethods

Defined in:
lib/dm-is-awesome_set.rb

Overview

def is_awesome_set

Instance Method Summary collapse

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_keysObject



57
# File 'lib/dm-is-awesome_set.rb', line 57

def child_keys; ias_options[: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_classObject

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_optionsObject

:nodoc:



55
# File 'lib/dm-is-awesome_set.rb', line 55

def ias_options;  @ias_options || superclass.ias_options end

#is_nested_set?Boolean

:nodoc:

Returns:

  • (Boolean)


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_hashObject

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_keysObject



58
# File 'lib/dm-is-awesome_set.rb', line 58

def scope_keys; ias_options[:scope]; end

#set_options(options) ⇒ Object

:nodoc:



51
52
53
# File 'lib/dm-is-awesome_set.rb', line 51

def set_options(options) #:nodoc:
  @ias_options = { :child_key => [:parent_id], :scope => [] }.merge(options)
end

#valid_scope?(hash) ⇒ Boolean

Checks to see if the hash or object contains a valid scope by checking attributes or keys

Returns:

  • (Boolean)


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