Class: ElVfs::Model

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Validations
Defined in:
lib/el_finder_api/el_vfs/model.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Model

Returns a new instance of Model.



7
8
9
# File 'lib/el_finder_api/el_vfs/model.rb', line 7

def initialize(params={})
  self.attributes = params
end

Class Method Details

.options(*args) ⇒ Object



17
18
19
20
# File 'lib/el_finder_api/el_vfs/model.rb', line 17

def self.options(*args)
  self.options = args.map(&:to_s)
  attr_accessor *args
end

Instance Method Details

#attributes=(params) ⇒ Object



11
12
13
14
15
# File 'lib/el_finder_api/el_vfs/model.rb', line 11

def attributes=(params)
  params.each do |attribute, value|
    send("#{attribute}=", value) if respond_to?("#{attribute}=") && value.present?
  end
end

#el_hashObject



22
23
24
# File 'lib/el_finder_api/el_vfs/model.rb', line 22

def el_hash
  methods.select{|m| method(m).owner == self.class}.inject({}){|h,m| h[m] = to_el_hash(send(m)); h}
end

#to_el_hash(object) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/el_finder_api/el_vfs/model.rb', line 26

def to_el_hash(object)
  if object.respond_to?(:el_hash)
    object = object.el_hash.tap do | hash |
      hash.each do | key, value |
      hash[key] = to_el_hash(value)
      end
    end
  elsif object.is_a? Array
    object.map!{|o| to_el_hash(o)}
  end
  object
end