Sha256: b76bff926235279181dedd713e7b1ec4fda336ae33aefc34d653c1aa12309119

Contents?: true

Size: 1.62 KB

Versions: 1

Compression:

Stored size: 1.62 KB

Contents

module Clamp
  module Attribute

    class Definition

      def initialize(options)
        if options.has_key?(:attribute_name)
          @attribute_name = options[:attribute_name].to_s
        end
        if options.has_key?(:default)
          @default_value = options[:default]
        end
        if options.has_key?(:environment_variable)
          @environment_variable = options[:environment_variable]
        end
      end

      attr_reader :description, :environment_variable

      def help_rhs
        description + default_description
      end

      def help
        [help_lhs, help_rhs]
      end

      def ivar_name
        "@#{attribute_name}"
      end

      def read_method
        attribute_name
      end

      def default_method
        "default_#{read_method}"
      end

      def write_method
        if multivalued?
          "append_to_#{attribute_name}"
        else
          "#{attribute_name}="
        end
      end

      def multivalued?
        @multivalued
      end

      def required?
        @required
      end

      def attribute_name
        @attribute_name ||= infer_attribute_name
      end

      def default_value
        if defined?(@default_value)
          @default_value
        elsif multivalued?
          []
        end
      end

      private

      def default_description
        default_sources = [
          ("$#{@environment_variable}" if defined?(@environment_variable)),
          (@default_value.inspect if defined?(@default_value))
        ].compact
        return "" if default_sources.empty?
        " (default: " + default_sources.join(", or ") + ")"
      end

    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
clamp-0.6.0 lib/clamp/attribute/definition.rb