Sha256: 8d43c561464499cb6199b1b274e8434aa5062ec021ba063422cc49fbf03375f8

Contents?: true

Size: 1.07 KB

Versions: 12

Compression:

Stored size: 1.07 KB

Contents

module KB
  class Plan < BaseModel
    include Listable

    kb_api :plan

    def self.attributes_from_response(response)
      response.transform_keys(&:underscore).transform_keys(&:to_sym).slice(*FIELDS)
    end

    private_class_method :attributes_from_response

    STRING_FIELDS = %i[key description plan_name type].freeze
    HASH_FIELDS = %i[benefits price].freeze
    FIELDS = [*STRING_FIELDS, *HASH_FIELDS, :plan_life_in_months, :buyable].freeze

    define_attribute_methods(*FIELDS)

    attribute :plan_life_in_months, :integer
    attribute :buyable, :boolean

    STRING_FIELDS.each do |field|
      attribute field, :string
    end

    HASH_FIELDS.each do |field|
      attribute field
    end

    def save!
      return unless changed?

      run_callbacks :save do
        self.attributes = if @persisted
                            self.class.update key, changes.transform_values(&:last)
                          else
                            self.class.create changes.transform_values(&:last)
                          end

        self
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
barkibu-kb-0.25.0 lib/kb/models/plan.rb
barkibu-kb-0.24.1 lib/kb/models/plan.rb
barkibu-kb-0.24.0 lib/kb/models/plan.rb
barkibu-kb-0.23.0 lib/kb/models/plan.rb
barkibu-kb-0.22.0 lib/kb/models/plan.rb
barkibu-kb-0.21.0 lib/kb/models/plan.rb
barkibu-kb-0.20.0 lib/kb/models/plan.rb
barkibu-kb-0.19.0 lib/kb/models/plan.rb
barkibu-kb-0.18.0 lib/kb/models/plan.rb
barkibu-kb-0.17.0 lib/kb/models/plan.rb
barkibu-kb-0.16.2 lib/kb/models/plan.rb
barkibu-kb-0.16.1 lib/kb/models/plan.rb