Module: BitmaskEnum::EvalScripts Private

Defined in:
lib/bitmask_enum/eval_scripts.rb

Overview

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Code strings to be templated and evaled to create methods

Class Method Summary collapse

Class Method Details

.attribute_validation(attribute, flags_size) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Code for validation method checking attribute is within valid values

Parameters:

  • attribute (String)

    Name of the attribute

  • flags_size (Integer)

    Number of defined flags

Returns:

  • (String)

    Code string to be evaled



12
13
14
15
16
17
18
19
# File 'lib/bitmask_enum/eval_scripts.rb', line 12

def attribute_validation(attribute, flags_size)
  %(
    validates(                                          # validates(
      :#{attribute},                                    #   :attribs,
      numericality: { less_than: (1 << #{flags_size}) } #   numericality: { less_than: (1 << 3) }
    )                                                   # )
  )
end

.class_flag_values(attribute, flags) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Code for class attribute values method: ‘#attribs=` The return value of the method will be an array of symbols representing all defined flags

Parameters:

  • attribute (String)

    Name of the attribute

  • flags (String)

    Array of symbols representing all defined flags

Returns:

  • (String)

    The code string to be evaled



129
130
131
132
133
134
135
# File 'lib/bitmask_enum/eval_scripts.rb', line 129

def class_flag_values(attribute, flags)
  %(
    def self.#{attribute}  # def self.attribs
      #{flags}             #   [:flag]
    end                    # end
  )
end

.dynamic_scope(scope_name, attribute, flags_and_values, bitwise_operator) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Code for methods dynamically scoping by flags: ‘.attribs_enabled`, `.attribs_disabled`

Parameters:

  • scope_name (String)

    Name of the scope

  • attribute (String)

    Name of the attribute

  • flags_and_values (Array)

    Array of arrays, first being the flag, second being the enum values for the flag

  • bitwise_operator (String)

    Bitwise operator used to combine the enum value arrays

Returns:

  • (String)

    Code string to be evaled



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/bitmask_enum/eval_scripts.rb', line 52

def dynamic_scope(scope_name, attribute, flags_and_values, bitwise_operator)
  %(
scope :#{scope_name}, ->(flags) do                             # scope :attribs_disabled, ->(flags) do
  enum_values = {                                              #   enum_values = {
    #{flags_and_values.map { |f, v| "#{f}: #{v}" }.join(', ')} #     flag: [1,3], flag2: [2]
  }                                                            #   }
                                                         #
  where('#{attribute}' => Array(flags).map do |flag|           #   where('attribs' => Array(flags).map do |flag|
    flag_values = enum_values[flag.to_sym]                     #     flag_values = enum_values[flag.to_sym]
    if flag_values.nil?                                        #     if flag_index.nil?
raise(                                                   #       raise(
  ArgumentError,                                         #         ArgumentError,
  "Invalid flag \#{flag} for #{attribute}"               #         "Invalid flag \#{flag} for attribs"
)                                                        #       )
    end                                                        #     end
    flag_values                                                #     flag_values
  end.reduce(&:#{bitwise_operator}))                           #   end.map(&:|))
end                                                            # end
  )
end

.flag_getter(attribute, flag_array_contents) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Code for attribute getter method: ‘#attribs` The return value of the method will be an array of symbols representing the enabled flags

Parameters:

  • attribute (String)

    Name of the attribute

  • flag_array_contents (String)

    Contents of the array which provides the enabled flags

Returns:

  • (String)

    The code string to be evaled



90
91
92
93
94
95
96
# File 'lib/bitmask_enum/eval_scripts.rb', line 90

def flag_getter(attribute, flag_array_contents)
  %(
    def #{attribute}                    # def attribs
      [#{flag_array_contents}].compact  #   [((self['attribs'] || 0) & 1).positive? ? :flag : nil].compact
    end                                 # end
  )
end

.flag_method(method_name, method_code) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Code for methods checking and setting flags: ‘#flag?`, `#flag!`, `#enable_flag!`, `#disable_flag!`

Parameters:

  • method_name (String)

    Name of the method

  • method_code (String)

    Code contents of the method

Returns:

  • (String)

    Code string to be evaled



25
26
27
28
29
30
31
# File 'lib/bitmask_enum/eval_scripts.rb', line 25

def flag_method(method_name, method_code)
  %(
    def #{method_name}  # def flag!
      #{method_code}    #   update!('attribs' => self['attribs'] ^ 1)
    end                 # end
  )
end

.flag_scope(scope_name, attribute, values_for_bitmask) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Code for methods scoping by flag: ‘.flag_enabled`, `.flag_disabled`

Parameters:

  • scope_name (String)

    Name of the scope

  • attribute (String)

    Name of the attribute

  • values_for_bitmask (Array)

    Array of integers for which the flag would be enabled/disabled

Returns:

  • (String)

    Code string to be evaled



38
39
40
41
42
43
44
# File 'lib/bitmask_enum/eval_scripts.rb', line 38

def flag_scope(scope_name, attribute, values_for_bitmask)
  %(
    scope :#{scope_name}, -> do                       # scope :flag_disabled, -> do
      where('#{attribute}' => #{values_for_bitmask})  #   where('attribs' => [0, 2, 4])
    end                                               # end
  )
end

.flag_setter(method_name, attribute) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Code for attribute setter method: ‘#attribs=` The method will take an integer, a symbol representing a flag or an array of symbols representing flags

Parameters:

  • method_name (String)

    Name of the method (the attribute name with an =)

  • attribute (String)

    Name of the attribute

Returns:

  • (String)

    The code string to be evaled



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/bitmask_enum/eval_scripts.rb', line 103

def flag_setter(method_name, attribute)
  %(
def #{method_name}(value)                                     # def attribs=(value)
  if value.is_a?(Integer)                                     #   if value.is_a?(Integer)
    super                                                     #     super
  else                                                        #   else
    super(Array(value).reduce(0) do |acc, flag|               #     super(Array(value).reduce(0) do |acc, x|
flag_index = self.class.#{attribute}.index(flag.to_sym) #       flag_index = self.class.attribs.index(flag.to_sym)
if flag_index.nil?                                      #       if flag_index.nil?
  raise(                                                #         raise(
    ArgumentError,                                      #           ArgumentError,
    "Invalid flag \#{flag} for #{attribute}"            #           "Invalid flag \#{flag} for attribs"
  )                                                     #         )
end                                                     #       end
acc | (1 << flag_index)                                 #       acc | (1 << flag_index)
    end)                                                      #     end)
  end                                                         #   end
end                                                           # end
  )
end

.flag_settings(method_name, flag_hash_contents) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Code for method returning hash of flags with their boolean setting: ‘#attribs_settings`

Parameters:

  • method_name (String)

    Name of the method

  • flag_hash_contents (String)

    Contents of the hash which provides the flag settings

Returns:

  • (String)

    The code string to be evaled



77
78
79
80
81
82
83
# File 'lib/bitmask_enum/eval_scripts.rb', line 77

def flag_settings(method_name, flag_hash_contents)
  %(
    def #{method_name}          # def attribs_settings
      { #{flag_hash_contents} } #   { flag: (self['attribs'] & 1).positive? }
    end                         # end
  )
end