Module: NRSER::Meta::ClassAttrs::ClassMethods

Defined in:
lib/nrser/meta/class_attrs.rb

Overview

Class methods to extend the receiver with when NRSER::Meta::ClassAttrs is included.

Instance Method Summary collapse

Instance Method Details

#class_attr_accessor(attr, default: NRSER::NO_ARG, default_from: NRSER::NO_ARG) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/nrser/meta/class_attrs.rb', line 75

def class_attr_accessor attr,
                        default: NRSER::NO_ARG,
                        default_from: NRSER::NO_ARG
  
  var_name = "@#{ attr }".to_sym
  
  singleton_class.class_eval do
    define_method(attr) do |*args|
      case args.length
      when 0
        instance_variable_lookup  var_name,
                                  default: default,
                                  default_from: default_from
      when 1
        instance_variable_set var_name, args[0]
      else
        raise ArgumentError.new NRSER.squish "wrong number of arguments\n(given \#{ args.length }, expected 0 or 1)\n"
      end
    end
    
    define_method("#{ attr }=") do |value|
      instance_variable_set var_name, value
    end
  end
end

#class_attr_writer(attr) ⇒ Object



104
105
106
107
108
109
110
111
112
# File 'lib/nrser/meta/class_attrs.rb', line 104

def class_attr_writer attr
  var_name = "@#{ attr }".to_sym
  
  singleton_class.class_eval do
    define_method("#{ attr }=") do |value|
      instance_variable_set var_name, value
    end
  end
end

#instance_variable_lookup(name, default: NRSER::NO_ARG, default_from: NRSER::NO_ARG) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/nrser/meta/class_attrs.rb', line 28

def instance_variable_lookup  name,
                              default: NRSER::NO_ARG,
                              default_from: NRSER::NO_ARG
  
  # If it's defined here on self, return that
  if instance_variable_defined? name
    return instance_variable_get name
  end
  
  # Ok, now to need to look for it.
  
  # See if the superclass has the lookup method
  if superclass.respond_to? :instance_variable_lookup
    # It does. See what we get from that. We create a new object to use
    # as a flag and assign it to `default` so we can tell if the search
    # failed.
    not_found = Object.new
    result = superclass.instance_variable_lookup name, default: not_found
    
    # If we found something, return it.
    return result unless result == not_found
    
  end # if superclass.respond_to? :instance_variable_lookup
  
  # Ok, nothing was found.
  
  # See if we can use a default...
  if default != NRSER::NO_ARG || default_from != NRSER::NO_ARG
    # We can use a default.
    # `default` takes precedence.
    if default != NRSER::NO_ARG
      default
    else
      send default_from
    end
    
  else
    # Nope, we can't, raise.
    raise NoMethodError.new NRSER.squish "\#{ name.inspect } is not defined anywhere in the class hierarchy\n"

  end # if we have a default value / else
  
end