Sha256: b148b6ac6988c4889b00c46411bf470ff13010d25cc73d10fb0be3d5b21500ea

Contents?: true

Size: 1.97 KB

Versions: 5

Compression:

Stored size: 1.97 KB

Contents

#!/usr/bin/env ruby

require "bundler/setup"
require "usable"
require "usable/struct"
require "byebug"
require "irb"

# You can add fixtures and/or initialization code here to make experimenting
# with your gem easier. You can also use a different console, if you like.

# (If you use this, don't forget to add pry to your Gemfile!)
# require "pry"
# Pry.start

module VersionMixin
  def save_version
    "Saving up to #{usables.max_versions} versions to #{usables.table_name}"
  end

  def destroy_version
    "Deleting versions from #{usables.table_name}"
  end
end

module Mixin
  extend Usable
  config.language = :en
  config do
    country 'US'
    state 'Hawaii'
    census({
             population: 1_400_00,
             daily_visitors: 218_150
           })
  end

  def name
    "defined by Mixin"
  end
end

class Model
  extend Usable

  usable VersionMixin, only: :save_version do
    max_versions 10
    table_name 'custom_versions'
  end

  def save
    usable_method(:save_version).call
  end
end

module PersistenceOverride
  def save
    'nope'
  end
end

module Nested
  module Extension
    def go
      'going'
    end
  end
end

class Example
  extend Usable
  usable Mixin
  usable VersionMixin do
    max_versions 10
    model { Model }
  end
  usable Nested::Extension
end

Model.usable PersistenceOverride, method: 'prepend'

def run_tests(subject)
  if subject.usables.instance_variable_get(:@lazy_loads).to_a != [:model]
    puts "Test @lazy_loads FAILED! Expected: #{[:model]}, Actual: #{subject.usables.instance_variable_get(:@lazy_loads)}"
  end
  if subject.usables.model != Model
    puts "Test #model FAILED! Expected: #{Model}, Actual: #{subject.usables.model}"
  end
  if subject.usables.max_versions != 10
    puts "Test #max_version FAILED! Expected: #{10}, Actual: #{subject.usables.max_versions}"
  end
  if subject.usables.model.new.save != 'nope'
    puts "Test #save FAILED! Expected: 'nope', Actual: #{subject.usables.model.new.save}"
  end
end

run_tests Example

IRB.start

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
usable-3.6.2 bin/console
usable-3.6.1 bin/console
usable-3.6.0 bin/console
usable-3.5.0 bin/console
usable-3.4.0 bin/console