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

- (Object) adjust_gap!(scoped_set, at, adjustment)

: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

- (Object) check_scope(hash)

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

- (Object) child_keys



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

def child_keys; ias_options[:child_key]; end

- (Object) extract_scope(hash)

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

- (Object) full_set(scope = {})

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

- (Object) get_class

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

- (Object) ias_options

:nodoc:



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

def ias_options;  @ias_options || superclass.ias_options end

- (Boolean) is_nested_set?

:nodoc:

Returns:

  • (Boolean)


59
60
61
# File 'lib/dm-is-awesome_set.rb', line 59

def is_nested_set? #:nodoc:
  true
end

- (Object) leaves(scope = {})

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

- (Object) root(scope = {})

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

- (Object) root_hash

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

- (Object) roots(scope = {})

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

- (Object) scope_keys



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

def scope_keys; ias_options[:scope]; end

- (Object) set_options(options)

: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

- (Boolean) valid_scope?(hash)

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