Class: Crowdskout::Components::Collection

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

#itemsObject

Returns the value of attribute items.



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

def items
  @items
end

#key_nameObject

Returns the value of attribute key_name.



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

def key_name
  @key_name
end

Class Method Details

.create(props) ⇒ Collection

Factory method to create an Collection object from a json string

Parameters:

  • key_name (String)
    • name of the collection

  • items (Array)
    • properties to create object from

Returns:



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

def self.create(props)
  obj = Collection.new
  obj.items = []
  props.each do |key, value|
    obj.key_name = key
    if value.is_a?(Hash) || value.is_a?(Array)
      value.each do |collection|
        obj.items << Components::Item.create(collection)
      end
    else
      obj.items << Components::Item.create({ key => value })
    end
  end
  obj
end

Instance Method Details

#to_hashObject

Hash override to generate the correct hash



33
34
35
36
37
# File 'lib/crowdskout/components/profiles/collection.rb', line 33

def to_hash
  {
    key_name => items.collect(&:to_hash)
  }
end