Sha256: c0ccabf8e928ca29c75b52434798150454fac72d583171181107a00852741b9e

Contents?: true

Size: 646 Bytes

Versions: 3

Compression:

Stored size: 646 Bytes

Contents

module Acfs::Resource::Attributes

  #
  class Base
    attr_reader :options

    def initialize(opts = {})
      @options = opts
      @options.reverse_merge! allow_nil: true
    end

    def nil_allowed?
      options[:allow_nil]
    end

    def blank_allowed?
      options[:allow_blank]
    end

    def default_value
      if options[:default].is_a? Proc
        options[:default]
      else
        cast options[:default]
      end
    end

    def cast(obj)
      return nil if obj.nil? && nil_allowed? || (obj == '' && blank_allowed?)
      cast_type obj
    end

    def cast_type(obj)
      raise NotImplementedError
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
acfs-1.0.0.dev.1.b305 lib/acfs/resource/attributes/base.rb
acfs-1.0.0.dev.1.b298 lib/acfs/resource/attributes/base.rb
acfs-1.0.0.dev.1.b297 lib/acfs/resource/attributes/base.rb