Class: EnumerableConstant::Base

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/spree_core/enumerable_constants.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.base(value) ⇒ Object



63
64
65
# File 'lib/spree_core/enumerable_constants.rb', line 63

def self.base(value)
  class_eval("@@#{self.class_var_prefix}_base = value")
end

.base=(value) ⇒ Object



59
60
61
# File 'lib/spree_core/enumerable_constants.rb', line 59

def self.base=(value)
  class_eval("@@#{self.class_var_prefix}_base = value")
end

.class_var_prefixObject



55
56
57
# File 'lib/spree_core/enumerable_constants.rb', line 55

def self.class_var_prefix
  self.name.gsub!('::', '_').underscore
end

.constant(name, value = nil, display_name = nil) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/spree_core/enumerable_constants.rb', line 67

def self.constant(name, value=nil, display_name=nil)
  # puts "name: #{name}"
  # puts "value: #{value}"
  # puts "display_name: #{display_name}"
  class_eval("@@#{self.class_var_prefix}_constants ||= []")
  class_eval("@@#{self.class_var_prefix}_base ||= 0 ")
  if value == nil and class_eval("@@#{self.class_var_prefix}_constants.size == 0")
    value = class_eval("@@#{self.class_var_prefix}_base")
  end
  if value == nil and class_eval("@@#{self.class_var_prefix}_constants.size > 0")
    begin
      value = class_eval("@@#{self.class_var_prefix}_constants.last.value.succ")
    rescue
      value = nil
    end
  end
  # puts "eval : " + "#{name.to_s.underscore.upcase} = #{value}"
  class_eval "#{name.to_s.underscore.upcase} = #{value}"
  # class_eval "A_STUFF = 25"
  # puts "display_name: #{display_name}" if display_name
  class_eval("@@#{self.class_var_prefix}_constants << EnumerableConstant::Tupple.new(name, value, display_name)")
end

.constant_namesObject



109
110
111
112
113
114
115
# File 'lib/spree_core/enumerable_constants.rb', line 109

def self.constant_names
  result = []
  class_eval("@@#{self.class_var_prefix}_constants").map do |tupple|
    result << tupple.name
  end
  result
end

.constantsObject



105
106
107
# File 'lib/spree_core/enumerable_constants.rb', line 105

def self.constants
  class_eval("@@#{self.class_var_prefix}_constants")
end

.from_value(value) ⇒ Object

TODO: add method_missing for class methods so you can do stuff like: :my_constant = 2 :my_constant 2 MY_CONSTANT = 2 MY_CONSTANT 2 def self.method_missing(symbol, *params)

end



98
99
100
101
102
103
# File 'lib/spree_core/enumerable_constants.rb', line 98

def self.from_value value
  self.constants.each do |constant|
    return constant.title if constant.value == value
  end
  nil
end

.namesObject



117
118
119
120
121
122
123
# File 'lib/spree_core/enumerable_constants.rb', line 117

def self.names
  result = []
  class_eval("@@#{self.class_var_prefix}_constants").map do |tupple|
    result << tupple.title
  end
  result
end

.valuesObject



125
126
127
128
129
130
131
# File 'lib/spree_core/enumerable_constants.rb', line 125

def self.values
  result = []
  class_eval("@@#{self.class_var_prefix}_constants").map do |tupple|
    result << tupple.value
  end
  result
end

Instance Method Details

#<=>Object



51
52
53
# File 'lib/spree_core/enumerable_constants.rb', line 51

def <=>

end