Class: Crowdskout::Components::Field

Inherits:
Component
  • Object
show all
Defined in:
lib/crowdskout/components/profiles/field.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Component

get_value, to_hash_value, #to_json

Instance Attribute Details

#key_nameObject

value can either be a string or a hash



11
12
13
# File 'lib/crowdskout/components/profiles/field.rb', line 11

def key_name
  @key_name
end

#valueObject

value can either be a string or a hash



11
12
13
# File 'lib/crowdskout/components/profiles/field.rb', line 11

def value
  @value
end

Class Method Details

.create(props) ⇒ Field

Factory method to create an Field object from a json string

Parameters:

  • key_name (String)
    • name of the Field

  • value (Hash or String)
    • properties to create object from

Returns:



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/crowdskout/components/profiles/field.rb', line 17

def self.create(props)
  obj = Field.new
  props.each do |key, value|
    obj.key_name = key
    if value.is_a?(Hash)
      obj.value = Value.create(value)
    else
      obj.value = value
    end
  end
  obj
end

Instance Method Details

#to_hashObject

Hash override to generate the correct hash



31
32
33
34
35
# File 'lib/crowdskout/components/profiles/field.rb', line 31

def to_hash
  {
    key_name => (value.to_hash rescue value)
  }
end