Class: ObjectStruct Abstract

Inherits:
OpenStruct
  • Object
show all
Defined in:
lib/object_struct/object_struct.rb,
lib/object_struct/version.rb

Overview

This class is abstract.

You probably want to subclass ObjectStruct, to reflect your business entities. See the README for example use.

Inherit from ObjectStruct to allow object-style accesses to a hash parameter passed to initialize. Properties are lazy-loaded.

See Also:

  • OpenStruct

Constant Summary collapse

VERSION =
'0.0.8'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ ObjectStruct

Create a new ObjectStruct.

Parameters:

  • data (Hash)

    a Hash representing your data. Keys are used to identify the type of the property.



60
61
62
63
64
65
66
67
# File 'lib/object_struct/object_struct.rb', line 60

def initialize(data)
	@table = Hash.new {|h, k| raise NoMethodError, "undefined property #{k} for #{self}"}
	data = {'data' => data} unless data.respond_to? :each
	for k,v in data
		@table[k.to_sym] = self.class.value_maybe_promise(k, v)
		new_ostruct_member(k)
	end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object

Provides has_property_name? methods. Intercepts messages matching /^has_+?$/. If the method matches this regex, ObjectStruct will see if it responds to such a property. If that property responds to a count method, it will furthermore make sure #count is greater than zero.

Returns:

  • the property, if it exists, nil otherwise



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

def method_missing(name, *args)
	if name =~ /^has_(\w+)\?$/
		if respond_to? (field = $1.to_sym)
			return @table.has_key?(:id) if field == :id # necessary, since all ruby objects have id method
			result = (send field)
			if (result.kind_of? Enumerable) && result.respond_to?(:count)
				(result.count > 0)
			else
				result ? true : false
			end
		end
	else
		super(name, *args)
	end
end

Class Method Details

.property_type(options) ⇒ Object

Specify the type of a particular property.

Parameters:

  • options (Hash)

    The options hash. Pass any :property_name => ClassName to enforce that type.

Options Hash (options):

  • :plural (Boolean) — default: false

    Whether the property is plural, e.g. an Array of the class specified. (Yes, this means that if you have a property named :plural, you cannot specify its type.)



48
49
50
51
52
53
54
# File 'lib/object_struct/object_struct.rb', line 48

def self.property_type(options)
	plural = options.delete(:plural)
	property, type = *options.first
	class_name = plural ? property.to_s.classify : property.to_s.camelize
	klass = (name.split('::')[0..-2]).join('::').constantize rescue Object
	klass.const_set class_name, Class.new(type)
end

Instance Method Details

#each(&blk) ⇒ Object

Delegated to Hash#each



31
32
33
# File 'lib/object_struct/object_struct.rb', line 31

def each &blk
	to_h.each &blk
end

#idObject

Specifically provide the object_id. If your objects have IDs, choose a more descriptive name.



92
93
94
# File 'lib/object_struct/object_struct.rb', line 92

def id
	object_id
end

#to_hObject



96
97
98
99
100
# File 'lib/object_struct/object_struct.rb', line 96

def to_h
	result = {}
	@table.each {|name, value| result[name.to_s] = value}
	result
end

#valuesObject

Delegated to Hash#values



36
37
38
# File 'lib/object_struct/object_struct.rb', line 36

def values
	to_h.values
end