Sha256: 9342a768aba4be813507668f561028a50f257fd7f23830476bd580ef3c5ca652

Contents?: true

Size: 785 Bytes

Versions: 7

Compression:

Stored size: 785 Bytes

Contents

require "declarative/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].(self)
  end

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

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

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
disposable-0.4.7 lib/disposable/twin/default.rb
disposable-0.4.6 lib/disposable/twin/default.rb
disposable-0.4.5 lib/disposable/twin/default.rb
disposable-0.4.4 lib/disposable/twin/default.rb
disposable-0.4.3 lib/disposable/twin/default.rb
disposable-0.4.2 lib/disposable/twin/default.rb
disposable-0.4.1 lib/disposable/twin/default.rb