Sha256: 65a5413a662640bc01ec0e99c3946c77a43d3db915e54df54402589849c16f08

Contents?: true

Size: 1.74 KB

Versions: 33

Compression:

Stored size: 1.74 KB

Contents

class HyperResource
  class Objects < Hash
    attr_accessor :_resource

    def initialize(resource=nil) 
      self._resource = resource || HyperResource.new
    end

    ## Creates accessor methods in self.class and self._resource.class.
    ## Protects against method creation into HyperResource::Objects and
    ## HyperResource classes.  Just subclasses, please!
    def _hr_create_methods!(opts={}) # @private
      return if self.class.to_s == 'HyperResource::Objects'
      return if self._resource.class.to_s == 'HyperResource' 
      return if self.class.send(
        :class_variable_defined?, :@@_hr_created_objects_methods) 

      self.keys.each do |attr|
        attr_sym = attr.to_sym

        self.class.send(:define_method, attr_sym) do
          self[attr]
        end

        ## Don't stomp on _resource's methods
        unless _resource.respond_to?(attr_sym)
          _resource.class.send(:define_method, attr_sym) do
            objects.send(attr_sym)
          end
        end
      end

      self.class.send(:class_variable_set, :@@_hr_created_objects_methods, true) 
    end

    def []=(attr, value) # @private
      super(attr.to_s, value)
    end

    ## When +key+ is a string, returns the array of objects under that name.
    ## When +key+ is a number, returns +ith(key)+. Returns nil on lookup
    ## failure.
    def [](key)
      case key
      when String, Symbol
        return super(key.to_s) if self.has_key?(key.to_s)
        return super(key.to_sym) if self.has_key?(key.to_sym)
      when Fixnum
        return ith(key)
      end
      nil
    end

    def method_missing(method, *args) # @private
      return self[method] if self[method]
      raise NoMethodError, "undefined method `#{method}' for #{self.inspect}"
    end

  end
end

Version data entries

33 entries across 33 versions & 3 rubygems

Version Path
aptible-resource-1.1.2 lib/hyper_resource/objects.rb
aptible-resource-1.1.1 lib/hyper_resource/objects.rb
aptible-resource-1.1.0 lib/hyper_resource/objects.rb
aptible-resource-1.1.0.pre.0 lib/hyper_resource/objects.rb
aptible-resource-1.0.2 lib/hyper_resource/objects.rb
aptible-resource-1.0.1 lib/hyper_resource/objects.rb
aptible-resource-1.0.0 lib/hyper_resource/objects.rb
aptible-resource-0.5.0 lib/hyper_resource/objects.rb
aptible-resource-0.4.0 lib/hyper_resource/objects.rb
aptible-resource-0.3.8 lib/hyper_resource/objects.rb
aptible-resource-0.3.7 lib/hyper_resource/objects.rb
aptible-resource-0.3.6 lib/hyper_resource/objects.rb
aptible-resource-0.3.5 lib/hyper_resource/objects.rb
aptible-resource-0.3.4 lib/hyper_resource/objects.rb
aptible-resource-0.3.3 lib/hyper_resource/objects.rb
aptible-resource-0.3.2 lib/hyper_resource/objects.rb
aptible-resource-0.3.1 lib/hyper_resource/objects.rb
aptible-resource-0.3.0 lib/hyper_resource/objects.rb
aptible-resource-0.2.9 lib/hyper_resource/objects.rb
aptible-resource-0.2.8 lib/hyper_resource/objects.rb