Class: Crowdskout::Components::Item

Inherits:
Component
  • Object
show all
Defined in:
lib/crowdskout/components/profiles/item.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

#deleteObject

Returns the value of attribute delete.



10
11
12
# File 'lib/crowdskout/components/profiles/item.rb', line 10

def delete
  @delete
end

#fieldsObject

Returns the value of attribute fields.



10
11
12
# File 'lib/crowdskout/components/profiles/item.rb', line 10

def fields
  @fields
end

#idObject

Returns the value of attribute id.



10
11
12
# File 'lib/crowdskout/components/profiles/item.rb', line 10

def id
  @id
end

Class Method Details

.create(props) ⇒ Item

Factory method to create an Item object from a json string

Parameters:

  • props (Hash)
    • properties to create object from

Returns:



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/crowdskout/components/profiles/item.rb', line 15

def self.create(props)
  obj = Item.new
  obj.id = 0
  obj.fields = []
  if props
    props.each do |key, value|
      if ['id'].include? key.downcase
        obj.send("#{key}=", value) if obj.respond_to? key
      else
        # key is the name of the field
        # value is the field's value
        obj.fields << Components::Field.create({key => value})
      end
    end
  end
  obj
end

Instance Method Details

#to_hashObject

Hash override to generate the correct hash



34
35
36
37
38
39
40
# File 'lib/crowdskout/components/profiles/item.rb', line 34

def to_hash
  ret_val = { id: id }
  fields.each do |field|
    ret_val.merge! field.to_hash
  end
  ret_val
end