class ShareProgress::Variant
Attributes
_destroy[R]
errors[R]
id[RW]
Public Class Methods
new(params=nil)
click to toggle source
# File lib/share_progress/variant.rb, line 11 def initialize(params=nil) update_attributes(params) unless params.nil? end
Public Instance Methods
all_fields()
click to toggle source
# File lib/share_progress/variant.rb, line 59 def all_fields self.class.fields + [:id, :_destroy] end
analytics()
click to toggle source
# File lib/share_progress/variant.rb, line 73 def analytics if @analytics @analytics else raise AnalyticsNotFound end end
destroy()
click to toggle source
# 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
save()
click to toggle source
# 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
serialize()
click to toggle source
# 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:)
click to toggle source
# 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_s()
click to toggle source
# File lib/share_progress/variant.rb, line 15 def to_s self.serialize.to_json end
type()
click to toggle source
# File lib/share_progress/variant.rb, line 63 def type self.class.type end
update_attributes(params)
click to toggle source
# 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
Private Instance Methods
add_error(field, message)
click to toggle source
# File lib/share_progress/variant.rb, line 83 def add_error(field, message) @errors ||= {} @errors[field.to_s] ||= [] @errors[field.to_s].push(message) end
parse_errors(errors)
click to toggle source
# File lib/share_progress/variant.rb, line 89 def parse_errors(errors) begin messages = errors['variants'][0] return {} if messages.nil? rescue NoMethodError, KeyError, IndexError return {} end # it concatenates the field and the error into a full message output = {} messages.map{|msg| msg.split(' ', 2)}.each do |field, error| output[field] ||= [] output[field].push error end output end