Method: ObjectStruct.property_type

Defined in:
lib/object_struct/object_struct.rb

.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