Sha256: d274fb66c1a00e84d6810a77c75d6984a08999fb120e672380cb1dc675975e4c

Contents?: true

Size: 1.02 KB

Versions: 2

Compression:

Stored size: 1.02 KB

Contents

module BlockScore
  module Actions
    # Public: Contains the :save instance method, which updates the
    # object with the BlockScore API to persist the changes.
    #
    # Examples
    #
    #  class Foo
    #    include BlockScore::Actions::Update
    #  end
    #
    #  foo = Foo.new
    #  foo.name_first = 'John'
    #  foo.save
    #  # => true
    module Update
      extend Forwardable

      # Attributes which will not change once the object is created.
      PERSISTENT_ATTRIBUTES = [
        :id,
        :object,
        :created_at,
        :updated_at,
        :livemode
      ]

      def_delegators 'self.class', :endpoint, :patch

      def save!
        if persisted?
          patch("#{endpoint}/#{id}", filter_params)
          true
        else
          super
        end
      end

      # Filters out the non-updateable params.
      def filter_params
        # Cannot %i syntax, not introduced until Ruby 2.0.0
        attributes.reject { |key, _| PERSISTENT_ATTRIBUTES.include?(key) }
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
blockscore-4.2.1 lib/blockscore/actions/update.rb
blockscore-4.2.0 lib/blockscore/actions/update.rb