class CanCamel::Node

Attributes

inherited[R]
path[W]

Public Class Methods

with_parents() click to toggle source
# File lib/can_camel/node.rb, line 28
def self.with_parents
  joins("LEFT JOIN #{TABLE_NAME} AS parents ON #{TABLE_NAME}.parent_id = parents.id")
end

Public Instance Methods

cached_inherited_node() click to toggle source
# File lib/can_camel/node.rb, line 22
def cached_inherited_node
  return unless inherit_id
  @inherited_path ||= inherited_node.path
  Cache[@inherited_path]
end
cached_parent() click to toggle source
# File lib/can_camel/node.rb, line 14
def cached_parent
  Cache[path[1..-1]]
end
cached_uncle() click to toggle source
# File lib/can_camel/node.rb, line 55
def cached_uncle
  Cache.children_of(cached_parent.cached_parent).select { |x| x.name == name }.first
end
condition() click to toggle source
Calls superclass method
# File lib/can_camel/node.rb, line 51
def condition
  super.symbolize_keys
end
inherit!(source = nil) click to toggle source
# File lib/can_camel/node.rb, line 32
def inherit!(source = nil)
  raise 'inherit nodes only after cache built' unless Cache[path]
  inherit_source! source

  return if inherited

  @inherited = true

  inherit_source! cached_parent
  inherit_source! cached_inherited_node
  # inherit_source! cached_uncle
end
name() click to toggle source
Calls superclass method
# File lib/can_camel/node.rb, line 47
def name
  super.to_sym
end
path() click to toggle source
# File lib/can_camel/node.rb, line 61
def path
  @path ||= parent ? [name] + parent.path  : [name]
end
rank() click to toggle source
# File lib/can_camel/node.rb, line 18
def rank
  raise "inheriting invalid node"
end

Private Instance Methods

inherit_field(source, field) { |send| ... } click to toggle source
# File lib/can_camel/node.rb, line 96
def inherit_field(source, field)
  return if override_fields.include?(field.to_s)
  yield source.send(field)
end
inherit_fields!(source) click to toggle source
# File lib/can_camel/node.rb, line 75
def inherit_fields!(source)
  inherit_field(source, :description) { |x| self.description ||= x }
  inherit_field(source, :result) { |x| result.merge!(x) { |_k, l, _r| l } }
  inherit_field(source, :condition) { |x| self.condition = x.merge(condition) }
end
inherit_neighbours!(source) click to toggle source
# File lib/can_camel/node.rb, line 69
def inherit_neighbours!(source)
  Cache.children_of(source).each do |child|
    Cache.append child, path if child.rank > rank
  end
end
inherit_source!(source) click to toggle source
# File lib/can_camel/node.rb, line 81
def inherit_source!(source)
  return unless source

  source.inherit!

  # WTF: if we inherit neighbours only once, it fails. Two times should be fine enought,
  # but we have to do find a way to understand when it enought in code
  # TODO: Refactor
  # FIXME: Refactor
  inherit_neighbours!(source)
  inherit_neighbours!(source)

  inherit_fields!(source) unless source.is_a? Root
end