Class: ShareProgress::Variant

Inherits:
Object
  • Object
show all
Defined in:
lib/share_progress/variant.rb

Direct Known Subclasses

EmailVariant, FacebookVariant, TwitterVariant

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = nil) ⇒ Variant

Returns a new instance of Variant.



11
12
13
# File 'lib/share_progress/variant.rb', line 11

def initialize(params=nil)
  update_attributes(params) unless params.nil?
end

Instance Attribute Details

#_destroyObject (readonly)

Returns the value of attribute _destroy.



9
10
11
# File 'lib/share_progress/variant.rb', line 9

def _destroy
  @_destroy
end

#buttonObject

Returns the value of attribute button.



8
9
10
# File 'lib/share_progress/variant.rb', line 8

def button
  @button
end

#errorsObject (readonly)

Returns the value of attribute errors.



9
10
11
# File 'lib/share_progress/variant.rb', line 9

def errors
  @errors
end

#idObject

Returns the value of attribute id.



8
9
10
# File 'lib/share_progress/variant.rb', line 8

def id
  @id
end

Instance Method Details

#all_fieldsObject



59
60
61
# File 'lib/share_progress/variant.rb', line 59

def all_fields
  self.class.fields + [:id, :_destroy]
end

#analyticsObject



73
74
75
76
77
78
79
# File 'lib/share_progress/variant.rb', line 73

def analytics
  if @analytics
    @analytics
  else
    raise AnalyticsNotFound
  end
end

#destroyObject



29
30
31
32
33
34
35
# File 'lib/share_progress/variant.rb', line 29

def destroy
  add_error('id', "can't be blank") and return false if id.nil?
  @_destroy = true
  saved = save
  @_destroy = nil
  saved ? self : false
end

#saveObject



19
20
21
22
23
24
25
26
27
# File 'lib/share_progress/variant.rb', line 19

def save
  add_error('button', "can't be blank") and return false if @button.nil?
  add_error('button', "must have an id") and return false if @button.id.nil?
  response = Button.update(id: @button.id, variants: {type => [serialize]})
  return false unless response.is_a? Hash

  @errors = parse_errors(response['errors'])
  (@errors.size == 0)
end

#serializeObject



37
38
39
40
41
42
43
44
# File 'lib/share_progress/variant.rb', line 37

def serialize
  serialized = Hash.new
  all_fields.each do |field|
    value = send(field)
    serialized[field] = value unless (value.nil? and field == :_destroy)
  end
  serialized
end

#set_analytics(analytics:) ⇒ Object



67
68
69
70
71
# File 'lib/share_progress/variant.rb', line 67

def set_analytics(analytics:)
  # Thinking at the moment that it makes sense to keep this as a basic hash, though
  # we might need to extend its functionality down the road.
  @analytics = analytics
end

#to_sObject



15
16
17
# File 'lib/share_progress/variant.rb', line 15

def to_s
  self.serialize.to_json
end

#typeObject



63
64
65
# File 'lib/share_progress/variant.rb', line 63

def type
  self.class.type
end

#update_attributes(params) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/share_progress/variant.rb', line 46

def update_attributes(params)
  if params.is_a? Variant
    params.instance_variables.each do |key|
      instance_variable_set(key, params.instance_variable_get(key))
    end
  elsif params.is_a? Hash
    Utils.symbolize_keys(params).each_pair do |key, value|
      @button = value if key == :button
      instance_variable_set("@#{key}", value) if all_fields.include? key
    end
  end
end