Skip to content

Commit

Permalink
refactor expandable attribute support and maintain expanded attribute…
Browse files Browse the repository at this point in the history
…s across API ops
  • Loading branch information
mkdynamic committed Jul 19, 2014
1 parent 565cf55 commit 8497a20
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 38 deletions.
3 changes: 2 additions & 1 deletion lib/delighted/operations/update.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ def self.included(klass)
end

def save(client = Delighted.shared_client)
params = Utils.hash_without_key(attributes, :id)
params = Utils.hash_without_key(to_hash, :id)
params = params.merge(:expand => expanded_attribute_names) unless expanded_attribute_names.empty?
params = Utils.serialize_values(params)
json = client.put_json(self.class.path(id), params)
self.class.new(json)
Expand Down
47 changes: 34 additions & 13 deletions lib/delighted/resource.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
module Delighted
class Resource
class << self
def path=(path)
@path = path
end

def path
@path
end
attr_accessor :path
attr_writer :singleton_resource, :expandable_attributes

def singleton_resource=(singleton_resource)
@singleton_resource = singleton_resource
def expandable_attributes
@expandable_attributes ||= {}
end

def singleton_resource?
Expand All @@ -28,20 +23,46 @@ def initialize(attributes = {})
build_from_attributes(attributes)
end

# Attributes used for serialization
def to_hash
attributes
serialized_attributes = attributes.dup

self.class.expandable_attributes.each_pair.select do |attribute_name, expanded_class|
if expanded_class === attributes[attribute_name]
serialized_attributes[attribute_name] = serialized_attributes[attribute_name].id
end
end

serialized_attributes
end
alias_method :to_h, :to_hash

protected
private

def expanded_attribute_names
names = Set.new

self.class.expandable_attributes.each_pair.select do |attribute_name, expanded_class|
if expanded_class === attributes[attribute_name]
names << attribute_name
end
end

names
end

def build_from_attributes(attributes)
@attributes = Utils.hash_without_key(attributes, :id)

self.class.expandable_attributes.each_pair do |attribute_name, expanded_class|
if Hash === @attributes[attribute_name]
@attributes[attribute_name] = expanded_class.new(@attributes.delete(attribute_name))
end
end

define_attribute_accessors(@attributes.keys)
end

private

def define_id_reader
Utils.eigenclass(self).instance_eval do
attr_reader :id
Expand Down
21 changes: 1 addition & 20 deletions lib/delighted/resources/survey_response.rb
Original file line number Diff line number Diff line change
@@ -1,30 +1,11 @@
module Delighted
class SurveyResponse < Resource
self.path = "/survey_responses"
self.expandable_attributes = { :person => Person }

include Operations::Create
include Operations::All
include Operations::Update
include Operations::Retrieve

def to_hash
if Person === attributes[:person]
Utils.hash_without_key(attributes, :person)
else
attributes
end
end

protected

def build_from_attributes(attributes)
attributes_dup = attributes.dup

if Hash === attributes_dup[:person]
attributes_dup[:person] = Person.new(attributes_dup.delete(:person))
end

super(attributes_dup)
end
end
end
2 changes: 1 addition & 1 deletion lib/delighted/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def self.serialize_values(object)
memo[k] = serialize_values(v)
memo
}
when Array
when Array, Set
object.map { |v| serialize_values(v) }
else
object
Expand Down
6 changes: 3 additions & 3 deletions test/delighted_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ def test_retrieving_a_survey_response_expand_person

survey_response = Delighted::SurveyResponse.retrieve('456', :expand => ['person'])
assert_kind_of Delighted::SurveyResponse, survey_response
assert_equal({ :score => 10 }, survey_response.to_hash)
assert_equal({ :person => '123', :score => 10 }, survey_response.to_hash)
assert_kind_of Delighted::Person, survey_response.person
assert_equal '123', survey_response.person.id
assert_equal({ :email => 'foo@bar.com' }, survey_response.person.to_hash)
Expand Down Expand Up @@ -157,13 +157,13 @@ def test_listing_all_survey_responses_expand_person
survey_responses = Delighted::SurveyResponse.all(:expand => ['person'])
assert_kind_of Delighted::EnumerableResourceCollection, survey_responses
assert_kind_of Delighted::SurveyResponse, survey_responses[0]
assert_equal({ :comment => 'One' }, survey_responses[0].to_hash)
assert_equal({ :person => '123', :comment => 'One' }, survey_responses[0].to_hash)
assert_equal 'One', survey_responses[0].comment
assert_equal '123', survey_responses[0].id
assert_kind_of Delighted::Person, survey_responses[0].person
assert_equal({ :email => 'foo@bar.com' }, survey_responses[0].person.to_hash)
assert_kind_of Delighted::SurveyResponse, survey_responses[1]
assert_equal({ :comment => 'Two' }, survey_responses[1].to_hash)
assert_equal({ :person => '123', :comment => 'Two' }, survey_responses[1].to_hash)
assert_equal 'Two', survey_responses[1].comment
assert_equal '456', survey_responses[1].id
assert_kind_of Delighted::Person, survey_responses[1].person
Expand Down

0 comments on commit 8497a20

Please sign in to comment.