Class: Tutor::Attributes::Attribute

Inherits:
Object
  • Object
show all
Includes:
Default, Type
Defined in:
lib/tutor/attributes/attribute.rb

Instance Attribute Summary collapse

Attributes included from Type

#nullable, #type

Attributes included from Default

#default

Instance Method Summary collapse

Methods included from Type

#check_value_type!, #valid_value_type?

Methods included from Default

#default_value_for

Constructor Details

#initialize(name, options = {}) ⇒ Attribute

Returns a new instance of Attribute.



15
16
17
18
19
20
21
22
23
24
# File 'lib/tutor/attributes/attribute.rb', line 15

def initialize(name, options = {})
  self.name = name.to_sym
  self.type = options[:type]
  self.nullable = options.has_key?(:nullable) ? options[:nullable] : true
  self.default = options[:default]
  self.override = options.has_key?(:override) ? options[:override] : false
  self.alias = options[:alias]
  self.get = options.has_key?(:get) ? options[:get] && Tutor::Attributes::Block.new(&options[:get]) : get_default
  self.set = options.has_key?(:set) ? options[:set] && Tutor::Attributes::Block.new(&options[:set]) : set_default
end

Instance Attribute Details

#aliasObject

Returns the value of attribute alias.



6
7
8
# File 'lib/tutor/attributes/attribute.rb', line 6

def alias
  @alias
end

#getObject

Returns the value of attribute get.



6
7
8
# File 'lib/tutor/attributes/attribute.rb', line 6

def get
  @get
end

#nameObject

Returns the value of attribute name.



6
7
8
# File 'lib/tutor/attributes/attribute.rb', line 6

def name
  @name
end

#overrideObject

Returns the value of attribute override.



6
7
8
# File 'lib/tutor/attributes/attribute.rb', line 6

def override
  @override
end

#reader_methodObject

Returns the value of attribute reader_method.



6
7
8
# File 'lib/tutor/attributes/attribute.rb', line 6

def reader_method
  @reader_method
end

#setObject

Returns the value of attribute set.



6
7
8
# File 'lib/tutor/attributes/attribute.rb', line 6

def set
  @set
end

#writer_methodObject

Returns the value of attribute writer_method.



6
7
8
# File 'lib/tutor/attributes/attribute.rb', line 6

def writer_method
  @writer_method
end

Instance Method Details

#add_attribute_methods(klass) ⇒ Object



26
27
28
29
30
31
# File 'lib/tutor/attributes/attribute.rb', line 26

def add_attribute_methods(klass)
  [
    add_reader_method(klass),
    add_writer_method(klass)
  ].compact
end

#add_reader_method(klass) ⇒ Object



33
34
35
36
37
38
39
40
# File 'lib/tutor/attributes/attribute.rb', line 33

def add_reader_method(klass)
  return if !self.get
  self.reader_method = add_method(
    klass,
    self.name,
    body: self.get
  )
end

#add_writer_method(klass) ⇒ Object



42
43
44
45
46
47
48
49
50
# File 'lib/tutor/attributes/attribute.rb', line 42

def add_writer_method(klass)
  return if !self.set
  self.writer_method = add_method(
    klass,
    "#{self.name}=".to_sym,
    pre_execute: lambda { |object, value| self.check_value_type!(value) },
    body: self.set
  )
end