From 8514b6903f511fafd50bbf72dde2e58a9f4a3b01 Mon Sep 17 00:00:00 2001 From: Ania Slimak Date: Tue, 17 Nov 2015 10:57:58 +0100 Subject: [PATCH 1/3] Return accountabilities, domains and people in role object --- lib/glassfrog/accountability.rb | 12 ++++++++++++ lib/glassfrog/domain.rb | 11 +++++++++++ lib/glassfrog/link_factory.rb | 19 +++++++++++++++++++ lib/glassfrog/role.rb | 22 +++++++++++++++++++--- 4 files changed, 61 insertions(+), 3 deletions(-) create mode 100644 lib/glassfrog/accountability.rb create mode 100644 lib/glassfrog/domain.rb create mode 100644 lib/glassfrog/link_factory.rb diff --git a/lib/glassfrog/accountability.rb b/lib/glassfrog/accountability.rb new file mode 100644 index 0000000..da68572 --- /dev/null +++ b/lib/glassfrog/accountability.rb @@ -0,0 +1,12 @@ +require 'glassfrog/base' + +module Glassfrog + # + # Encapsulates GlassFrog Accountability + # + class Accountability < Glassfrog::Base + # @return [String] + attr_accessor :description + end +end + diff --git a/lib/glassfrog/domain.rb b/lib/glassfrog/domain.rb new file mode 100644 index 0000000..1535d12 --- /dev/null +++ b/lib/glassfrog/domain.rb @@ -0,0 +1,11 @@ +require 'glassfrog/base' + +module Glassfrog + # + # Encapsulates GlassFrog Accountability + # + class Domain < Glassfrog::Base + # @return [String] + attr_accessor :description + end +end diff --git a/lib/glassfrog/link_factory.rb b/lib/glassfrog/link_factory.rb new file mode 100644 index 0000000..33533b7 --- /dev/null +++ b/lib/glassfrog/link_factory.rb @@ -0,0 +1,19 @@ +require 'glassfrog/accountability' +require 'glassfrog/domain' +require 'glassfrog/person' + +module Glassfrog + class LinkFactory + CONFIG = { + accountabilities: Accountability, + domains: Domain, + people: Person + } + + def self.build(link_type, attributes) + klass = CONFIG[link_type] + klass.new(attributes) + end + end +end + diff --git a/lib/glassfrog/role.rb b/lib/glassfrog/role.rb index 68de8cb..bb9004d 100644 --- a/lib/glassfrog/role.rb +++ b/lib/glassfrog/role.rb @@ -1,6 +1,7 @@ require 'glassfrog/base' require 'glassfrog/rest/get' require 'glassfrog/rest/patch' +require 'glassfrog/link_factory' module Glassfrog # @@ -10,11 +11,22 @@ class Role < Glassfrog::Base # @return [String] attr_accessor :name, :purpose # @return [Hash] - attr_accessor :links + attr_accessor :links, :accountabilities, :domains, :people PATH = '/roles' PATCH_PATH = '/roles/0/links/people/' TYPE = :roles + LINK_TYPES = [:accountabilities, :domains, :people] + + def build_link_objects(response, type) + link_objects = links[type].map do |link_id| + links = response[:linked][type] + attributes = links.detect { |link| link[:id] == link_id } + LinkFactory.build(type, attributes) + end + self.send("#{type}=", link_objects) + end + # # Sends a GET request for Role(s) to GlassFrog. # @param client [Glassfrog::Client] The client that will send the request. Contains the API key. @@ -23,7 +35,11 @@ class Role < Glassfrog::Base # @return [Array] The array of Role(s) fetched from GlassFrog. def self.get(client, options) response = Glassfrog::REST::Get.get(client, PATH, options) - response[TYPE].map { |object| self.new(object) } + response[TYPE].map do |object| + role = self.new(object) + LINK_TYPES.each { |type| role.build_link_objects(response, type) } + role + end end # @@ -91,4 +107,4 @@ def self.formify_role_patch(options, operation) end end end -end \ No newline at end of file +end From 1b704f5c90d1c4011f9d28529faef5a0d597ca9e Mon Sep 17 00:00:00 2001 From: Ania Slimak Date: Thu, 19 Nov 2015 15:05:53 +0100 Subject: [PATCH 2/3] Added building links to circles too --- lib/glassfrog/accountability.rb | 3 +++ lib/glassfrog/base.rb | 11 ++++++++++- lib/glassfrog/circle.rb | 12 +++++++++--- lib/glassfrog/domain.rb | 3 +++ lib/glassfrog/link_factory.rb | 16 ++++++---------- lib/glassfrog/person.rb | 5 ++++- lib/glassfrog/role.rb | 9 +-------- 7 files changed, 36 insertions(+), 23 deletions(-) diff --git a/lib/glassfrog/accountability.rb b/lib/glassfrog/accountability.rb index da68572..204d655 100644 --- a/lib/glassfrog/accountability.rb +++ b/lib/glassfrog/accountability.rb @@ -1,4 +1,5 @@ require 'glassfrog/base' +require 'glassfrog/link_factory' module Glassfrog # @@ -7,6 +8,8 @@ module Glassfrog class Accountability < Glassfrog::Base # @return [String] attr_accessor :description + + LinkFactory.register(:accountabilities, self) end end diff --git a/lib/glassfrog/base.rb b/lib/glassfrog/base.rb index fe3bb0f..61bea5d 100644 --- a/lib/glassfrog/base.rb +++ b/lib/glassfrog/base.rb @@ -39,5 +39,14 @@ def hashify self.instance_variables.each { |var| hash[var.to_s.delete("@")] = self.instance_variable_get(var) } symbolize_keys(hash) end + + def build_link_objects(response, type) + link_objects = links[type].map do |link_id| + links = response[:linked][type] + attributes = links.detect { |link| link[:id] == link_id } + LinkFactory.build(type, attributes) + end + self.send("#{type}=", link_objects) + end end -end \ No newline at end of file +end diff --git a/lib/glassfrog/circle.rb b/lib/glassfrog/circle.rb index bf523b4..70476ad 100644 --- a/lib/glassfrog/circle.rb +++ b/lib/glassfrog/circle.rb @@ -9,12 +9,14 @@ class Circle < Glassfrog::Base # @return [String] attr_accessor :name, :short_name, :strategy # @return [Hash] - attr_accessor :links + attr_accessor :links, :roles # @return [Array] The array of Circle(s) fetched from GlassFrog. def self.get(client, options) response = Glassfrog::REST::Get.get(client, PATH, options) - response[TYPE].map { |object| self.new(object) } + response[TYPE].map do |object| + circle = self.new(object) + LINK_TYPES.each { |type| circle.build_link_objects(response, type) } + circle + end end end -end \ No newline at end of file +end diff --git a/lib/glassfrog/domain.rb b/lib/glassfrog/domain.rb index 1535d12..a34067f 100644 --- a/lib/glassfrog/domain.rb +++ b/lib/glassfrog/domain.rb @@ -1,4 +1,5 @@ require 'glassfrog/base' +require 'glassfrog/link_factory' module Glassfrog # @@ -7,5 +8,7 @@ module Glassfrog class Domain < Glassfrog::Base # @return [String] attr_accessor :description + + LinkFactory.register(:domains, self) end end diff --git a/lib/glassfrog/link_factory.rb b/lib/glassfrog/link_factory.rb index 33533b7..d421bd2 100644 --- a/lib/glassfrog/link_factory.rb +++ b/lib/glassfrog/link_factory.rb @@ -1,17 +1,13 @@ -require 'glassfrog/accountability' -require 'glassfrog/domain' -require 'glassfrog/person' - module Glassfrog class LinkFactory - CONFIG = { - accountabilities: Accountability, - domains: Domain, - people: Person - } + @@config = {} + + def self.register(key, klass) + @@config[key] = klass + end def self.build(link_type, attributes) - klass = CONFIG[link_type] + klass = @@config[link_type] klass.new(attributes) end end diff --git a/lib/glassfrog/person.rb b/lib/glassfrog/person.rb index 72b8e6d..1e6b471 100644 --- a/lib/glassfrog/person.rb +++ b/lib/glassfrog/person.rb @@ -3,6 +3,7 @@ require 'glassfrog/rest/post' require 'glassfrog/rest/patch' require 'glassfrog/rest/delete' +require 'glassfrog/link_factory' module Glassfrog # @@ -18,6 +19,8 @@ class Person < Glassfrog::Base PATH = '/people' TYPE = :people + LinkFactory.register(:people, self) + # # Sends a GET request for Person(s) to GlassFrog. # @param client [Glassfrog::Client] The client that will send the request. Contains the API key. @@ -81,4 +84,4 @@ def self.parse_options(options) params_hash end end -end \ No newline at end of file +end diff --git a/lib/glassfrog/role.rb b/lib/glassfrog/role.rb index bb9004d..0f63c46 100644 --- a/lib/glassfrog/role.rb +++ b/lib/glassfrog/role.rb @@ -18,14 +18,7 @@ class Role < Glassfrog::Base LINK_TYPES = [:accountabilities, :domains, :people] - def build_link_objects(response, type) - link_objects = links[type].map do |link_id| - links = response[:linked][type] - attributes = links.detect { |link| link[:id] == link_id } - LinkFactory.build(type, attributes) - end - self.send("#{type}=", link_objects) - end + LinkFactory.register(:roles, self) # # Sends a GET request for Role(s) to GlassFrog. From 425a94e6ddc0605fa10ba03ad70e1472014236cc Mon Sep 17 00:00:00 2001 From: Ania Slimak Date: Fri, 4 Dec 2015 15:55:10 +0100 Subject: [PATCH 3/3] Refactor --- lib/glassfrog/accountability.rb | 3 --- lib/glassfrog/base.rb | 8 +++++++- lib/glassfrog/circle.rb | 10 ++++++++-- lib/glassfrog/domain.rb | 3 --- lib/glassfrog/link_factory.rb | 3 ++- lib/glassfrog/person.rb | 3 --- lib/glassfrog/role.rb | 13 ++++++++++--- 7 files changed, 27 insertions(+), 16 deletions(-) diff --git a/lib/glassfrog/accountability.rb b/lib/glassfrog/accountability.rb index 204d655..da68572 100644 --- a/lib/glassfrog/accountability.rb +++ b/lib/glassfrog/accountability.rb @@ -1,5 +1,4 @@ require 'glassfrog/base' -require 'glassfrog/link_factory' module Glassfrog # @@ -8,8 +7,6 @@ module Glassfrog class Accountability < Glassfrog::Base # @return [String] attr_accessor :description - - LinkFactory.register(:accountabilities, self) end end diff --git a/lib/glassfrog/base.rb b/lib/glassfrog/base.rb index 61bea5d..c055d76 100644 --- a/lib/glassfrog/base.rb +++ b/lib/glassfrog/base.rb @@ -40,7 +40,13 @@ def hashify symbolize_keys(hash) end - def build_link_objects(response, type) + def build_link_objects(response) + link_types.each do |type| + build_link_objects_with_type(response, type) + end + end + + def build_link_objects_with_type(response, type) link_objects = links[type].map do |link_id| links = response[:linked][type] attributes = links.detect { |link| link[:id] == link_id } diff --git a/lib/glassfrog/circle.rb b/lib/glassfrog/circle.rb index 70476ad..e26f76b 100644 --- a/lib/glassfrog/circle.rb +++ b/lib/glassfrog/circle.rb @@ -1,5 +1,7 @@ require 'glassfrog/base' require 'glassfrog/rest/get' +require 'glassfrog/role' +require 'glassfrog/link_factory' module Glassfrog # @@ -15,7 +17,11 @@ class Circle < Glassfrog::Base PATH = '/circles' TYPE = :circles - LINK_TYPES = [:roles] + LinkFactory.register(:roles, Role) + + def link_types + [:roles] + end # # Sends a GET request for Circle(s) to GlassFrog. @@ -27,7 +33,7 @@ def self.get(client, options) response = Glassfrog::REST::Get.get(client, PATH, options) response[TYPE].map do |object| circle = self.new(object) - LINK_TYPES.each { |type| circle.build_link_objects(response, type) } + circle.build_link_objects(response) circle end end diff --git a/lib/glassfrog/domain.rb b/lib/glassfrog/domain.rb index a34067f..1535d12 100644 --- a/lib/glassfrog/domain.rb +++ b/lib/glassfrog/domain.rb @@ -1,5 +1,4 @@ require 'glassfrog/base' -require 'glassfrog/link_factory' module Glassfrog # @@ -8,7 +7,5 @@ module Glassfrog class Domain < Glassfrog::Base # @return [String] attr_accessor :description - - LinkFactory.register(:domains, self) end end diff --git a/lib/glassfrog/link_factory.rb b/lib/glassfrog/link_factory.rb index d421bd2..1f561ee 100644 --- a/lib/glassfrog/link_factory.rb +++ b/lib/glassfrog/link_factory.rb @@ -8,7 +8,8 @@ def self.register(key, klass) def self.build(link_type, attributes) klass = @@config[link_type] - klass.new(attributes) + object = klass.new(attributes) + object end end end diff --git a/lib/glassfrog/person.rb b/lib/glassfrog/person.rb index 1e6b471..39ce318 100644 --- a/lib/glassfrog/person.rb +++ b/lib/glassfrog/person.rb @@ -3,7 +3,6 @@ require 'glassfrog/rest/post' require 'glassfrog/rest/patch' require 'glassfrog/rest/delete' -require 'glassfrog/link_factory' module Glassfrog # @@ -19,8 +18,6 @@ class Person < Glassfrog::Base PATH = '/people' TYPE = :people - LinkFactory.register(:people, self) - # # Sends a GET request for Person(s) to GlassFrog. # @param client [Glassfrog::Client] The client that will send the request. Contains the API key. diff --git a/lib/glassfrog/role.rb b/lib/glassfrog/role.rb index 0f63c46..0d0da5c 100644 --- a/lib/glassfrog/role.rb +++ b/lib/glassfrog/role.rb @@ -2,6 +2,9 @@ require 'glassfrog/rest/get' require 'glassfrog/rest/patch' require 'glassfrog/link_factory' +require 'glassfrog/accountability' +require 'glassfrog/domain' +require 'glassfrog/person' module Glassfrog # @@ -16,9 +19,13 @@ class Role < Glassfrog::Base PATCH_PATH = '/roles/0/links/people/' TYPE = :roles - LINK_TYPES = [:accountabilities, :domains, :people] + LinkFactory.register(:accountabilities, Accountability) + LinkFactory.register(:domains, Domain) + LinkFactory.register(:people, Person) - LinkFactory.register(:roles, self) + def link_types + [:accountabilities, :domains, :people] + end # # Sends a GET request for Role(s) to GlassFrog. @@ -30,7 +37,7 @@ def self.get(client, options) response = Glassfrog::REST::Get.get(client, PATH, options) response[TYPE].map do |object| role = self.new(object) - LINK_TYPES.each { |type| role.build_link_objects(response, type) } + role.build_link_objects(response) role end end