Module: OffTheGrid::GridResource
- Includes:
- Comparable
- Included in:
- AccessList, Host, HostGroup, Project, User
- Defined in:
- lib/off_the_grid/grid_resource.rb
Class Method Summary collapse
Instance Method Summary collapse
- #<=>(other) ⇒ Object
- #delete(safe = true) ⇒ Object
- #extract_detail(detail, options = {}) ⇒ Object
- #initalize(name) ⇒ Object
- #new? ⇒ Boolean
- #save(safe = true) ⇒ Object
- #validate_before_delete ⇒ Object
- #validate_before_save ⇒ Object
Class Method Details
.list ⇒ Object
9 10 11 |
# File 'lib/off_the_grid/grid_resource.rb', line 9 def self.list [] end |
Instance Method Details
#<=>(other) ⇒ Object
61 62 63 |
# File 'lib/off_the_grid/grid_resource.rb', line 61 def <=>(other) name <=> other.name end |
#delete(safe = true) ⇒ Object
26 27 28 29 30 31 32 33 |
# File 'lib/off_the_grid/grid_resource.rb', line 26 def delete(safe = true) if safe raise "Validation Failed" unless validate_before_delete remove else remove # don't bother checking if the resource exists end end |
#extract_detail(detail, options = {}) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/off_the_grid/grid_resource.rb', line 35 def extract_detail(detail, = {}) [:split_on] ||= /,|\s/ return [] unless respond_to?(:details) values = [] details.split("\n").each do |line| if values.size > 0 # Add follow-up lines from the weird word-wrapped output line.match(/^( ){8}/) ? values << line : break end values << line if line.match /^#{detail.to_s} / end values.join .gsub(/(#{detail.to_s} |\\|\s{2,})/, '') .split([:split_on]) .uniq - [''] end |
#initalize(name) ⇒ Object
5 6 7 |
# File 'lib/off_the_grid/grid_resource.rb', line 5 def initalize(name) @name = name end |
#new? ⇒ Boolean
13 14 15 |
# File 'lib/off_the_grid/grid_resource.rb', line 13 def new? !self.class.list.map(&:name).include?(name) end |
#save(safe = true) ⇒ Object
17 18 19 20 21 22 23 24 |
# File 'lib/off_the_grid/grid_resource.rb', line 17 def save(safe = true) if safe raise "Validation Failed" unless validate_before_save add else add # don't bother checking if the resource exists end end |
#validate_before_delete ⇒ Object
57 58 59 |
# File 'lib/off_the_grid/grid_resource.rb', line 57 def validate_before_delete !new? end |
#validate_before_save ⇒ Object
53 54 55 |
# File 'lib/off_the_grid/grid_resource.rb', line 53 def validate_before_save new? end |