Class: Hashery::PropertyHash

### Home Classes Methods

### In Files

- lib/hashery/property_hash.rb

### Parent

Hashery::CRUDHash

### Methods

- ::new
- ::properties
- ::property
- #property
- #store
- #store!

### Files

- LICENSE.txt
- NOTICE.txt

### Class/Module Index

Quicksearch

- Hashery
- Hashery::Association
- Hashery::Association::Kernel
- Hashery::CRUDHash
- Hashery::CastingHash
- Hashery::Dictionary
- Hashery::FuzzyHash
- Hashery::IniHash
- Hashery::KeyHash
- Hashery::LRUHash
- Hashery::LinkedList
- Hashery::LinkedList::Node
- Hashery::OpenCascade
- Hashery::OpenHash
- Hashery::OrderedHash
- Hashery::PathHash
- Hashery::PropertyHash
- Hashery::QueryHash
- Hashery::Stash
- Hashery::StaticHash
- Hash

No matching classes.

# Hashery::PropertyHash

A PropertyHash is the same as a regular Hash except it strictly limits the allowed keys.

There are two ways to use it.

1) As an object in itself.

    h = PropertyHash.new(:a=>1, :b=>2)
    h[:a]        #=> 1
    h[:a] = 3
    h[:a]        #=> 3

But if we try to set key that was not fixed, then we will get an error.

    h[:x] = 5    #=> ArgumentError

2) As a superclass.

    class MyPropertyHash < PropertyHash
      property :a, :default => 1
      property :b, :default => 2
    end

    h = MyPropertyHash.new
    h[:a]        #=> 1
    h[:a] = 3
    h[:a]        #=> 3

Again, if we try to set key that was not fixed, then we will get an error.

    h[:x] = 5    #=> ArgumentError

### Public Class Methods

new(properties={}, &default_proc) click to toggle source

Initialize new instance of PropertyHash.

properties - [Hash] Priming properties with default values, or

    if it doesn't respond to #each_pair, a default object.

default_proc - [Proc] Procedure for default value of properties

    for properties without specific defaults.

    # File lib/hashery/property_hash.rb, line 76
    def initialize(properties={}, &default_proc)
      if properties.respond_to?(:each_pair)
        super(&default_proc)
        fixed = self.class.properties.merge(properties)
        fixed.each_pair do |key, value|
          store!(key, value)
        end
      else
        super(*[properties].compact, &default_proc)
      end
    end

properties() click to toggle source

Get a list of properties with default values.

Returns [Hash] of properties and their default values.

    # File lib/hashery/property_hash.rb, line 44
    def self.properties
      @properties ||= (
        parent = ancestors[1]
        if parent.respond_to?(:properties)
          parent.properties
        else
          {}
        end
      )
    end

property(key, opts={}) click to toggle source

Define a property.

key - Name of property. opts - Property options.

    :default - Default value of property.

Returns default value.

    # File lib/hashery/property_hash.rb, line 64
    def self.property(key, opts={})
      properties[key] = opts[:default]
    end

### Public Instance Methods

property(key, opts={}) click to toggle source

Create a new property, on-the-fly.

key - Name of property. opts - Property options.

    :default - Default value of property.

Returns default value.

    # File lib/hashery/property_hash.rb, line 101
    def property(key, opts={})
      if opts[:default]
        store!(key, opts[:default])
      else
        store!(key, retrieve(key))
      end
    end

store(key, value) click to toggle source

Store key value pair, ensuring the key is a valid property first.

key - The `Object` to act as indexing key. value - The `Object` to associate with key.

Raises ArgumentError if key is not a valid property.

Returns `value`.

    # File lib/hashery/property_hash.rb, line 119
    def store(key, value)
      assert_key!(key)
      super(key, value)
    end

Also aliased as: store!
store!(key, value) click to toggle source

Alias original store method and make private.

Alias for: store

[Validate]

Generated with the Darkfish Rdoc Generator 2.