Class: Compostr::CustomFieldValue

Inherits:
Object
  • Object
show all
Defined in:
lib/compostr/custom_field_value.rb

Overview

Describe a Custom Field Value with optionally an id (corresponding to the WordPress data).

Direct Known Subclasses

DeleteCustomFieldValue

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, key, value) ⇒ CustomFieldValue

Returns a new instance of CustomFieldValue.



6
7
8
9
10
# File 'lib/compostr/custom_field_value.rb', line 6

def initialize id, key, value
  @id    = id
  @key   = key
  @value = value
end

Instance Attribute Details

#idObject

Returns the value of attribute id.



4
5
6
# File 'lib/compostr/custom_field_value.rb', line 4

def id
  @id
end

#keyObject

Returns the value of attribute key.



4
5
6
# File 'lib/compostr/custom_field_value.rb', line 4

def key
  @key
end

#valueObject

Returns the value of attribute value.



4
5
6
# File 'lib/compostr/custom_field_value.rb', line 4

def value
  @value
end

Instance Method Details

#to_hashObject

Convert to hash that is consumable by RubyPress/Wordpress. Important that neither key nor value are present for custom field values that should be deleted in wordpress instance.



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/compostr/custom_field_value.rb', line 15

def to_hash
  if @id
    hsh = { id: @id }
    hsh[:key]   = @key if @key
    hsh[:value] = @value if @value
    hsh
  else
    hsh = {}
    hsh[:key]   = @key if @key
    hsh[:value] = @value if @value
    hsh
  end
end