Skip to content

Commit

Permalink
Merge pull request #65 from dashofcode/dirty_tracking
Browse files Browse the repository at this point in the history
Dirty tracking
  • Loading branch information
forest committed Feb 18, 2016
2 parents 645773e + badac58 commit f3575e3
Show file tree
Hide file tree
Showing 68 changed files with 361 additions and 116 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ spec/reports
test/tmp
test/version_tmp
tmp
.byebug_history
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ story.activity # Get

story.name = 'Save the Ewoks' # Update a single story attribute
story.attributes = { name: 'Save the Ewoks', description: '...' } # Update multiple story attributes
story.labels << TrackerApi::Resources::Label.new(name: 'Endor') # Add a new label to an existing story
story.add_label('Endor') # Add a new label to an existing story
story.save # Save changes made to a story

story = TrackerApi::Resources::Story.new( client: client,
Expand Down
8 changes: 5 additions & 3 deletions lib/tracker_api.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
require 'tracker_api/version'

# dependencies
require 'virtus'
require 'faraday'
require 'faraday_middleware'

if defined?(ActiveSupport)
require 'active_support/core_ext/object/blank'
else
require 'core_ext/object/blank'
end
require 'equalizer'
require 'representable/json'

require 'multi_json'
require 'representable/json'

# stdlib
require 'addressable/uri'
Expand Down Expand Up @@ -51,7 +52,8 @@ module Endpoints

module Resources
module Shared
autoload :HasId, 'tracker_api/resources/shared/has_id'
autoload :Base, 'tracker_api/resources/shared/base'
autoload :Collection, 'tracker_api/resources/shared/collection'
end
autoload :Activity, 'tracker_api/resources/activity'
autoload :Account, 'tracker_api/resources/account'
Expand Down
2 changes: 2 additions & 0 deletions lib/tracker_api/endpoints/epic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ def update(epic, params={})
data = client.put("/projects/#{epic.project_id}/epics/#{epic.id}", params: params).body

epic.attributes = data
epic.clean!
epic
end
end
end
Expand Down
2 changes: 2 additions & 0 deletions lib/tracker_api/endpoints/story.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ def update(story, params={})
data = client.put("/projects/#{story.project_id}/stories/#{story.id}", params: params).body

story.attributes = data
story.clean!
story
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/tracker_api/resources/account.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module TrackerApi
module Resources
class Account
include Shared::HasId
include Shared::Base

attribute :created_at, DateTime
attribute :status, String
Expand Down
4 changes: 2 additions & 2 deletions lib/tracker_api/resources/activity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ class Activity
attribute :project_version, Integer
attribute :message, String
attribute :highlight, String
attribute :changes, Array[Change]
attribute :primary_resources, Array[PrimaryResource]
attribute :changes, Shared::Collection[Change]
attribute :primary_resources, Shared::Collection[PrimaryResource]
attribute :project, Project
attribute :performed_by, Person
attribute :occurred_at, DateTime
Expand Down
2 changes: 1 addition & 1 deletion lib/tracker_api/resources/change.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module TrackerApi
module Resources
class Change
include Shared::HasId
include Shared::Base

attribute :change_type, String
attribute :kind, String
Expand Down
8 changes: 4 additions & 4 deletions lib/tracker_api/resources/comment.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
module TrackerApi
module Resources
class Comment
include Shared::HasId
include Shared::Base

attribute :story_id, Integer
attribute :epic_id, Integer
attribute :text, String
attribute :person_id, Integer
attribute :created_at, DateTime
attribute :updated_at, DateTime
attribute :file_attachment_ids, Array[Integer]
attribute :google_attachment_ids, Array[Integer]
attribute :file_attachment_ids, Shared::Collection[Integer]
attribute :google_attachment_ids, Shared::Collection[Integer]
attribute :commit_identifier, String
attribute :commit_type, String
attribute :kind, String
end
end
end
end
10 changes: 5 additions & 5 deletions lib/tracker_api/resources/epic.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
module TrackerApi
module Resources
class Epic
include Shared::HasId
include Shared::Base

attribute :client

attribute :comment_ids, Array[Integer]
attribute :comments, Array[Comment]
attribute :comment_ids, Shared::Collection[Integer]
attribute :comments, Shared::Collection[Comment]
attribute :created_at, DateTime
attribute :description, String
attribute :follower_ids, Array[Integer]
attribute :followers, Array[Person]
attribute :follower_ids, Shared::Collection[Integer]
attribute :followers, Shared::Collection[Person]
attribute :kind, String
attribute :label, Label
attribute :label_id, Integer
Expand Down
4 changes: 2 additions & 2 deletions lib/tracker_api/resources/iteration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ class Iteration
attribute :planned, Boolean
attribute :project_id, Integer
attribute :start, DateTime
attribute :stories, [Story]
attribute :story_ids, [Integer]
attribute :stories, Shared::Collection[Story]
attribute :story_ids, Shared::Collection[Integer]
attribute :team_strength, Float

def stories=(data)
Expand Down
2 changes: 1 addition & 1 deletion lib/tracker_api/resources/label.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module TrackerApi
module Resources
class Label
include Shared::HasId
include Shared::Base

attribute :created_at, DateTime
attribute :kind, String
Expand Down
8 changes: 4 additions & 4 deletions lib/tracker_api/resources/me.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
module TrackerApi
module Resources
class Me
include Shared::HasId
include Shared::Base

attribute :name, String
attribute :initials, String
attribute :username, String
attribute :time_zone, TimeZone
attribute :api_token, String
attribute :has_google_identity, Boolean
attribute :project_ids, Array[Integer]
attribute :projects, [MembershipSummary]
attribute :workspace_ids, Array[Integer]
attribute :project_ids, Shared::Collection[Integer]
attribute :projects, Shared::Collection[MembershipSummary]
attribute :workspace_ids, Shared::Collection[Integer]
attribute :email, String
attribute :receives_in_app_notifications, Boolean
attribute :kind, String
Expand Down
2 changes: 1 addition & 1 deletion lib/tracker_api/resources/membership_summary.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module TrackerApi
module Resources
class MembershipSummary
include Shared::HasId
include Shared::Base

attribute :kind, String
attribute :last_viewed_at, DateTime
Expand Down
2 changes: 1 addition & 1 deletion lib/tracker_api/resources/notification.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module TrackerApi
module Resources
class Notification
include Shared::HasId
include Shared::Base

attribute :client

Expand Down
2 changes: 1 addition & 1 deletion lib/tracker_api/resources/person.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module TrackerApi
module Resources
class Person
include Shared::HasId
include Shared::Base

attribute :kind, String
attribute :name, String
Expand Down
2 changes: 1 addition & 1 deletion lib/tracker_api/resources/primary_resource.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module TrackerApi
module Resources
class PrimaryResource
include Shared::HasId
include Shared::Base

attribute :kind, String
attribute :name, String
Expand Down
21 changes: 11 additions & 10 deletions lib/tracker_api/resources/project.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module TrackerApi
module Resources
class Project
include Shared::HasId
include Shared::Base

attribute :client

Expand All @@ -17,14 +17,14 @@ class Project
attribute :enable_incoming_emails, Boolean
attribute :enable_planned_mode, Boolean
attribute :enable_tasks, Boolean
attribute :epic_ids, Array[Integer]
attribute :epics, Array[Epic]
attribute :epic_ids, Shared::Collection[Integer]
attribute :epics, Shared::Collection[Epic]
attribute :has_google_domain, Boolean
attribute :initial_velocity, Integer
attribute :iteration_length, Integer
attribute :kind, String
attribute :label_ids, Array[Integer]
attribute :labels, Array[Label]
attribute :label_ids, Shared::Collection[Integer]
attribute :labels, Shared::Collection[Label]
attribute :name, String
attribute :number_of_done_iterations_to_show, Integer
attribute :point_scale, String
Expand Down Expand Up @@ -54,7 +54,7 @@ def label_ids
# @param [Hash] params
# @return [Array[Label]] labels of this project
def labels(params = {})
if @labels && @labels.any?
if @labels && @labels.present?
@labels
else
@labels = Endpoints::Labels.new(client).get(id, params)
Expand All @@ -66,10 +66,11 @@ def labels(params = {})
# @param [Hash] params
# @return [Array[Epic]] epics associated with this project
def epics(params={})
raise ArgumentError, 'Expected @epics to be an Array' unless @epics.is_a? Array
return @epics unless @epics.empty?

@epics = Endpoints::Epics.new(client).get(id, params)
if @epics && @epics.present?
@epics
else
@epics = Endpoints::Epics.new(client).get(id, params)
end
end

# Provides a list of all the iterations in the project.
Expand Down
2 changes: 1 addition & 1 deletion lib/tracker_api/resources/project_membership.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module TrackerApi
module Resources
class ProjectMembership
include Shared::HasId
include Shared::Base

attribute :person_id, Integer
attribute :project_id, Integer
Expand Down
30 changes: 30 additions & 0 deletions lib/tracker_api/resources/shared/base.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
require 'virtus'
if Virtus::Attribute::NullifyBlank.method_defined?(:coerce)
require 'virtus/attribute/nullify_blank'
else
raise """
WARNING: The above monkey patch can't be applied as expected.
See discussion here: https://github.com/dashofcode/tracker_api/commit/27599e7e2169776c32bbff8c972a31b930452879
"""
end
require 'virtus/dirty_attribute'

module TrackerApi
module Resources
module Shared
module Base
def self.included(base)
base.class_eval do
include Virtus.model(nullify_blank: true)
include Virtus::DirtyAttribute
include Virtus::DirtyAttribute::InitiallyClean

include Equalizer.new(:id)

attribute :id, Integer
end
end
end
end
end
end
17 changes: 17 additions & 0 deletions lib/tracker_api/resources/shared/collection.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module TrackerApi
module Resources
module Shared
class Collection < Array
def <<(item)
warn """
WARNING: Direct mutation of an attribute value skips coercion
and dirty tracking. Please use direct assignment or the
specialized add_* methods to get expected behavior.
https://github.com/solnic/virtus#important-note-about-member-coercions
"""
super
end
end
end
end
end
18 changes: 0 additions & 18 deletions lib/tracker_api/resources/shared/has_id.rb

This file was deleted.

Loading

0 comments on commit f3575e3

Please sign in to comment.