Class: ObjectStruct Abstract
- Inherits:
-
OpenStruct
- Object
- OpenStruct
- ObjectStruct
- Defined in:
- lib/object_struct/object_struct.rb,
lib/object_struct/version.rb
Overview
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.
Constant Summary collapse
- VERSION =
'0.0.8'
Class Method Summary collapse
-
.property_type(options) ⇒ Object
Specify the type of a particular property.
Instance Method Summary collapse
-
#each(&blk) ⇒ Object
Delegated to Hash#each.
-
#id ⇒ Object
Specifically provide the object_id.
-
#initialize(data) ⇒ ObjectStruct
constructor
Create a new ObjectStruct.
-
#method_missing(name, *args) ⇒ Object
Provides has_property_name? methods.
- #to_h ⇒ Object
-
#values ⇒ Object
Delegated to Hash#values.
Constructor Details
#initialize(data) ⇒ ObjectStruct
Create a new ObjectStruct.
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.
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.
48 49 50 51 52 53 54 |
# File 'lib/object_struct/object_struct.rb', line 48 def self.property_type() plural = .delete(:plural) property, type = *.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 |
#id ⇒ Object
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_h ⇒ Object
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 |
#values ⇒ Object
Delegated to Hash#values
36 37 38 |
# File 'lib/object_struct/object_struct.rb', line 36 def values to_h.values end |