Module: Nylas::Model::Attributable::ClassMethods

Defined in:
lib/nylas/model/attributable.rb

Overview

Methods to call when tweaking Attributable classes

Instance Method Summary collapse

Instance Method Details

#attribute(name, type_name, read_only: false, default: nil) ⇒ Object

rubocop:enable Naming/PredicateName



49
50
51
52
53
54
55
56
# File 'lib/nylas/model/attributable.rb', line 49

def attribute(name, type_name, read_only: false, default: nil)
  attribute_definitions[name] = AttributeDefinition.new(
    type_name: type_name,
    read_only: read_only,
    default: default
  )
  define_accessors(name)
end

#attribute_definitionsObject



83
84
85
# File 'lib/nylas/model/attributable.rb', line 83

def attribute_definitions
  @attribute_definitions ||= Registry.new
end

#define_accessors(name) ⇒ Object



58
59
60
61
62
63
64
65
66
# File 'lib/nylas/model/attributable.rb', line 58

def define_accessors(name)
  define_method :"#{name}" do
    attributes[name]
  end

  define_method :"#{name}=" do |value|
    attributes[name] = value
  end
end

#has_n_of_attribute(name, type_name, read_only: false, default: []) ⇒ Object

rubocop:disable Naming/PredicateName



39
40
41
42
43
44
45
46
# File 'lib/nylas/model/attributable.rb', line 39

def has_n_of_attribute(name, type_name, read_only: false, default: [])
  attribute_definitions[name] = ListAttributeDefinition.new(
    type_name: type_name,
    read_only: read_only,
    default: default
  )
  define_accessors(name)
end

#inherit_attributesObject

Allows a class to inherit parent’s attributes



69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/nylas/model/attributable.rb', line 69

def inherit_attributes
  return if superclass.nil?

  parent_attributes = superclass.attribute_definitions
  parent_attributes.each do |parent_attribute|
    name = parent_attribute[0]
    attr = parent_attribute[1]
    next if attribute_definitions.key?(name)

    attribute_definitions[name] = attr
    define_accessors(name)
  end
end