Sha256: 9865c9ecb83435ca029a8b79320e3f3691d39e50ae4f99f5110898938ecb427f

Contents?: true

Size: 1.3 KB

Versions: 10

Compression:

Stored size: 1.3 KB

Contents

module NNEClient

  # ResultAttributes provides an attributes class method. When calling attributes
  # with a list of keys from the @hash instance variable a method are defined for
  # each key returning the value from the @hash. As such it requires a @hash
  # instance variable on the class that extends the module. Additionally it
  # ignores any value in the @hash that is itself a Hash. This is due to the way
  # the NNE SOAP API works together with Savon.
  #
  # Example:
  #
  #   class Foo
  #     extends NNEClient::ResultAttributes
  #     def initialize
  #       @hash = { :foo => 1, :bar => { :a => 2 }}
  #       attributes :foo, :bar
  #     end
  #   end
  #
  #   Foo.new.foo
  #     => 1
  #   Foo.new.bar
  #     => nil
  #
  # @!visibility private
  module ResultAttributes

    # Create a method for each key listed in attrs
    def attributes(*attrs)
      attrs.each do |attribute|
        define_method(attribute) do
          @hash[attribute] unless @hash[attribute].kind_of?(Hash)
        end
      end
    end

    # Create a method that converts to float for each key listed in attrs
    def float_attributes(*attrs)
      attrs.each do |attribute|
        define_method(attribute) do
          @hash[attribute].to_f unless @hash[attribute].kind_of?(Hash)
        end
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
nne_client-0.0.11 lib/nne_client/result_attributes.rb
nne_client-0.0.10 lib/nne_client/result_attributes.rb
nne_client-0.0.9 lib/nne_client/result_attributes.rb
nne_client-0.0.8 lib/nne_client/result_attributes.rb
nne_client-0.0.7 lib/nne_client/result_attributes.rb
nne_client-0.0.6 lib/nne_client/result_attributes.rb
nne_client-0.0.5 lib/nne_client/result_attributes.rb
nne_client-0.0.4 lib/nne_client/result_attributes.rb
nne_client-0.0.3 lib/nne_client/result_attributes.rb
nne_client-0.0.2 lib/nne_client/result_attributes.rb