Sha256: d69f3c020f6e15ff4ec0407a6f9c7a2ba8741ecd77f992b727ff1cd4f7d7bdff

Contents?: true

Size: 1.99 KB

Versions: 26

Compression:

Stored size: 1.99 KB

Contents

module Scorpion
  # An injected attribute and it's configuration.
  class Attribute

    # ============================================================================
    # @!group Attributes
    #

    # @!attribute
    # @return [Symbol] the name of the attribute.
      attr_reader :name

    # @!attribute
    # @return [Class,Module,Symbol] contract that describes the desired behavior
    #   of the injected object.
      def contract
        @contract = @contract.constantize if @contract.is_a? String
        @contract
      end

    # @!attribute
    # @return [Array<Symbol>] traits that must match on instances of the {#contract}
      attr_reader :traits

    # @!attribute
    # @return [Boolean] true if the attribute is not immediately required and
    #   will be hunted down on first use.
      def lazy?; @lazy end

    # @!attribute
    # @return [Boolean] true if the attribute should have a public writer.
      def public?; @public end

    # @!attribute
    # @return [Boolean] true if the attribute should have a public writer.
      def private?; @private end

    #
    # @!endgroup Attributes


    def initialize( name, contract, traits = nil, options = {} )
      @name      = name.to_sym
      @contract  = contract
      @traits    = Array( traits ).flatten.freeze
      @trait_set = Set.new( @traits.map{ |t| :"#{t}?" } )
      @lazy      = options.fetch( :lazy, false )
      @public    = options.fetch( :public, false )
      @private   = options.fetch( :private, false )
    end

    def respond_to?( name, include_all = false )
      super || trait_set.include?( name )
    end

    private
      # @return [Set] the set of traits associated with the attribute pre-processed
      #   to include the trait names with a '?' suffix.
        attr_reader :trait_set

      def method_missing( name, *args )
        if is_trait_method?( name )
          trait_set.include? name
        else
          super
        end
      end

      def is_trait_method?( name )
        name[-1] == '?'
      end
  end
end

Version data entries

26 entries across 26 versions & 1 rubygems

Version Path
scorpion-ioc-0.6.2 lib/scorpion/attribute.rb
scorpion-ioc-0.6.1 lib/scorpion/attribute.rb
scorpion-ioc-0.6.0 lib/scorpion/attribute.rb
scorpion-ioc-0.5.23 lib/scorpion/attribute.rb
scorpion-ioc-0.5.22 lib/scorpion/attribute.rb
scorpion-ioc-0.5.21 lib/scorpion/attribute.rb
scorpion-ioc-0.5.20 lib/scorpion/attribute.rb
scorpion-ioc-0.5.19 lib/scorpion/attribute.rb
scorpion-ioc-0.5.18 lib/scorpion/attribute.rb
scorpion-ioc-0.5.17 lib/scorpion/attribute.rb
scorpion-ioc-0.5.16 lib/scorpion/attribute.rb
scorpion-ioc-0.5.15 lib/scorpion/attribute.rb
scorpion-ioc-0.5.14 lib/scorpion/attribute.rb
scorpion-ioc-0.5.13 lib/scorpion/attribute.rb
scorpion-ioc-0.5.12 lib/scorpion/attribute.rb
scorpion-ioc-0.5.11 lib/scorpion/attribute.rb
scorpion-ioc-0.5.10 lib/scorpion/attribute.rb
scorpion-ioc-0.5.9 lib/scorpion/attribute.rb
scorpion-ioc-0.5.8 lib/scorpion/attribute.rb
scorpion-ioc-0.5.7 lib/scorpion/attribute.rb