Module: Ansible::AnsibleValue

Includes:
AnsibleCallback
Included in:
KNX::KNXValue, ZWave::ValueID
Defined in:
lib/ansible/ansible_value.rb

Overview

A base module for Ansible Values, which is the most basic form to declare a protocol-agnostic control endpoint, be it an input (a button or event), or an output (a device such as a relay or dimmer)

Constant Summary collapse

@@AllValues =

singleton array of all known Values

[]
@@AllValuesMutex =
Mutex.new

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from AnsibleCallback

#add_callback, #fire_callback, #remove_callback

Instance Attribute Details

#current_valueObject (readonly)

Returns the value of attribute current_value.



36
37
38
# File 'lib/ansible/ansible_value.rb', line 36

def current_value
  @current_value
end

#flagsObject (readonly)

Returns the value of attribute flags.



38
39
40
# File 'lib/ansible/ansible_value.rb', line 38

def flags
  @flags
end

#last_updateObject (readonly)

Returns the value of attribute last_update.



37
38
39
# File 'lib/ansible/ansible_value.rb', line 37

def last_update
  @last_update
end

#previous_valueObject (readonly)

Returns the value of attribute previous_value.



36
37
38
# File 'lib/ansible/ansible_value.rb', line 36

def previous_value
  @previous_value
end

Class Method Details

.[](filter_hash) ⇒ Object

lookup an AnsibleValue by a filter hash returns an array of matching values



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/ansible/ansible_value.rb', line 69

def AnsibleValue.[](filter_hash)
    result_set = []
    @@AllValuesMutex.synchronize {
        puts "AnsibleValue[] called, filter_hash=#{filter_hash}" if $DEBUG
        @@AllValues.each { |v|
            raise "ooops! @@AllValues contains a non-AnsibleValue!" unless v.is_a?(AnsibleValue)
            if v.matches?(filter_hash) then
                puts "Found a matching value! #{v}" if $DEBUG
                result_set << v
            end
        }
        puts "AnsibleValue[] returns=#{result_set}" if $DEBUG
    }
    return result_set
end

.insert(newvalue) ⇒ Object

add an AnsibleValue to the singleton @@AllValues returns the newvalue, or the existing value (using equality test ==), if found



88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/ansible/ansible_value.rb', line 88

def AnsibleValue.insert(newvalue)
    result = nil
    @@AllValuesMutex.synchronize {
        # check if newvalue is already stored in @@AllValues, find it and return it
        if  (result = @@AllValues.find{|val| newvalue == val}).nil? then
            puts "Adding a new value to @@AllValues (#{newvalue})" if $DEBUG
            @@AllValues << (result = newvalue)
            # get initial state
            newvalue.get 
        end
    }
    return(result)
end

Instance Method Details

#as_canonical_valueObject

value convertion from protocol-specific to its canonical form must be overriden by protocol value subclass



167
168
169
# File 'lib/ansible/ansible_value.rb', line 167

def as_canonical_value()
    raise "#{self.class}.as_canonical_value() must be overriden!!!"
end

#getObject

get a value’s current state returns: the value, if found in eibd’s cache or nil otherwise



105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/ansible/ansible_value.rb', line 105

def get
    return if write_only?
    #
    puts "get() called for #{self.inspect} by:\n\t" + caller[1] if $DEBUG
    #
    fire_callback(:onBeforeGet)
    if read_value() then 
        fire_callback(:onAfterGetSuccess)
    else
        fire_callback(:onAfterGetFail)
        #raise "get value failed for #{self}"
    end
end

#matches?(hash) ⇒ Boolean

return true if a value’s instance variable (whose symbol is iv_symbol) matches a filter value (as a regexp) e.g. value.matches?(:name => /elias/, :telephone => /210/)

Returns:

  • (Boolean)


42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/ansible/ansible_value.rb', line 42

def matches?(hash)
    raise "#{self.class}: AnsibleValue.match? single argument must be a hash.." unless hash.is_a?Hash
    result = true 
    hash.each { |iv_symbol, filter|
        raise "#{self.class}: AnsibleValue.match?(hash)'s keys must be Symbols.." unless iv_symbol.is_a?Symbol
        if respond_to?(iv_symbol) and (val = instance_eval(iv_symbol.to_s)) then
            #puts "match.val(#{iv_symbol}) == #{val.inspect}" if $DEBUG
            result = result & case filter
            # if the filter is a regular expression, use it to match the instance value
            when Regexp then filter.match(val.to_s)
            # if the filter is an array, use set intersection
            when Array then (filter & val).length > 0
            else filter == val
            end
        else
            return false
        end
    }
    return(result)
end

#set(new_val) ⇒ Object

set a value new_val: the new value, must be ruby-castable to OpenZWave’s type system returns: true on success, raises exception on error WARNING: a true return value doesn’t mean the command actually succeeded, it only means that it was queued for delivery to the target node



125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/ansible/ansible_value.rb', line 125

def set(new_val)
    return if read_only?
    #
    puts "set() called for #{self.inspect} by:\n\t" + caller[1] if $DEBUG
    #
    fire_callback(:onBeforeSet)
    if write_value(new_val) then 
        fire_callback(:onSetSuccess)
    else
        fire_callback(:onSetFail)
        raise "set value #{self}: call to #{write_operation} failed!!"
    end
end

#to_protocol_value(v) ⇒ Object

convert a canonical value back to its protocol-specific form must be overriden by protocol value subclass



173
174
175
# File 'lib/ansible/ansible_value.rb', line 173

def to_protocol_value(v)
    raise "#{self.class}.to_protocol_value() must be overriden!!!"
end

#update(newval) ⇒ Object

update internal instance variable representing the current state of the value called by read_value() and write_value()



142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/ansible/ansible_value.rb', line 142

def update(newval)
    validate_ranges() if respond_to?(:validate_ranges)
    unless newval == @current_value then
        @last_update = Time.now
        puts "+++ updating value #{self}, with #{newval.class}:#{newval.inspect}"
        
        # previous value was different, update it and fire onUpdate handler
        @previous_value = @current_value if defined?(@current_value)
        @current_value = newval
        # trigger onUpdate callback, if any
        fire_callback(:onUpdate, nil, newval)
    end
    return(@current_value)
end

#write_valueObject

write value to the protocol



161
162
163
# File 'lib/ansible/ansible_value.rb', line 161

def write_value()
    raise "#{self.class}.write_value() must be overriden!!!"
end