module ArtirixDataModels::Model::Attributes::ClassMethods

Public Instance Methods

all_defined_attributes() click to toggle source

deal with model inheritance

# File lib/artirix_data_models/model.rb, line 167
def all_defined_attributes
  attribute_config.all_attributes
end
attribute(*attributes) click to toggle source
# File lib/artirix_data_models/model.rb, line 153
def attribute(*attributes)
  options = attributes.extract_options!
  attributes.each { |attribute| _define_attribute attribute, options }
end
attribute_config() click to toggle source
# File lib/artirix_data_models/model.rb, line 158
def attribute_config
  @attribute_config ||= AttributeConfig.new
end
defined_attributes() click to toggle source
# File lib/artirix_data_models/model.rb, line 162
def defined_attributes
  attribute_config.attributes
end
inherited(child_class) click to toggle source
# File lib/artirix_data_models/model.rb, line 171
def inherited(child_class)
  child_class.attribute_config.parent_attribute_config = attribute_config
end
writer_visibility() click to toggle source
# File lib/artirix_data_models/model.rb, line 175
def writer_visibility
  @writer_visibility ||= :private
end
writer_visibility=(visibility) click to toggle source
# File lib/artirix_data_models/model.rb, line 179
def writer_visibility=(visibility)
  raise InvalidArgumentError, "Invalid visibility #{visibility.inspect}" unless [:public, :private, :protected].include? visibility
  @writer_visibility = visibility
end

Private Instance Methods

_define_attribute(attribute, options) click to toggle source
# File lib/artirix_data_models/model.rb, line 185
def _define_attribute(attribute, options)
  at = attribute.to_sym
  _define_getter(at, options)
  _define_presence(at, options)
  _define_writer(at, options)

  attribute_config.add_attribute at
end
_define_getter(attribute, options) click to toggle source
# File lib/artirix_data_models/model.rb, line 222
def _define_getter(attribute, options)
  skip_option = Array(options.fetch(:skip, []))
  return nil if skip_option.include?(:reader) || skip_option.include?(:getter)

  variable_name = "@#{attribute}"
  dir_get_name  = Attributes.direct_getter_method_name(attribute)
  reader        = attribute.to_s

  define_method dir_get_name do
    instance_variable_get variable_name
  end

  define_method(reader) do
    val = send dir_get_name
    if val.nil?
      nil_attribute(attribute)
    else
      val
    end
  end

  vis = options.fetch(:reader_visibility, :public)

  if vis == :private
    private reader
  elsif vis == :protected
    protected reader
  end

end
_define_presence(attribute, options) click to toggle source
# File lib/artirix_data_models/model.rb, line 212
def _define_presence(attribute, options)
  skip_option = Array(options.fetch(:skip, []))
  return nil if skip_option.include?(:presence) || skip_option.include?(:predicate)

  presence_method = "#{attribute}?"
  define_method presence_method do
    send(attribute).present?
  end
end
_define_writer(attribute, options) click to toggle source
# File lib/artirix_data_models/model.rb, line 194
def _define_writer(attribute, options)
  skip_option = Array(options.fetch(:skip, []))
  return nil if skip_option.include?(:writer) || skip_option.include?(:setter)

  vis = options.fetch(:writer_visibility, writer_visibility)

  attr_writer attribute
  writer = "#{attribute}="

  if vis == :private
    private writer
  elsif vis == :protected
    protected writer
  end

  writer
end