class Mio::Model
Attributes
fields[RW]
resource_name[RW]
args[R]
client[R]
search[R]
Public Class Methods
field(key, type, desc, default=nil, matcher=nil)
click to toggle source
# File lib/mio/model.rb, line 10 def field key, type, desc, default=nil, matcher=nil @fields ||= [] @fields << {name: key, type: type, default: default, matcher: matcher, desc: desc,} end
mappings()
click to toggle source
# File lib/mio/model.rb, line 29 def mappings m = {} ObjectSpace.each_object(Class).each do |k| if k < Mio::Model m[ k.to_s.split('::').last.downcase ] = k end end m end
nested(val=nil)
click to toggle source
# File lib/mio/model.rb, line 20 def nested val=nil if val.nil? @nested_value || false else @nested_value = val end end
Also aliased as: nested?
new(client, args)
click to toggle source
# File lib/mio/model.rb, line 40 def initialize client, args @client = client @args = args @search = Mio::Search.new @client end
set_resource(r)
click to toggle source
# File lib/mio/model.rb, line 6 def set_resource r @resource_name = r.to_s end
Public Instance Methods
configure()
click to toggle source
# File lib/mio/model.rb, line 67 def configure @client.configure self.class.resource_name, @object['id'], config_hash end
create()
click to toggle source
# File lib/mio/model.rb, line 63 def create @client.create self.class.resource_name, create_hash end
go()
click to toggle source
# File lib/mio/model.rb, line 46 def go unless look_up @object = create else @object = look_up # We can't edit a running resource set_start :stop end configure if self.respond_to? :config_hash set_enable set_start return @object end
set_enable(a=nil)
click to toggle source
# File lib/mio/model.rb, line 73 def set_enable a=nil if a.nil? action = @args.enable == :true ? 'enable' : 'disable' else action = a.to_s end @client.action self.class.resource_name, @object['id'], {action: action} end
set_start(a=nil)
click to toggle source
# File lib/mio/model.rb, line 86 def set_start a=nil if a.nil? action = @args.start == :true ? 'start' : 'stop' else action = a.to_s end @client.action self.class.resource_name, @object['id'], {action: action} end
validate()
click to toggle source
# File lib/mio/model.rb, line 99 def validate testable = self.args.dup.to_h self.class.fields.each do |f| unless testable.key? f[:name] raise Mio::Model::MissingField, "Missing field #{f[:name]} to #{self}" end extracted_field = testable.delete f[:name] unless extracted_field.is_a? f[:type] raise Mio::Model::DataTypeError, "#{f[:name]} should be of type #{f[:type]} for #{self}" end #unless f[:matcher].nil? or extracted_field.to_s.match(f[:matcher]) if !f[:matcher].nil? && extracted_field.to_s.match(f[:matcher]).nil? raise Mio::Model::DataValueError, "#{self} #{f[:name]} value '#{extracted_field}' does not match #{f[:matcher]}" end end testable.keys.each do |k| raise Mio::Model::NoSuchField, "#{k} for #{self}" end true end
Also aliased as: valid?
Private Instance Methods
look_up()
click to toggle source
# File lib/mio/model.rb, line 126 def look_up r = self.class.resource_name all_resources = @client.find_all r return nil if all_resources['totalCount'] == 0 all_resources[r].find{|o| o['name'] == @args.name} end