Module: OpenStructable

Module | OpenStructable
In: | lib/more/facets/ostructable.rb

OpensStructable is a mixin module which can provide OpenStruct behavior to any class or object. OpenStructable allows extention of data objects with arbitrary attributes.

## Usage

      require 'ostructable'

      class Record
        include OpenStructable
      end

      record = Record.new
      record.name    = "John Smith"
      record.age     = 70
      record.pension = 300

      puts record.name     # -> "John Smith"
      puts record.address  # -> nil

## Author(s)

- Thomas Sawyer
- Yukihiro Matsumoto
- Gavin Sinclair (Documentation)

### Methods

== delete_field initialize_copy inspect marshal_dump marshal_load new new_ostruct_member update

### Public Class methods

new(hash=nil)

[Source]

    # File lib/more/facets/ostructable.rb, line 61
      def initialize(hash=nil)
        @__table__ = {}
        if hash
          for k,v in hash
            @__table__[k.to_sym] = v
            new_ostruct_member(k)
          end
        end
      end

### Public Instance methods

==(other)

Compare this object and `other` for equality.

[Source]

    # File lib/more/facets/ostructable.rb, line 154
      def ==(other)
        return false unless(other.kind_of?(OpenStruct))
        return @__table__ == other.table
      end

delete_field(name)

Remove the named field from the object.

[Source]

    # File lib/more/facets/ostructable.rb, line 132
      def delete_field(name)
        @__table__ ||= {}
        @__table__.delete name.to_sym
      end

initialize_copy(orig)

duplicate an OpenStruct object members.

[Source]

    # File lib/more/facets/ostructable.rb, line 72
      def initialize_copy(orig)
        super
        @__table__ = @__table__.dup
      end

inspect()

Returns a string containing a detailed summary of the keys and values.

[Source]

    # File lib/more/facets/ostructable.rb, line 140
      def inspect
        str = "<#{self.class}"
        for k,v in (@__table__ ||= {})
          str << " #{k}=#{v.inspect}"
        end
        str << ">"
      end

marshal_dump()

[Source]

    # File lib/more/facets/ostructable.rb, line 77
      def marshal_dump
        @table
      end

marshal_load(x)

[Source]

    # File lib/more/facets/ostructable.rb, line 80
      def marshal_load(x)
        @table = x
        @table.each_key{|key| new_ostruct_member(key)}
      end

new_ostruct_member(name)

[Source]

    # File lib/more/facets/ostructable.rb, line 85
      def new_ostruct_member(name)
        unless self.respond_to?(name)
          self.instance_eval %{
            def #{name}; @__table__[:#{name}]; end
            def #{name}=(x); @__table__[:#{name}] = x; end
          }
        end
      end

update(hash)

Generate additional attributes and values.

[Source]

    # File lib/more/facets/ostructable.rb, line 97
      def update(hash)
        @__table__ ||= {}
        if hash
          for k,v in hash
            @__table__[k.to_sym] = v
            new_ostruct_member(k)
          end
        end
      end

[Validate]