Class: Caseblocks::Case
- Inherits:
-
Object
- Object
- Caseblocks::Case
- Defined in:
- lib/caseblocks/case.rb
Overview
Class for handling Case objects
Relationships are accessed using dynamic method names based on the related case type code, for example if you have an order case type relating to customer then kase.customers will return an array of related customer cases
Currently there is a very simplistic caching mechanism, so may cause issues if you need to refresh data
Class Method Summary collapse
-
.create(data, case_type, client = Caseblocks::Client.default) ⇒ Caseblocks::Case
Creates a case within the supplied case type with the data requested.
-
.find(id, client = Caseblocks::Client.default) ⇒ Caseblocks::Case
Finds an individual case from an id.
-
.find_all_by(criteria, client = Caseblocks::Client.default, options = {}) ⇒ Array<Caseblocks::Case>
Finds all cases matching the supplied search criteria matched to the cases data.
-
.find_by(criteria, client = Caseblocks::Client.default) ⇒ Caseblocks::Case
Finds an individual case based on search criteria matched to the cases data.
Instance Method Summary collapse
-
#attributes ⇒ Hash
The case data for the current case.
-
#case_type ⇒ Caseblocks::CaseType
Case type for the current case.
-
#case_type_id ⇒ Integer
Case type id for the current case.
-
#id ⇒ String
Returns the bson id for the case.
-
#initialize(case_data, client) ⇒ Caseblocks::Case
constructor
Initializes a new case object.
- #method_missing(m, *args, &block) ⇒ Object
-
#save ⇒ Boolean
Saves the current data against the case and updates attributes with latest from server.
- #tasklists ⇒ Object
-
#tasks ⇒ Array<Caseblocks::Task>
Returns an array of Caseblocks::Task that are on this case.
Constructor Details
#initialize(case_data, client) ⇒ Caseblocks::Case
Initializes a new case object
62 63 64 65 |
# File 'lib/caseblocks/case.rb', line 62 def initialize(case_data, client) @client = client @case_data = case_data end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(m, *args, &block) ⇒ Object
119 120 121 122 123 124 125 126 |
# File 'lib/caseblocks/case.rb', line 119 def method_missing(m, *args, &block) matched_relationship = case_type.relationships.detect{|r| r.case_type.code == m.to_s.singularize} if matched_relationship return @client.request("/case_blocks/#{matched_relationship.case_type.code.pluralize}?related_cases%5Brelation_id%5D=#{matched_relationship.id}&related_cases%5Brelationship_type%5D=CaseBlocks%3A%3ACaseTypeDirectRelationship&related_cases%5Bcase_from_id%5D=#{id}&related_cases%5Bpage_size%5D=1000")[matched_relationship.case_type.code.pluralize].map{|kase| Caseblocks::Case.new(kase, @client) } else super end end |
Class Method Details
.create(data, case_type, client = Caseblocks::Client.default) ⇒ Caseblocks::Case
Creates a case within the supplied case type with the data requested. Will fail if validation fails with a Caseblocks::ValidationError error
104 105 106 107 108 |
# File 'lib/caseblocks/case.rb', line 104 def self.create(data, case_type, client=Caseblocks::Client.default) data["case_type_id"] = case_type.id res = client.post("/case_blocks/#{case_type.code.pluralize}.json", {case_type.code => data}) Case.new(res[case_type.code], client) end |
.find(id, client = Caseblocks::Client.default) ⇒ Caseblocks::Case
Finds an individual case from an id
19 20 21 22 |
# File 'lib/caseblocks/case.rb', line 19 def self.find(id, client=Caseblocks::Client.default) res = client.request("/case_blocks/cases/#{id}.json") return Case.new(res["case"], client) end |
.find_all_by(criteria, client = Caseblocks::Client.default, options = {}) ⇒ Array<Caseblocks::Case>
Finds all cases matching the supplied search criteria matched to the cases data
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/caseblocks/case.rb', line 36 def self.find_all_by(criteria, client=Caseblocks::Client.default, ={}) [:page_size] = 10 unless .has_key?(:page_size) [:page] = 0 unless .has_key?(:page) current_page = 0 page_size = [:page_size] total = -1 results = [] while (total == -1 || ((current_page * page_size) + page_size) < total) do [:query_type] = "AND" unless .has_key?(:query_type) query = {:query => criteria.map{|k,v| "#{k}:\"#{Caseblocks::Helpers::Elastic.encode_query(v).gsub("&", "%26")}\""}.join(" #{[:query_type]} ")} query.merge!() res = client.request("/case_blocks/search.json", query) total = res.inject(0){|total, ct| total += ct["total"].to_i } cases = res.map{|ct| ct["cases"] }.flatten results += cases.map{|kase| Case.new(kase, client) } current_page += 1 [:page] = current_page end results end |
.find_by(criteria, client = Caseblocks::Client.default) ⇒ Caseblocks::Case
Finds an individual case based on search criteria matched to the cases data
28 29 30 |
# File 'lib/caseblocks/case.rb', line 28 def self.find_by(criteria, client=Caseblocks::Client.default) Caseblocks::Case.find_all_by(criteria, client).first end |
Instance Method Details
#attributes ⇒ Hash
The case data for the current case. This can be treated as the hash of data within the case as the save operation will use this data to update the case
87 88 89 |
# File 'lib/caseblocks/case.rb', line 87 def attributes @case_data end |
#case_type ⇒ Caseblocks::CaseType
Case type for the current case
75 76 77 |
# File 'lib/caseblocks/case.rb', line 75 def case_type @case_type ||= Caseblocks::CaseType.find(case_type_id, @client) end |
#case_type_id ⇒ Integer
Case type id for the current case
81 82 83 |
# File 'lib/caseblocks/case.rb', line 81 def case_type_id @case_data["case_type_id"] || @case_data["work_type_id"] || @case_data["people_type_id"] || @case_data["organization_type_id"] end |
#id ⇒ String
Returns the bson id for the case
69 70 71 |
# File 'lib/caseblocks/case.rb', line 69 def id @case_data["_id"] end |
#save ⇒ Boolean
Saves the current data against the case and updates attributes with latest from server
112 113 114 115 116 |
# File 'lib/caseblocks/case.rb', line 112 def save res = @client.put("/case_blocks/cases/#{id}.json", @case_data) @case_data.merge(res["case"]) true end |
#tasklists ⇒ Object
Implement me
129 130 131 |
# File 'lib/caseblocks/case.rb', line 129 def tasklists [] end |
#tasks ⇒ Array<Caseblocks::Task>
Returns an array of Caseblocks::Task that are on this case. This is a full list of all tasks, not split by tasklist.
135 136 137 |
# File 'lib/caseblocks/case.rb', line 135 def tasks @tasks ||= Caseblocks::Task.find_all(@case_data["tasks"], @client) end |