Permit symbolic names in the model.
authorFrantišek Dvořák <valtri@civ.zcu.cz>
Tue, 13 Sep 2016 14:26:01 +0000 (16:26 +0200)
committerFrantišek Dvořák <valtri@civ.zcu.cz>
Tue, 13 Sep 2016 14:38:08 +0000 (16:38 +0200)
models/network.rb
models/range.rb

index 44cf15d..3e09ca3 100644 (file)
@@ -36,10 +36,8 @@ module Now
     attr_accessor :zone
 
     def initialize(parameters = {})
-      unless parameters.key?(:id)
-        raise NowError.new(500), 'ID required in network object'
-      end
-      if parameters.key?(:range) && !valid_range?(parameters[:range])
+      range = parameters.key?(:range) && parameters[:range] || parameters.key?('range') && parameters['range']
+      if range && !valid_range?(range)
         raise NowError.new(500), 'Valid range object required in network object'
       end
       super
@@ -48,7 +46,6 @@ module Now
     # Check to see if the all the properties in the model are valid
     # @return true if the model is valid
     def valid?
-      return false if id.nil?
       return false unless valid_range?(range)
       return true
     end
index 20b49b3..c390684 100644 (file)
@@ -18,10 +18,11 @@ module Now
     attr_accessor :allocation
 
     def initialize(parameters = {})
-      unless parameters.key?(:address)
+      address = parameters.key?(:address) && parameters[:address] || parameters.key?('address') && parameters['address']
+      unless address
         raise NowError.new(500), 'Internal error: IP network address required'
       end
-      unless valid_address?(parameters[:address])
+      unless valid_address?(address)
         raise NowError.new(500), 'Internal error: Invalid IP network address'
       end
       super
@@ -71,6 +72,20 @@ module Now
       return h
     end
 
+    # Build the object from hash
+    # @return [Now::Range] Returns the Now object of the address range
+    def self.from_hash(h = {})
+      p = {}
+
+      v = (h.key?('address') && h['address']) || (h.key?(:address) && h[:address])
+      p[:address] = IPAddress v if v
+
+      v = h.key?('allocation') && h['allocation'] || h.key?(:allocation) && h[:allocation]
+      p[:allocation] = v if v
+
+      new(p)
+    end
+
     private
 
     def valid_address?(value)