Class: Crowdskout::Components::Component
- Inherits:
-
Object
- Object
- Crowdskout::Components::Component
- Defined in:
- lib/crowdskout/components/component.rb
Direct Known Subclasses
Attribute, Collection, Field, FieldOptions, Item, Option, Profile, TrackingCode, Value
Class Method Summary collapse
-
.get_value(hsh, key, default = nil) ⇒ String
Get the requested value from a hash, or return the default.
-
.to_hash_value(val) ⇒ Hash
Get the nested value as a hash.
Instance Method Summary collapse
-
#to_hash ⇒ Hash
Return the object as hash.
-
#to_json(val = nil) ⇒ String
Object to a json string.
Class Method Details
.get_value(hsh, key, default = nil) ⇒ String
Get the requested value from a hash, or return the default
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
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_hash ⇒ Hash
Return the object as 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
38 39 40 |
# File 'lib/crowdskout/components/component.rb', line 38 def to_json(val = nil) self.to_hash.to_json end |