class Crowdskout::Components::Component

Public Class Methods

get_value(hsh, key, default = nil) click to toggle source

Get the requested value from a hash, or return the default @param [Hash] hsh - the hash to search for the provided hash key @param [String] key - hash key to look for @param [String] default - value to return if the key is not found, default is null @return [String]

# 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) click to toggle source

Get the nested value as a hash @param [Object] an object to change into a hash @return [Hash] hash of the val

# 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

Public Instance Methods

to_hash() click to toggle source

Return the object as hash @return [Hash]

# 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) click to toggle source

Object to a json string @return [String] the hash of the object to a json string

# File lib/crowdskout/components/component.rb, line 38
def to_json(val = nil)
  self.to_hash.to_json
end