Last active
August 29, 2015 14:15
-
-
Save gogogarrett/9d56c6c7fbbfe35e9cf8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'representable/json' | |
require 'representable/json/collection' | |
# representers | |
module StudentProgressRepresenter | |
include Representable::JSON | |
property :id | |
collection :precinct_progresses#, extend: PrecinctProgressRepresenter, class: PrecinctProgress | |
end | |
module PrecinctProgressesRepresenter | |
include Representable::JSON::Collection | |
items extend: PrecinctProgressRepresenter | |
end | |
module PrecinctProgressRepresenter | |
include Representable::JSON | |
property :precinct | |
property :maximum_lesson | |
end | |
PrecinctProgress = Class.new(Struct.new(:precinct, :maximum_lesson)) | |
class StudentProgress | |
attr_accessor :id, :precinct_progresses | |
def initialize(id) | |
@id = id | |
end | |
def id | |
@id | |
end | |
def precinct_progresses | |
[ | |
PrecinctProgress.new('my_lessons', 1), | |
PrecinctProgress.new('my_lessons', 2), | |
PrecinctProgress.new('my_lessons', 3), | |
] | |
end | |
end | |
student_progress = StudentProgress.new(1) | |
StudentProgressRepresenter.prepare(student_progress).to_json | |
from_json = "{\"5612776\":[{\"precinct_progress\":{\"precinct\":\"my_lessons\",\"maximum_lesson\":1}},{\"precinct_progress\":{\"precinct\":\"english_skills_spelling\",\"maximum_lesson\":22}}],\"5612778\":[{\"precinct_progress\":{\"precinct\":\"my_lessons\",\"maximum_lesson\":45}},{\"precinct_progress\":{\"precinct\":\"english_skills_spelling\",\"maximum_lesson\":122}}],\"5612775\":[{\"precinct_progress\":{\"precinct\":\"my_lessons\",\"maximum_lesson\":1}},{\"precinct_progress\":{\"precinct\":\"english_skills_spelling\",\"maximum_lesson\":1}}],\"5612777\":[{\"precinct_progress\":{\"precinct\":\"my_lessons\",\"maximum_lesson\":1}},{\"precinct_progress\":{\"precinct\":\"english_skills_spelling\",\"maximum_lesson\":1}}],\"4766964\":[{\"precinct_progress\":{\"precinct\":\"my_lessons\",\"maximum_lesson\":1}},{\"precinct_progress\":{\"precinct\":\"english_skills_spelling\",\"maximum_lesson\":1}}]}" | |
StudentProgress.new(1).extend(StudentProgressRepresenter).from_json(from_json) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
So if we replaced precinct_progresses with the hash that's in the spec for the presenter: i.e.
then does
StudentProgress
(edit - andPrecinctProgress
) need to contain the logic of parsing this for a student or will this be handled somewhere else inPrecinctProgressRepresenter
?