Module: Pantheios::Globals::Helpers_

Defined in:
lib/pantheios/globals.rb

Class Method Summary collapse

Class Method Details

.cattr(receiver, name, types, initial_value, **options) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/pantheios/globals.rb', line 94

def self.cattr receiver, name, types, initial_value, **options

  if options[:boolean] && types.nil?

    types = Internals_::TRUTHY_CLASSES
  end

  types = nil if !types.nil? && types.empty?

  receiver.class_eval do

    field_name  =  '@' + name
    get_name  =  name
    get_name  += '?' if options[:boolean]

    instance_variable_set field_name, initial_value

    define_singleton_method(get_name) do

      instance_variable_get field_name
    end

    define_singleton_method(name + '=') do |v|

      if types

        warn "Assigning to #{__method__} with argument of invalid type - #{v.class} given; one of #{types.join(', ')} required" unless types.any? { |c| c === v }
      end

      instance_variable_set field_name, v
    end
  end
end