Class: Maintain::Value

Inherits:
Object
  • Object
show all
Defined in:
lib/maintain/value.rb

Direct Known Subclasses

BitmaskValue, IntegerValue

Instance Method Summary collapse

Constructor Details

#initialize(state, value = nil) ⇒ Value

Returns a new instance of Value.



36
37
38
39
# File 'lib/maintain/value.rb', line 36

def initialize(state, value = nil)
  @state = state
  @value = state_name_for(value)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object (private)

TODO: Sweet god, this is hideous and needs to be cleaned up!



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
# File 'lib/maintain/value.rb', line 80

def method_missing(method, *args)
  if (method.to_s =~ /^(.+)\?$/)
    check = $1.to_sym
    if @state.states.has_key?(check)
      self.class.class_eval <<-EOC
        def #{method}
          self == #{check.inspect}
        end
      EOC
      # Calling `method` on ourselves fails. Something to do w/subclasses. Meh.
      return self == check.to_sym
    elsif aggregates = @state.aggregates[check]
      self.class.class_eval <<-EOC
        def #{method}
          @state.aggregates[#{check.inspect}].include?(@value)
        end
      EOC
      return aggregates.include?(@value)
    else
      super
    end
  elsif value_for(@value).respond_to?(method)
    value_for(@value).send(method, *args)
  else
    super
  end
end

Instance Method Details

#<(value) ⇒ Object



12
13
14
# File 'lib/maintain/value.rb', line 12

def <(value)
  compare_value < compare_value_for(value)
end

#<=(value) ⇒ Object



16
17
18
# File 'lib/maintain/value.rb', line 16

def <=(value)
  compare_value <= compare_value_for(value)
end

#==(value) ⇒ Object



20
21
22
# File 'lib/maintain/value.rb', line 20

def ==(value)
  compare_value == compare_value_for(value)
end

#===(value) ⇒ Object



24
25
26
# File 'lib/maintain/value.rb', line 24

def ===(value)
  (compare_value == compare_value_for(value)) || super
end

#>(value) ⇒ Object



4
5
6
# File 'lib/maintain/value.rb', line 4

def >(value)
  compare_value > compare_value_for(value)
end

#>=(value) ⇒ Object



8
9
10
# File 'lib/maintain/value.rb', line 8

def >=(value)
  compare_value >= compare_value_for(value)
end

#as_json(options = nil) ⇒ Object



28
29
30
# File 'lib/maintain/value.rb', line 28

def as_json(options = nil)
  @value.to_s
end

#classObject



32
33
34
# File 'lib/maintain/value.rb', line 32

def class
  value.class
end

#inspectObject



41
42
43
# File 'lib/maintain/value.rb', line 41

def inspect
  value.inspect
end

#nameObject



45
46
47
# File 'lib/maintain/value.rb', line 45

def name
  @value.to_s
end

#nil?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/maintain/value.rb', line 49

def nil?
  value.nil?
end

#set_value(value) ⇒ Object



53
54
55
56
# File 'lib/maintain/value.rb', line 53

def set_value(value)
  @compare_value = nil
  @value = state_name_for(value)
end

#to_sObject



58
59
60
# File 'lib/maintain/value.rb', line 58

def to_s
  value.to_s
end

#valueObject



62
63
64
# File 'lib/maintain/value.rb', line 62

def value
  @value
end

#value_for(state) ⇒ Object



66
67
68
# File 'lib/maintain/value.rb', line 66

def value_for(state)
  state_value_for(state, :value)
end