class Tutor::Attributes::Attribute

Attributes

alias[RW]
get[RW]
name[RW]
override[RW]
reader_method[RW]
set[RW]
writer_method[RW]

Public Class Methods

new(name, options = {}) click to toggle source
# 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

Public Instance Methods

add_attribute_methods(klass) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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

Private Instance Methods

add_method(klass, name, options = {}, &block) click to toggle source
# File lib/tutor/attributes/attribute.rb, line 54
def add_method(klass, name, options = {}, &block)
  Tutor::Attributes::Method.new(name, options, &block).tap { |m| m.define_on(klass, override: self.override) }
end
get_default() click to toggle source
# File lib/tutor/attributes/attribute.rb, line 58
def get_default
  if self.alias
    Tutor::Attributes::Blocks::Alias::Reader.new(self.alias)
  else
    Tutor::Attributes::Blocks::Instance::Reader.new(self.name)
  end
end
set_default() click to toggle source
# File lib/tutor/attributes/attribute.rb, line 66
def set_default
  if self.alias
    Tutor::Attributes::Blocks::Alias::Writer.new(self.alias)
  else
    Tutor::Attributes::Blocks::Instance::Writer.new(self.name)
  end
end