Module: Glimmer::SWT::StyleConstantizable::ClassMethods

Defined in:
lib/glimmer/swt/style_constantizable.rb

Constant Summary collapse

REGEX_SYMBOL_NEGATIVITY =
/^([^!]+)(!)?$/

Instance Method Summary collapse

Instance Method Details

#[](*symbols) ⇒ Object

Gets constants (e.g. SWT::CONSTANT) where constant is passed in as a lower case symbol



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/glimmer/swt/style_constantizable.rb', line 51

def [](*symbols)
  symbols = symbols.first if symbols.size == 1 && symbols.first.is_a?(Array)
  result = symbols.compact.map do |symbol|
    constant(symbol).tap do |constant_value|
      raise Glimmer::Error, symbol.to_s + error_message_invalid_style unless constant_value.is_a?(Integer)
    end
  end.reduce do |output, constant_value|
    if constant_value < 0
      output & constant_value
    else
      output | constant_value
    end
  end
  result.nil? ? constant_value_none : result
end

#constant(symbol) ⇒ Object

Returns style integer value for passed in symbol or allows passed in object to pass through (e.g. Integer). This makes is convenient to use symbols or actual style integers in Glimmer Does not raise error for invalid values. Just lets them pass as is. (look into [] operator if you want an error raised on invalid values)



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/glimmer/swt/style_constantizable.rb', line 72

def constant(symbol)
  return symbol unless symbol.is_a?(Symbol) || symbol.is_a?(String)
  symbol_string, negative = extract_symbol_string_negativity(symbol)
  swt_constant_symbol = symbol_string.downcase == symbol_string ? symbol_string.upcase.to_sym : symbol_string.to_sym
  bit_value = constant_source_class.const_get(swt_constant_symbol)
  negative ? ~bit_value : bit_value
rescue => e
  begin
#             Glimmer::Config.logger.debug {e.full_message}
    alternative_swt_constant_symbol = constant_source_class.constants.find {|c| c.to_s.upcase == swt_constant_symbol.to_s.upcase}
    bit_value = constant_source_class.const_get(alternative_swt_constant_symbol)
    negative ? ~bit_value : bit_value
  rescue => e
#             Glimmer::Config.logger.debug {e.full_message}
    bit_value = extra_styles[swt_constant_symbol]
    if bit_value
      negative ? ~bit_value : bit_value
    else
      symbol
    end
  end
end

#constant_source_classObject



31
32
33
# File 'lib/glimmer/swt/style_constantizable.rb', line 31

def constant_source_class
  raise 'Not implemented! Mixer must implement!'
end

#constant_value_noneObject



35
36
37
38
# File 'lib/glimmer/swt/style_constantizable.rb', line 35

def constant_value_none
  # TODO instead of raising error try a convention instead like CLASSNAME::NONE by default
  raise 'Not implemented! Mixer must implement!'
end

#constantify_args(args) ⇒ Object



116
117
118
# File 'lib/glimmer/swt/style_constantizable.rb', line 116

def constantify_args(args)
  args.map {|arg| constant(arg)}
end

#deconstruct(integer) ⇒ Object

Deconstructs a style integer into symbols Useful for debugging



122
123
124
125
126
127
128
# File 'lib/glimmer/swt/style_constantizable.rb', line 122

def deconstruct(integer)
  constant_source_class.constants.reduce([]) do |found, c|
    constant_value = constant_source_class.const_get(c) rescue -1
    is_found = constant_value.is_a?(Integer) && (integer & constant_value) == integer
    is_found ? found += [c] : found
  end
end

#error_message_invalid_styleObject



45
46
47
# File 'lib/glimmer/swt/style_constantizable.rb', line 45

def error_message_invalid_style
  " is an invalid #{constant_source_class.name.split(':').last} style! Please choose a style from #{constant_source_class.name} class constants." # TODO parameterize
end

#extra_stylesObject

hash of extra styles (i.e. new style combinations)



41
42
43
# File 'lib/glimmer/swt/style_constantizable.rb', line 41

def extra_styles
  raise 'Not implemented! Mixer must implement!'
end

#extract_symbol_string_negativity(symbol) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
# File 'lib/glimmer/swt/style_constantizable.rb', line 95

def extract_symbol_string_negativity(symbol)
 if symbol.is_a?(Symbol) || symbol.is_a?(String)
   symbol_negativity_match = symbol.to_s.match(REGEX_SYMBOL_NEGATIVITY)
   symbol = symbol_negativity_match[1]
   negative = !!symbol_negativity_match[2]
   [symbol, negative]
 else
   negative = symbol < 0
   [symbol, negative]
 end
end

#has_constant?(symbol) ⇒ Boolean

Returns:

  • (Boolean)


111
112
113
114
# File 'lib/glimmer/swt/style_constantizable.rb', line 111

def has_constant?(symbol)
  return false unless symbol.is_a?(Symbol) || symbol.is_a?(String)
  constant(symbol).is_a?(Integer)
end

#include?(swt_constant, *symbols) ⇒ Boolean

Returns:

  • (Boolean)


140
141
142
# File 'lib/glimmer/swt/style_constantizable.rb', line 140

def include?(swt_constant, *symbols)
  swt_constant & self[symbols] == self[symbols]
end

#negative?(symbol) ⇒ Boolean

Returns:

  • (Boolean)


107
108
109
# File 'lib/glimmer/swt/style_constantizable.rb', line 107

def negative?(symbol)
  extract_symbol_string_negativity(symbol)[1]
end

#reverse_lookup(integer) ⇒ Object

Reverse engineer a style integer into a symbol Useful for debugging



132
133
134
135
136
137
138
# File 'lib/glimmer/swt/style_constantizable.rb', line 132

def reverse_lookup(integer)
  constant_source_class.constants.reduce([]) do |found, c|
    constant_value = constant_source_class.const_get(c) rescue -1
    is_found = constant_value.is_a?(Integer) && integer == constant_value
    is_found ? found += [c] : found
  end
end