module Micro::Attributes::Macros

Constants

RaiseKindError

Public Instance Methods

__attribute_assign(key, can_overwrite, opt) click to toggle source
# File lib/micro/attributes/macros.rb, line 134
def __attribute_assign(key, can_overwrite, opt)
  name = __attribute_key_check__(__attribute_key_transform__(key))

  Options.check(opt)

  has_attribute = attribute?(name, true)

  visibility_index = Options.visibility_index(opt)

  __attribute_reader(name, visibility_index) unless has_attribute

  if can_overwrite || !has_attribute
    __attributes_data__[name] = __attributes_data_to_assign(name, opt, visibility_index)
  end

  __call_after_attribute_assign__(name, opt)
end
__attribute_key_check__(value) click to toggle source
# File lib/micro/attributes/macros.rb, line 84
def __attribute_key_check__(value)
  value
end
__attribute_key_transform__(value) click to toggle source
# File lib/micro/attributes/macros.rb, line 88
def __attribute_key_transform__(value)
  value.to_s
end
__attribute_reader(name, visibility_index) click to toggle source
# File lib/micro/attributes/macros.rb, line 101
def __attribute_reader(name, visibility_index)
  attr_reader(name)

  __attributes.add(name)
  __attributes_groups[visibility_index] << name

  private(name) if Options.private?(visibility_index)
  protected(name) if Options.protected?(visibility_index)
end
__attributes() click to toggle source
# File lib/micro/attributes/macros.rb, line 78
def __attributes; __attributes_groups[Options::ALL]; end
__attributes_data__() click to toggle source

NOTE: can't be renamed! It is used by u-case v4.

# File lib/micro/attributes/macros.rb, line 97
def __attributes_data__
  @__attributes_data__ ||= {}
end
__attributes_data_to_assign(name, opt, visibility_index) click to toggle source
# File lib/micro/attributes/macros.rb, line 119
def __attributes_data_to_assign(name, opt, visibility_index)
  hasnt_default = !opt.key?(:default)

  default = hasnt_default ? __attributes_required_add(name, opt, hasnt_default) : opt[:default]

  [
    default,
    Options.for_accept(opt),
    opt[:freeze],
    Options.visibility_name_from_index(visibility_index)
  ]
end
__attributes_groups() click to toggle source
# File lib/micro/attributes/macros.rb, line 68
def __attributes_groups
  @__attributes_groups ||= [
    Set.new, # all
    Set.new, # public
    [],      # private
    [],      # protected
    Set.new, # required
  ]
end
__attributes_keys_transform__(hash) click to toggle source
# File lib/micro/attributes/macros.rb, line 92
def __attributes_keys_transform__(hash)
  Utils::Hashes.stringify_keys(hash)
end
__attributes_public() click to toggle source
# File lib/micro/attributes/macros.rb, line 80
def __attributes_public; __attributes_groups[Options::PUBLIC]; end
__attributes_required__() click to toggle source
# File lib/micro/attributes/macros.rb, line 82
def __attributes_required__; __attributes_groups[Options::REQUIRED]; end
__attributes_required_add(name, opt, hasnt_default) click to toggle source
# File lib/micro/attributes/macros.rb, line 111
def __attributes_required_add(name, opt, hasnt_default)
  if opt[:required] || (attributes_are_all_required? && hasnt_default)
    __attributes_required__.add(name)
  end

  nil
end
__attributes_set_after_inherit__(arg) click to toggle source

NOTE: can't be renamed! It is used by u-case v4.

# File lib/micro/attributes/macros.rb, line 153
def __attributes_set_after_inherit__(arg)
  arg.each do |key, val|
    opt = {}

    default = val[0]
    accept_key, accept_val = val[1]
    freeze, visibility = val[2], val[3]

    opt[:default] = default if default
    opt[accept_key] = accept_val if accept_key
    opt[:freeze] = freeze if freeze
    opt[visibility] = true if visibility != :public

    __attribute_assign(key, true, opt || Kind::Empty::HASH)
  end
end
__call_after_attribute_assign__(attr_name, options) click to toggle source
# File lib/micro/attributes/macros.rb, line 132
def __call_after_attribute_assign__(attr_name, options); end
attribute(name, options = Kind::Empty::HASH) click to toggle source
# File lib/micro/attributes/macros.rb, line 178
def attribute(name, options = Kind::Empty::HASH)
  __attribute_assign(name, false, options)
end
attribute?(name, include_all = false) click to toggle source
# File lib/micro/attributes/macros.rb, line 170
def attribute?(name, include_all = false)
  key = __attribute_key_transform__(name)

  return __attributes.member?(key) if include_all

  __attributes_public.member?(key)
end
attributes(*args) click to toggle source
# File lib/micro/attributes/macros.rb, line 192
def attributes(*args)
  return __attributes.to_a if args.empty?

  args.flatten!

  options =
    args.size > 1 && args.last.is_a?(::Hash) ? args.pop : Kind::Empty::HASH

  args.each do |arg|
    if arg.is_a?(String) || arg.is_a?(Symbol)
      __attribute_assign(arg, false, options)
    else
      RaiseKindError.call('String/Symbol'.freeze, arg)
    end
  end
end
attributes_access() click to toggle source
# File lib/micro/attributes/macros.rb, line 64
def attributes_access
  :indifferent
end
attributes_are_all_required?() click to toggle source
# File lib/micro/attributes/macros.rb, line 60
def attributes_are_all_required?
  false
end
attributes_by_visibility() click to toggle source
# File lib/micro/attributes/macros.rb, line 209
def attributes_by_visibility
  {
    public: __attributes_groups[Options::PUBLIC].to_a,
    private: __attributes_groups[Options::PRIVATE].dup,
    protected: __attributes_groups[Options::PROTECTED].dup
  }
end