Sha256: b8eaa9caf50648f5bece9514d901fb50022074494d445bde11c8bd57c8dd5f17

Contents?: true

Size: 1 KB

Versions: 1

Compression:

Stored size: 1 KB

Contents

require "virtus"
require "ice_nine"

module Fortnox
  module API
    class Entity

      extend Forwardable
      include Virtus.model

      attr_accessor :unsaved

      def initialize( hash = {} )
        @unsaved = hash.delete( :unsaved ){ true }
        super
        IceNine.deep_freeze( self )
      end

      def update( hash )
        attributes = self.to_hash.merge( hash )

        return self if attributes == self.to_hash

        self.class.new( attributes )
      end

      # Generic comparison, by value, use .eql? or .equal? for object identity.
      def ==( other )
        return false unless other.is_a? self.class
        self.to_hash == other.to_hash
      end

      def to_json
        hash = attribute_set.each_with_object({}) do |attribute, hash|
          name = attribute.options[ :name ]
          key = attribute.options[ :json_name ] || attribute_name_to_json( name )
          value = attributes[name]
          hash[ key ] = value
        end
        hash.to_json
      end

    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
fortnox-api-0.0.2 lib/fortnox/api/entity.rb