Class: Crowdskout::Components::Component

Inherits:
Object
  • Object
show all
Defined in:
lib/crowdskout/components/component.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.get_value(hsh, key, default = nil) ⇒ String

Get the requested value from a hash, or return the default

Parameters:

  • hsh (Hash)
    • the hash to search for the provided hash key

  • key (String)
    • hash key to look for

  • default (String) (defaults to: nil)
    • value to return if the key is not found, default is null

Returns:

  • (String)


47
48
49
# File 'lib/crowdskout/components/component.rb', line 47

def self.get_value(hsh, key, default = nil)
  hsh.has_key?(key) and hsh[key] ? hsh[key] : default
end

.to_hash_value(val) ⇒ Hash

Get the nested value as a hash

Parameters:

  • an (Object)

    object to change into a hash

Returns:

  • (Hash)

    hash of the val



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/crowdskout/components/component.rb', line 24

def self.to_hash_value(val)
  if val.is_a? Crowdskout::Components::Component
    return val.to_hash
  elsif val.is_a? Array
    return val.collect{|subval| Component.to_hash_value(subval) }
  elsif val.is_a? DateTime
    return val.to_s
  else
    return val
  end
end

Instance Method Details

#to_hashHash

Return the object as hash

Returns:

  • (Hash)


13
14
15
16
17
18
19
# File 'lib/crowdskout/components/component.rb', line 13

def to_hash
  hash = Hash.new
  self.instance_variables.collect do |var|
    hash[var.to_s[1..-1]] = self.class.to_hash_value(self.instance_variable_get(var))
  end
  hash
end

#to_json(val = nil) ⇒ String

Object to a json string

Returns:

  • (String)

    the hash of the object to a json string



38
39
40
# File 'lib/crowdskout/components/component.rb', line 38

def to_json(val = nil)
  self.to_hash.to_json
end