Module: OffTheGrid::GridResource

Includes:
Comparable
Included in:
Host, NamedResource
Defined in:
lib/off_the_grid/grid_resource.rb

Overview

Handy helper methods for all Grid-related Resources Serves as a mixable base

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.listObject



7
8
9
# File 'lib/off_the_grid/grid_resource.rb', line 7

def self.list
  []
end

Instance Method Details

#<=>(other) ⇒ Object



51
52
53
# File 'lib/off_the_grid/grid_resource.rb', line 51

def <=>(other)
  name <=> other.name
end

#delete(safe = true) ⇒ Object



20
21
22
23
# File 'lib/off_the_grid/grid_resource.rb', line 20

def delete(safe = true)
  raise 'Validation Failed' if safe && !validate_before_delete
  remove
end

#extract_detail(detail, options = {}) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/off_the_grid/grid_resource.rb', line 25

def extract_detail(detail, options = {})
  options[:split_on] ||= /,|\s/
  return [] unless respond_to?(:details)

  values = []
  details.split("\n").each do |line|
    unless values.empty?
      # Add follow-up lines from the weird word-wrapped output
      line =~ /^( ){8}/ ? values << line : break
    end
    values << line if line =~ /^#{detail.to_s} /
  end
  values.join
        .gsub(/(#{detail.to_s} |\\|\s{2,})/, '')
        .split(options[:split_on])
        .uniq - ['']
end

#new?Boolean

Returns:

  • (Boolean)


11
12
13
# File 'lib/off_the_grid/grid_resource.rb', line 11

def new?
  !self.class.list.map(&:name).include?(name)
end

#save(safe = true) ⇒ Object



15
16
17
18
# File 'lib/off_the_grid/grid_resource.rb', line 15

def save(safe = true)
  raise 'Validation Failed' if safe && !validate_before_save
  add
end

#validate_before_deleteObject



47
48
49
# File 'lib/off_the_grid/grid_resource.rb', line 47

def validate_before_delete
  !new?
end

#validate_before_saveObject



43
44
45
# File 'lib/off_the_grid/grid_resource.rb', line 43

def validate_before_save
  new?
end