Class: MacroObject

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby-macrodroid/base.rb

Direct Known Subclasses

Action, Trigger

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(h = {}) ⇒ MacroObject

Returns a new instance of MacroObject.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/ruby-macrodroid/base.rb', line 18

def initialize(h={})
  
  $env ||= {}
  
  @attributes = %i(constraint_list is_or_condition is_disabled siguid)
  @h = {constraint_list: [], is_or_condition: false, 
        is_disabled: false, siguid: nil}.merge(h)
  @list = []
  
  # fetch the class name and convert from camelCase to snake_eyes
  @type = self.class.to_s.sub(/Trigger|Action$/,'')\
      .gsub(/\B[A-Z][a-z]/){|x| '_' + x.downcase}\
      .gsub(/[a-z][A-Z]/){|x| x[0] + '_' + x[1].downcase}\
      .downcase.to_sym
  @constraints = []
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



16
17
18
# File 'lib/ruby-macrodroid/base.rb', line 16

def options
  @options
end

#siguidObject (readonly)

Returns the value of attribute siguid.



15
16
17
# File 'lib/ruby-macrodroid/base.rb', line 15

def siguid
  @siguid
end

#typeObject (readonly)

Returns the value of attribute type.



15
16
17
# File 'lib/ruby-macrodroid/base.rb', line 15

def type
  @type
end

Instance Method Details

#to_hObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/ruby-macrodroid/base.rb', line 35

def to_h()

  h = @h

  h2 = h.inject({}) do |r,x|
    puts 'x: ' + x.inspect if @debug
    key, value = x
    puts 'key: ' + key.inspect if @debug
    new_key = key.to_s.gsub(/\w_\w/){|x| x[0] + x[-1].upcase}
    new_key = new_key.prepend 'm_' unless @list.include? new_key
    new_key = 'm_SIGUID' if new_key == 'm_siguid'
    r.merge(new_key => value)
  end
  
  h2.merge('m_classType' => self.class.to_s)

end

#to_s(colour: false) ⇒ Object Also known as: to_summary



57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/ruby-macrodroid/base.rb', line 57

def to_s(colour: false)

  h = @h.clone    
  h.delete :macro
  @s ||= "#<%s %s>" % [self.class, h.inspect]
  operator = @h[:is_or_condition] ? 'OR' : 'AND'
  constraints = @constraints.map \
      {|x| x.to_summary(colour: colour)}.join(" %s " % operator)
  
  @s + constraints
  
end