Sha256: 5ddaa4afe361b368f6f036763a1e5f47b80ca62c8faf1d2432bac1a37cba9ff5

Contents?: true

Size: 1.02 KB

Versions: 3

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 respond_to? :id
          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

3 entries across 3 versions & 1 rubygems

Version Path
blockscore-4.1.2 lib/blockscore/actions/update.rb
blockscore-4.1.1 lib/blockscore/actions/update.rb
blockscore-4.1.0 lib/blockscore/actions/update.rb