Sha256: 5c4958cfff60d8a63379d5a18b041dc17f44fa4c433f0292fce7d8f8f276a9b2

Contents?: true

Size: 1.65 KB

Versions: 1

Compression:

Stored size: 1.65 KB

Contents

# Simple attribute handling for resources
# Ties in with ZerigoDNS::Resource by defining a 
# from_response method to initialize the class by its attributes. 
module ZerigoDNS::Resource::Attributes
  module InstanceMethods
    attr_accessor :attributes
    
    
    # Allows method-style access to the attributes.
    def method_missing mtd, *args
      if mtd.to_s.chars.to_a.last == '='
        raise ArgumentError, "Invalid number of arguments (#{args.length} for 1)" if args.length != 1
        attributes[mtd.to_s.slice(0,mtd.to_s.length-1)] = args.first
      else
        raise ArgumentError, "Invalid number of arguments (#{args.length} for 0)" if args.length != 0
        attributes[mtd.to_s]
      end
    end
    
    # Converts the resource to a hash
    # @return [Hash] The attributes
    def to_hash
      attributes
    end
    
    # Initialize a new resource
    # @param [Hash] attributes Initial attributes.
    def initialize attributes={}
      @attributes = {}
      merge_attributes attributes
    end
    
    private
    
    # Merge current attributes with specified ones.
    # Will handle symbols as well as strings as keys
    # @param [Hash] attrs Attributes to merge
    def merge_attributes attrs
      attrs.each do |key, val|
        send("#{key}=", val)
      end
    end
  end
  
  
  module ClassMethods
    # Constructs a new resource from a response
    # @param [Faraday::Response] response The response
    # @param [Hash] body The response body without root
    def from_response response, body
      new body
    end
  end
  
  def self.included includer
    includer.send :include, InstanceMethods
    includer.send :extend, ClassMethods
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
zerigodns-1.1.0 lib/zerigodns/resource/attributes.rb