class Scorpion::AttributeSet

Attributes

attributes[R]

Public Class Methods

new( attributes = {} ) click to toggle source
# File lib/scorpion/attribute_set.rb, line 7
def initialize( attributes = {} )
  @attributes = attributes
end

Public Instance Methods

[]( key ) click to toggle source
# File lib/scorpion/attribute_set.rb, line 11
def []( key )
  attributes.fetch( key )
end
define( ) { |self| ... } click to toggle source

Defines the food that {Scorpion::Object} will feed on. A food is defined by invoking a method with the desired name passing the contract desired. AttributeSet uses method_missing to dynamically define attributes.

If the block takes an argument, AttributeSet will yield to the block passing itself. If no argument is provided, yield will use the AttributeSet itself as the calling context.

@example With Argument

define do |set|
  set.logger Rails::Logger
end

@example Without Argument

define do
  logger Rails::Logger, :color
end
# File lib/scorpion/attribute_set.rb, line 57
def define( &block )
  return unless block_given?

  @defining_attributes = true
  if block.arity == 1
    yield self
  else
    instance_eval &block
  end

  self
ensure
  @defining_attributes = false
end
define_attribute( name, contract, **options ) click to toggle source

Define a single attribute with the given name that expects food that will satisfy the contract. @param [String] name of the attribute. @param [Class,Module,Symbol] contract that describes the desired behavior

of the injected object.

@return [Attribute] the attribute that was created.

# File lib/scorpion/attribute_set.rb, line 78
def define_attribute( name, contract, **options )
  attributes[name.to_sym] = Attribute.new name, contract, options
end
each( ) { |v| ... } click to toggle source
# File lib/scorpion/attribute_set.rb, line 15
def each( &block )
  attributes.each_value do |v|
    yield v
  end
end
inherit!( other ) click to toggle source

Inherit attribute definitions from another set.

# File lib/scorpion/attribute_set.rb, line 28
def inherit!( other )
  other.each do |attr|
    attributes[attr.name] ||= attr
  end
end
key?( name ) click to toggle source
# File lib/scorpion/attribute_set.rb, line 34
def key?( name )
  attributes.key? name
end
merge( other ) click to toggle source

Merge two sets and create another.

# File lib/scorpion/attribute_set.rb, line 22
def merge( other )
  AttributeSet.new attributes.merge( other.attributes )
end
Also aliased as: |
|( other )
Alias for: merge

Private Instance Methods

method_missing( name, *args ) click to toggle source
Calls superclass method
# File lib/scorpion/attribute_set.rb, line 89
def method_missing( name, *args )
  return super unless @defining_attributes

  if args.length >= 1
    define_attribute name, *args
  else
    super
  end
end