Module: AttributedObject::Base::ClassExtension

Included in:
Coerce::ClassExtension, Strict::ClassExtension
Defined in:
lib/attributed_object/base.rb,
lib/attributed_object/types/hash_of.rb,
lib/attributed_object/types/array_of.rb

Instance Method Summary collapse

Instance Method Details

#ArrayOf(type_info) ⇒ Object



22
23
24
# File 'lib/attributed_object/types/array_of.rb', line 22

def ArrayOf(type_info)
  AttributedObject::Types::ArrayOf.new(type_info)
end

#attribute(attr_name, type_info = Unset, default: Unset, disallow: Unset) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/attributed_object/base.rb', line 27

def attribute(attr_name, type_info = Unset, default: Unset, disallow: Unset)
  if default == Unset
    default_to = attributed_object_options.fetch(:default_to)

    if default_to != Unset
      default = default_to.is_a?(TypeDefaults) ? default_to.fetch(type_info) : default_to
    end
  end

  _attributed_object_check_type_supported!(type_info)

  attribute_defs[attr_name] = {
    type_info: type_info,
    default: default,
    disallow: disallow,
  }

  attr_writer attr_name
  attr_reader attr_name
end

#attribute_defsObject



20
21
22
23
24
25
# File 'lib/attributed_object/base.rb', line 20

def attribute_defs
  return @attribute_defs if @attribute_defs
  parent_defs = {}
  parent_defs = self.superclass.attribute_defs if self.superclass.respond_to?(:attribute_defs)
  @attribute_defs = parent_defs.clone
end

#attributed_object(options = {}) ⇒ Object



4
5
6
# File 'lib/attributed_object/base.rb', line 4

def attributed_object(options={})
  @attributed_object_options = attributed_object_options.merge(options)
end

#attributed_object_optionsObject



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/attributed_object/base.rb', line 8

def attributed_object_options
  return @attributed_object_options if !@attributed_object_options.nil?

  parent_ops = self.superclass.respond_to?(:attributed_object_options) ? self.superclass.attributed_object_options : {}

  @attributed_object_options = {
    default_to: Unset,
    ignore_extra_keys: false,
    coerce_blanks_to_nil: false
  }.merge(parent_ops)
end

#HashOf(key_type_info, value_type_info) ⇒ Object



25
26
27
# File 'lib/attributed_object/types/hash_of.rb', line 25

def HashOf(key_type_info, value_type_info)
  AttributedObject::Types::HashOf.new(key_type_info, value_type_info)
end