Class: Scorpion::AttributeSet
- Inherits:
-
Object
- Object
- Scorpion::AttributeSet
- Includes:
- Enumerable
- Defined in:
- lib/scorpion/attribute_set.rb
Instance Method Summary collapse
- #[](key) ⇒ Object
-
#define(&block) ⇒ Object
Defines the food that Object will feed on.
-
#define_attribute(name, contract, **options) ⇒ Attribute
Define a single attribute with the given name that expects food that will satisfy the contract.
- #each(&block) ⇒ Object
-
#inherit!(other) ⇒ Object
Inherit attribute definitions from another set.
-
#initialize(attributes = {}) ⇒ AttributeSet
constructor
A new instance of AttributeSet.
- #key?(name) ⇒ Boolean
-
#merge(other) ⇒ Object
(also: #|)
Merge two sets and create another.
Constructor Details
#initialize(attributes = {}) ⇒ AttributeSet
Returns a new instance of AttributeSet.
7 8 9 |
# File 'lib/scorpion/attribute_set.rb', line 7 def initialize( attributes = {} ) @attributes = attributes end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args) ⇒ Object (private)
89 90 91 92 93 94 95 96 97 |
# 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 |
Instance Method Details
#[](key) ⇒ Object
11 12 13 |
# File 'lib/scorpion/attribute_set.rb', line 11 def []( key ) attributes.fetch( key ) end |
#define(&block) ⇒ Object
Defines the food that 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.
57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# 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) ⇒ Attribute
Define a single attribute with the given name that expects food that will satisfy the contract.
78 79 80 |
# File 'lib/scorpion/attribute_set.rb', line 78 def define_attribute( name, contract, ** ) attributes[name.to_sym] = Attribute.new name, contract, end |
#each(&block) ⇒ Object
15 16 17 18 19 |
# File 'lib/scorpion/attribute_set.rb', line 15 def each( &block ) attributes.each_value do |v| yield v end end |
#inherit!(other) ⇒ Object
Inherit attribute definitions from another set.
28 29 30 31 32 |
# File 'lib/scorpion/attribute_set.rb', line 28 def inherit!( other ) other.each do |attr| attributes[attr.name] ||= attr end end |
#key?(name) ⇒ Boolean
34 35 36 |
# File 'lib/scorpion/attribute_set.rb', line 34 def key?( name ) attributes.key? name end |
#merge(other) ⇒ Object Also known as: |
Merge two sets and create another.
22 23 24 |
# File 'lib/scorpion/attribute_set.rb', line 22 def merge( other ) AttributeSet.new attributes.merge( other.attributes ) end |