Module: Pebbles::Path

Defined in:
lib/pebbles-path.rb,
lib/pebbles-path/version.rb,
lib/pebbles-path/positions.rb

Defined Under Namespace

Classes: Positions

Constant Summary collapse

VERSION =
"0.0.3"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#pathObject

Returns the value of attribute path.



36
37
38
# File 'lib/pebbles-path.rb', line 36

def path
  @path
end

Class Method Details

.included(base) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/pebbles-path.rb', line 15

def self.included(base)
  base.class_eval do
    scope :by_path, lambda { |path|
      where Positions.to_conditions(path)
    }

    validates_presence_of :label_0
    validate :must_be_valid_uid_path

    after_initialize :initialize_path

    def self.declare!(path)
      raise ArgumentError, "Path must be valid" unless Pebbles::Uid.valid_path?(path)
      attributes = Pebbles::Path.to_conditions(path)
      path = self.where(attributes).first
      path ||= self.create!(attributes)
    end

  end
end

.max_depthObject



7
8
9
# File 'lib/pebbles-path.rb', line 7

def self.max_depth
  Positions::MAX_DEPTH
end

.to_conditions(paths) ⇒ Object



11
12
13
# File 'lib/pebbles-path.rb', line 11

def self.to_conditions(paths)
  Positions.to_conditions(paths)
end

Instance Method Details

#initialize_pathObject



46
47
48
49
50
51
52
# File 'lib/pebbles-path.rb', line 46

def initialize_path
  positions = []
  [:label_0, :label_1, :label_2, :label_3, :label_4, :label_5, :label_6, :label_7, :label_8, :label_9].each do |attribute|
    positions << send(attribute)
  end
  self.path = positions
end

#must_be_valid_uid_pathObject



54
55
56
57
58
# File 'lib/pebbles-path.rb', line 54

def must_be_valid_uid_path
  unless Pebbles::Uid.valid_path?(self.path.to_s)
    errors.add(:base, "Location path '#{self.path.to_s}' is invalid.")
  end
end