Module: PebblePath

Defined in:
lib/pebble_path.rb,
lib/pebble_path/positions.rb

Defined Under Namespace

Classes: Positions

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#pathObject

Returns the value of attribute path.



34
35
36
# File 'lib/pebble_path.rb', line 34

def path
  @path
end

Class Method Details

.included(base) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/pebble_path.rb', line 13

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 Pebblebed::Uid.valid_path?(path)
      attributes = PebblePath.to_conditions(path)
      path = self.where(attributes).first
      path ||= self.create!(attributes)
    end

  end
end

.max_depthObject



5
6
7
# File 'lib/pebble_path.rb', line 5

def self.max_depth
  Positions::MAX_DEPTH
end

.to_conditions(paths) ⇒ Object



9
10
11
# File 'lib/pebble_path.rb', line 9

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

Instance Method Details

#initialize_pathObject



44
45
46
47
48
49
50
# File 'lib/pebble_path.rb', line 44

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



52
53
54
55
56
# File 'lib/pebble_path.rb', line 52

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