Sha256: 343847a32954edbad57e0027340348fa57102a722a5ed9762827fa306fa14483

Contents?: true

Size: 813 Bytes

Versions: 5

Compression:

Stored size: 813 Bytes

Contents

require "representable/option"

# TODO: allow default: -> for hashes, etc.
module Disposable::Twin::Default
  def setup_value_for(dfn, options)
    value = super
    return value unless value.nil?
    default_for(dfn, options)
  end

  def default_for(dfn, options)
    # TODO: introduce Null object in Declarative::Definition#[].
    # dfn[:default].(self) # dfn#[] should return a Null object here if empty.
    return unless dfn[:default]
    dfn[:default].(exec_context: self) # Representable::Option#call
  end

  module ClassMethods
  private
    def build_definition(name, options={}, &block)
      options = options.merge(default: ::Representable::Option(options[:default])) if options.has_key?(:default)
      super
    end
  end

  def self.included(includer)
    includer.extend ClassMethods
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
disposable-0.6.3 lib/disposable/twin/default.rb
disposable-0.6.2 lib/disposable/twin/default.rb
disposable-0.6.1 lib/disposable/twin/default.rb
disposable-0.6.0 lib/disposable/twin/default.rb
disposable-0.5.0 lib/disposable/twin/default.rb