Module: AltStruct::M

Included in:
AltStruct
Defined in:
lib/astruct/module.rb

Constant Summary collapse

ThreadKey =

:nodoc:

:__inspect_astruct_ids__

Instance Attribute Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object

The ‘method_missing()` method catches all non-tabled method calls. The AltStruct object will return two specific errors depending on the call.



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/astruct/module.rb', line 73

def method_missing(method, *args)
  name = method.to_s
  case
  when !name.include?('=')
    # This is to catch non-assignment methods
    message = "undefined method `#{name}' for #{self}"
    raise NoMethodError, message, caller(1)
  when args.size != 1
    # This is to catch the []= method
    message = "wrong number of arguments (#{args.size} for 1)"
    raise ArgumentError, message, caller(1)
  else
    __new_field__ name.chomp!('='), args.first
  end
end

Instance Attribute Details

#tableObject (readonly)

Returns the value of attribute table.



4
5
6
# File 'lib/astruct/module.rb', line 4

def table
  @table
end

Instance Method Details

#==(object) ⇒ Object



89
90
91
92
93
94
95
# File 'lib/astruct/module.rb', line 89

def ==(object)
  if object.respond_to? :table
    table == object.table
  else
    false
  end
end

#__delete_field__(key) ⇒ Object Also known as: delete_field, delete

The delete_field() method removes a key/value pair on the @table and on the singleton class. It also mimics the Hash#delete method.



62
63
64
65
66
# File 'lib/astruct/module.rb', line 62

def __delete_field__(key)
  __singleton_class__.send :remove_method, key
  __singleton_class__.send :remove_method, :"#{key}="
  @table.delete key
end

#__dump__(*keys) ⇒ Object Also known as: marshal_dump, dump, to_hash

The ‘dump()` takes the table and out puts in it’s natural hash format. In addition you can pass along a specific set of keys to dump.



47
48
49
# File 'lib/astruct/module.rb', line 47

def __dump__(*keys)
  keys.empty? ? table : __dump_specific__(keys)
end

#__inspect__Object Also known as: inspect, to_s



54
55
56
# File 'lib/astruct/module.rb', line 54

def __inspect__
  "#<#{self.class}#{__dump_inspect__}>"
end

#__load__(pairs) ⇒ Object Also known as: marshal_load, load, merge, merge!

This is the ‘load()` method, which works like initialize in that it will create new fields for each pair passed. Notice that it also is a double-underbar method, making it really hard to overwrite. It also mimics the behavior of a Hash#merge and Hash#merge!



34
35
36
37
38
# File 'lib/astruct/module.rb', line 34

def __load__(pairs)
  for key, value in pairs
    __new_field__ key, value
  end unless pairs.empty?
end

#freezeObject Also known as: __freeze__



97
98
99
100
# File 'lib/astruct/module.rb', line 97

def freeze
  super
  @table.freeze
end

#initialize(pairs = {}) ⇒ Object

Create a new field for each of the key/value pairs passed. By default the resulting OpenStruct object will have no attributes. If no pairs are passed avoid any work.

require 'ostruct'
hash = { "country" => "Australia", :population => 20_000_000 }
data = OpenStruct.new hash

p data # => <OpenStruct country="Australia" population=20000000>

If you happen to be inheriting then you can define your own your @table.



22
23
24
25
26
27
# File 'lib/astruct/module.rb', line 22

def initialize(pairs = {})
  @table ||= {}
  for key, value in pairs
    __new_field__ key, value
  end unless pairs.empty?
end