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



154
155
156
157
158
159
160
# File 'lib/bitmask_enum/eval_scripts.rb', line 154

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



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/bitmask_enum/eval_scripts.rb', line 77

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



115
116
117
118
119
120
121
# File 'lib/bitmask_enum/eval_scripts.rb', line 115

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



63
64
65
66
67
68
69
# File 'lib/bitmask_enum/eval_scripts.rb', line 63

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



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/bitmask_enum/eval_scripts.rb', line 128

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



102
103
104
105
106
107
108
# File 'lib/bitmask_enum/eval_scripts.rb', line 102

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

.flag_test_method(method_name, attribute, test_method, boolean) ⇒ 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 testing flags on a model: ‘#any_attribs_enabled?`, `#all_attribs_disabled?` The methods will take a symbol representing a flag or an array of symbols representing flags

Parameters:

  • method_name (String)

    Name of the method

  • attribute (String)

    Name of the attribute

  • test_method (String)

    Name of the method used to test the provided flags against the model

  • boolean (Boolean)

    Boolean to test flags against

Returns:

  • (String)

    Code string to be evaled



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/bitmask_enum/eval_scripts.rb', line 40

def flag_test_method(method_name, attribute, test_method, boolean)
  bool_text = boolean == true ? 'true' : 'false'

  %(
    def #{method_name}(flags)                              # def any_attribs_enabled?(flags)
      Array(flags).#{test_method} do |flag|                #   Array(flags).any? do |flag|
        if self.class.#{attribute}.index(flag.to_sym).nil? #     if self.class.attribs.index(flag.to_sym).nil?
          raise(                                           #       raise(
            ArgumentError,                                 #         ArgumentError,
            "Invalid flag \#{flag} for #{attribute}"       #         "Invalid flag \#{flag} for attribs"
          )                                                #       )
        end                                                #     end
        #{attribute}_settings[flag.to_sym] == #{bool_text} #     attribs_settings[flag.to_sym] == true
      end                                                  #   end
    end                                                    # end
  )
end