Class: Rhoconnect::MemoryOrm

Inherits:
Object
  • Object
show all
Defined in:
lib/rhoconnect/source.rb

Direct Known Subclasses

Source

Constant Summary collapse

@@model_data =
{}
@@string_fields =
[]
@@integer_fields =
[]

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.validates_presenceObject

Returns the value of attribute validates_presence.



9
10
11
# File 'lib/rhoconnect/source.rb', line 9

def validates_presence
  @validates_presence
end

Instance Attribute Details

#idObject

Returns the value of attribute id.



6
7
8
# File 'lib/rhoconnect/source.rb', line 6

def id
  @id
end

Class Method Details

.class_prefix(classname) ⇒ Object



62
63
64
65
66
67
68
# File 'lib/rhoconnect/source.rb', line 62

def class_prefix(classname)
  classname.to_s.
    sub(%r{(.*::)}, '').
    gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
    gsub(/([a-z\d])([A-Z])/,'\1_\2').
    downcase
end

.create(fields, params) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/rhoconnect/source.rb', line 11

def create(fields,params)
  if self.validates_presence
    self.validates_presence.each do |field|
      raise ArgumentError.new("Missing required field '#{field}'") unless fields[field]
    end
  end
end

.define_fields(string_fields = [], integer_fields = []) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/rhoconnect/source.rb', line 19

def define_fields(string_fields = [], integer_fields = [])
  @@string_fields,@@integer_fields = string_fields,integer_fields
  integer_fields.each do |attrib|
    define_method("#{attrib}=") do |value|
      value = (value.nil?) ? nil : value.to_i
      @@model_data[self.name.to_sym][attrib.to_sym] = value
    end
    define_method("#{attrib}") do
      @@model_data[self.name.to_sym][attrib.to_sym]
    end
  end
  string_fields.each do |attrib|
    define_method("#{attrib}=") do |value|
      attrib = attrib.to_sym
      name = nil
      if attrib == :name
        instance_variable_set(:@name, value)
        name = value
      else
        name = self.name
      end
      @@model_data[name.to_sym] ||= {} # TODO: shouldn't be nil here
      @@model_data[name.to_sym][attrib] = value
    end
    define_method("#{attrib}") do
      @@model_data[instance_variable_get(:@name).to_sym][attrib.to_sym]
    end
    # we have separate methods for this
    @@integer_fields << :poll_interval unless @@integer_fields.include?(:poll_interval)
  end
end

.is_exist?(id) ⇒ Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/rhoconnect/source.rb', line 58

def is_exist?(id)
  !@@model_data[id.to_sym].nil?
end

.validates_presence_of(*names) ⇒ Object



51
52
53
54
55
56
# File 'lib/rhoconnect/source.rb', line 51

def validates_presence_of(*names)
  self.validates_presence ||= []
  names.each do |name|
    self.validates_presence << name
  end
end

Instance Method Details

#to_arrayObject



78
79
80
81
82
83
84
85
86
87
# File 'lib/rhoconnect/source.rb', line 78

def to_array
  res = []
  @@string_fields.each do |field|
    res << {"name" => field, "value" => send(field.to_sym), "type" => "string"}
  end
  @@integer_fields.each do |field|
    res << {"name" => field, "value" => send(field.to_sym), "type" => "integer"}
  end
  res
end

#to_hashObject



89
90
91
92
93
94
95
96
97
98
# File 'lib/rhoconnect/source.rb', line 89

def to_hash
  res = {}
  @@string_fields.each do |field|
   res[field] = send(field.to_sym)
  end
  @@integer_fields.each do |field|
   res[field] = send(field.to_sym)
  end
  res
end

#update_fields(fields) ⇒ Object



71
72
73
74
75
76
# File 'lib/rhoconnect/source.rb', line 71

def update_fields(fields)
  fields.each do |name,value|
    arg = "#{name}=".to_sym
    self.send(arg, value)
  end
end