Sha256: 180fcce1b32cf84c26b0fbd7850c798eb1c97cf57974b1ef17b0b3f8113772fa

Contents?: true

Size: 1.16 KB

Versions: 10

Compression:

Stored size: 1.16 KB

Contents

require 'json'

module Razorpay
  # Entity class is the base class for all Razorpay objects
  # This saves data in a hash internally, and makes it available
  # via direct methods
  class Entity
    attr_reader :attributes

    def initialize(attributes)
      @attributes = attributes
    end

    # This method fakes attr_reader, but uses
    # the @attributes hash as the source, instead of
    # instance variables
    def method_missing(name)
      if @attributes.key? name.to_s
        @attributes[name.to_s]
      else
        super
      end
    end

    def respond_to_missing?(method_name, include_private = false)
      @attributes.key?(method_name.to_s) || super
    end

    # Public: Convert the Entity object to JSON
    # Returns the JSON representation of the Entity (as a string)
    def to_json(*args)
      @attributes.to_json(*args)
    end

    # Mutates the entity in accordance with
    # the block passed to this construct
    #
    # Used to implement bang methods, by calling
    # the non-bang method in the passed block
    def with_a_bang
      mutated_entity = yield
      @attributes = mutated_entity.attributes
      mutated_entity
    end
  end
end

Version data entries

10 entries across 10 versions & 2 rubygems

Version Path
razorpay-3.2.2 lib/razorpay/entity.rb
razorpay-3.2.1 lib/razorpay/entity.rb
razorpay-3.2.0 lib/razorpay/entity.rb
razorpay-3.1.0 lib/razorpay/entity.rb
razorpay-3.0.1 lib/razorpay/entity.rb
razorruby-3.0.4 lib/razorpay/entity.rb
razorruby-3.0.1 lib/razorpay/entity.rb
razorpay-3.0.0 lib/razorpay/entity.rb
razorpay-2.4.1 lib/razorpay/entity.rb
razorpay-2.4.0 lib/razorpay/entity.rb