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.



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

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



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

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.



60
61
62
63
64
# File 'lib/astruct/module.rb', line 60

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.



45
46
47
# File 'lib/astruct/module.rb', line 45

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

#__inspect__Object Also known as: inspect, to_s



52
53
54
# File 'lib/astruct/module.rb', line 52

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!



32
33
34
35
36
# File 'lib/astruct/module.rb', line 32

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

#freezeObject



95
96
97
98
# File 'lib/astruct/module.rb', line 95

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.



20
21
22
23
24
25
# File 'lib/astruct/module.rb', line 20

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