Class: Safe::SafeStruct

Inherits:
Object
  • Object
show all
Defined in:
lib/safestruct/safe_struct.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.build_class(class_name, **attributes) ⇒ Object Also known as: new



18
19
20
21
22
23
24
25
26
27
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
73
74
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/safestruct/safe_struct.rb', line 18

def self.build_class( class_name, **attributes )

 ## todo/fix:
 ## check if valid class_name MUST start with uppercase letter etc.
 ##  todo/fix: check if constant is undefined in Safe namespace!!!!


  klass = Class.new( SafeStruct ) do
    define_method( :initialize ) do |*args|
      attributes.keys.zip( args ).each do |key, arg|
        instance_variable_set( "@#{key}", arg )
      end
      self  ## note: return reference to self for chaining method calls
    end

    attributes.each do |key,value|
      define_method( key ) do
        instance_variable_get( "@#{key}" )
      end
      if value == false  ## note: for Bool add getter with question mark (e.g. voted? etc.)
        define_method( "#{key}?" ) do
          instance_variable_get( "@#{key}" )
        end
      end
      define_method( "#{key}=" ) do |arg|
        instance_variable_set( "@#{key}", arg )
      end
    end

    alias_method :old_freeze, :freeze   # note: store "old" orginal version of freeze
    define_method( :freeze ) do
      old_freeze    ## same as calling super
      attributes.keys.each do |key|
        instance_variable_get( "@#{key}" ).freeze
      end
      self   # return reference to self
    end

    define_method( :== ) do |other|
       if other.is_a?( klass )
         attributes.keys.all? do |key|
           __send__( key ) == other.__send__( key )
         end
       else
         false
       end
    end
    alias_method :eql?, :==
  end

  ## add self.new too - note: call/forward to "old" orginal self.new of Event (base) class
  klass.define_singleton_method( :new ) do |*args|
    if args.empty?  ## no args - use new_zero and set (initialize) all ivars to zero
      new_zero
    else
      if args.size != attributes.size
        ## check for required args/params - all MUST be passed in!!!
        raise ArgumentError.new( "[SafeStruct] wrong number of arguments for #{name}.new - #{args.size} for #{attributes.size}" )
      end
      old_new( *args )
    end
  end

  klass.define_singleton_method( :new_zero ) do
    ## note: if attribute value is a composite
    ##     use new_zero to create a new instance!!!
    ##     do NOT use the passed in reference!!!!
    values = attributes.values.map do |value|
      ## note: was: use more "generic" check for respond_to?( :new_zero )
      ##  value.is_a?(SafeStruct) ||
      ##  value.is_a?(SafeArray)  ||
      ##  value.is_a?(SafeHash)
      if value.is_a?(String) && value == '0x0000'
        value.dup    ## special case for Address(0) or Address.zero encoded as string for now!!!!
      elsif value.class.respond_to?( :new_zero )
        value.class.new_zero
      else
        ## assume "value" object / semantics (e.g. 0, false, '0x0000' etc.) and pass through
        value
      end
    end
    old_new( *values )
  end


  ## note: use Kernel for "namespacing"
  ##   make all enums convenience converters (always) global
  ##     including uppercase methods (e.g. State(), Color(), etc.) does NOT work otherwise (with other module includes)

  ## add global "Kernel" convenience converter function
  ##  e.g. Vote(0) is same as Vote.convert(0)
  Kernel.class_eval( <<RUBY )
    def #{class_name}( arg )
       #{class_name}.convert( arg )
    end
RUBY

 ## note: use Safe (module) and NOT Object for namespacing
 ##   use include Safe to make all structs global
  Safe.const_set( class_name, klass )   ## returns klass (plus sets global constant class name)
end

.convert(arg) ⇒ Object



8
9
10
11
12
13
14
15
# File 'lib/safestruct/safe_struct.rb', line 8

def self.convert( arg )
  ## note: for now only supports 0 for Vote(0) as Vote.zero shortcut
  if arg == 0
    zero
  else
    raise ArgumentError.new( "[SafeStruct] invalid argument #{arg} - cannot convert to #{name}" )
  end
end

.zeroObject



125
126
127
128
129
130
131
132
# File 'lib/safestruct/safe_struct.rb', line 125

def self.zero
  ## note: freeze return new zero (for "singelton" & "immutable" zero instance)
  ##  todo/fix:
  ##   in build_class add freeze for composite/reference objects
  ##     that is, arrays, hash mappings, structs etc.
  ##   freeze only works for now for "value" objects e.g. integer, bool, etc.
  @zero ||= new_zero.freeze
end

Instance Method Details

#zero?Boolean

Returns:

  • (Boolean)


134
# File 'lib/safestruct/safe_struct.rb', line 134

def zero?() self == self.class.zero; end