Class: ActivePodio::Base
- Inherits:
-
Object
- Object
- ActivePodio::Base
- Extended by:
- ActiveModel::Callbacks, ActiveModel::Naming
- Includes:
- ActiveModel::Conversion
- Defined in:
- lib/podio/active_podio/base.rb
Direct Known Subclasses
Podio::AccountProvider, Podio::Action, Podio::ActivationStatus, Podio::Activity, Podio::Answer, Podio::AppStoreCategory, Podio::AppStoreShare, Podio::Application, Podio::ApplicationEmail, Podio::ApplicationField, Podio::Batch, Podio::ByLine, Podio::CalendarEvent, Podio::CalendarMute, Podio::Campaign, Podio::Category, Podio::Comment, Podio::Condition, Podio::ConditionSet, Podio::Contract, Podio::ContractAccounting, Podio::ContractAttribution, Podio::ContractEvent, Podio::ContractPeriod, Podio::ContractPrice, Podio::ContractPriceItem, Podio::Conversation, Podio::ConversationEvent, Podio::ConversationMessage, Podio::ConversationParticipant, Podio::Device, Podio::EmailContact, Podio::EmailSubscriptionSetting, Podio::Embed, Podio::Experiment, Podio::Extension, Podio::ExternalFile, Podio::FileAttachment, Podio::Filter, Podio::Form, Podio::Friend, Podio::Grant, Podio::Hook, Podio::Importer, Podio::Integration, Podio::Invoice, Podio::Item, Podio::ItemDiff, Podio::ItemField, Podio::ItemRevision, Podio::ItemTransaction, Podio::LinkedAccount, Podio::LinkedAccountData, Podio::Live, Podio::NetPromoterScore, Podio::Notification, Podio::NotificationGroup, Podio::OAuth, Podio::OAuthClient, Podio::OAuthScope, Podio::Organization, Podio::OrganizationMember, Podio::OrganizationMembership, Podio::OrganizationProfile, Podio::Pin, Podio::Profile, Podio::Promotion, Podio::PromotionGroup, Podio::PromotionGroupMember, Podio::Question, Podio::QuestionAnswer, Podio::QuestionOption, Podio::Rating, Podio::Recurrence, Podio::Reference, Podio::Referral, Podio::Reminder, Podio::Search, Podio::Space, Podio::SpaceInvitation, Podio::SpaceMember, Podio::Status, Podio::StreamActivityGroup, Podio::StreamMute, Podio::StreamObject, Podio::Subscription, Podio::Tag, Podio::TagSearch, Podio::Task, Podio::TaskLabel, Podio::User, Podio::UserMail, Podio::UserStatus, Podio::Variation, Podio::Via, Podio::View, Podio::Vote, Podio::Voting, Podio::Voucher, Podio::Widget
Instance Attribute Summary collapse
-
#attributes ⇒ Object
Returns the value of attribute attributes.
Class Method Summary collapse
-
.collection(response) ⇒ Object
Returns a struct that includes: * all: A collection model instances * count: The number of returned records * total_count: The total number of records matching the given conditions.
- .delegate_to_hash(hash_name, *attribute_names) ⇒ Object
-
.has_many(name, options = {}) ⇒ Object
Wraps a collection of hashes from the API to a collection of the given model.
-
.has_one(name, options = {}) ⇒ Object
Wraps a single hash provided from the API in the given model.
- .klass_from_string(klass_name) ⇒ Object
-
.list(response) ⇒ Object
Returns a simple collection model instances.
-
.member(response) ⇒ Object
Returns a single instance of the model.
- .output_attribute_as_json(*attributes) ⇒ Object
-
.property(name, type = :string, options = {}) ⇒ Object
Defines the the supported attributes of the model.
Instance Method Summary collapse
- #==(other) ⇒ Object (also: #eql?)
- #[](attribute) ⇒ Object
- #[]=(attribute, value) ⇒ Object
-
#api_friendly_ref_type ⇒ Object
Override this in models where the class name doesn’t match the ref type.
- #as_json(options = {}) ⇒ Object
- #hash ⇒ Object
-
#initialize(attributes = {}, options = {}) ⇒ Base
constructor
A new instance of Base.
- #initialize_attributes(attributes) ⇒ Object
- #new_record? ⇒ Boolean
- #parent_model ⇒ Object
- #persisted? ⇒ Boolean
- #to_param ⇒ Object
Constructor Details
#initialize(attributes = {}, options = {}) ⇒ Base
Returns a new instance of Base.
17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/podio/active_podio/base.rb', line 17 def initialize(attributes = {}, = {}) attributes = {} if attributes.blank? self.attributes = Hash[*self.valid_attributes.collect { |n| [n.to_sym, nil] }.flatten].merge(attributes.symbolize_keys) @values_from_api = [:values_from_api] # Used to determine if date times should be converted from local to utc, or are already utc self.initialize_attributes(attributes) @belongs_to = [:belongs_to] # Allows has_one associations to communicate their changed content back to their parent model @values_from_api = false end |
Instance Attribute Details
#attributes ⇒ Object
Returns the value of attribute attributes.
15 16 17 |
# File 'lib/podio/active_podio/base.rb', line 15 def attributes @attributes end |
Class Method Details
.collection(response) ⇒ Object
Returns a struct that includes:
-
all: A collection model instances
-
count: The number of returned records
-
total_count: The total number of records matching the given conditions
295 296 297 298 299 |
# File 'lib/podio/active_podio/base.rb', line 295 def collection(response) result = Struct.new(:all, :count, :total_count).new(response['items'], response['filtered'], response['total']) result.all.map! { |item| new(item, :values_from_api => true) } result end |
.delegate_to_hash(hash_name, *attribute_names) ⇒ Object
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 |
# File 'lib/podio/active_podio/base.rb', line 301 def delegate_to_hash(hash_name, *attribute_names) = attribute_names. .reverse_merge!(:prefix => false, :setter => false) .assert_valid_keys(:prefix, :setter) attribute_names.each do |attribute_name| hash_index = attribute_name.to_s.gsub(/[\?!]/, '') method_name = "#{[:prefix] ? "#{hash_name}_" : ''}#{attribute_name}" self.send(:define_method, method_name) do self.send("#{hash_name}=", {}) unless self.send(hash_name) self.send(hash_name)[hash_index] end if [:setter] self.send(:define_method, "#{method_name}=") do |value| self.send("#{hash_name}=", {}) unless self.send(hash_name) self.send(hash_name)[hash_index] = value end end end end |
.has_many(name, options = {}) ⇒ Object
Wraps a collection of hashes from the API to a collection of the given model
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 |
# File 'lib/podio/active_podio/base.rb', line 257 def has_many(name, = {}) self._associations = self._associations.merge({name => :has_many}) self.send(:define_method, name) do klass = klass_for_association() instances = self.instance_variable_get("@#{name}_has_many_instances") unless instances.present? property = [:property] || name.to_sym if self[property].present? && self[property].respond_to?(:map) instances = self[property].map { |attributes| klass.new(attributes, :belongs_to => { :model => self }) } self.instance_variable_set("@#{name}_has_many_instances", instances) else instances = [] end end instances end self.send(:define_method, "#{name}?") do self.send(name).length > 0 end end |
.has_one(name, options = {}) ⇒ Object
Wraps a single hash provided from the API in the given model
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 |
# File 'lib/podio/active_podio/base.rb', line 233 def has_one(name, = {}) self._associations = self._associations.merge({name => :has_one}) self.send(:define_method, name) do klass = klass_for_association() instance = self.instance_variable_get("@#{name}_has_one_instance") unless instance.present? property = [:property] || name.to_sym if self[property].present? instance = klass.new(self[property], :belongs_to => { :model => self, :as => property }) self.instance_variable_set("@#{name}_has_one_instance", instance) else instance = nil end end instance end self.send(:define_method, "clear_#{name}") do self.instance_variable_set("@#{name}_has_one_instance", nil) end end |
.klass_from_string(klass_name) ⇒ Object
325 326 327 328 329 |
# File 'lib/podio/active_podio/base.rb', line 325 def klass_from_string(klass_name) klass = klass_name.constantize rescue nil klass = "Podio::#{klass_name}".constantize unless klass.respond_to?(:ancestors) && klass.ancestors.include?(ActivePodio::Base) return klass end |
.list(response) ⇒ Object
Returns a simple collection model instances
286 287 288 289 |
# File 'lib/podio/active_podio/base.rb', line 286 def list(response) response.map! { |item| new(item, :values_from_api => true) } response end |
.member(response) ⇒ Object
Returns a single instance of the model
281 282 283 |
# File 'lib/podio/active_podio/base.rb', line 281 def member(response) new(response, :values_from_api => true) end |
.output_attribute_as_json(*attributes) ⇒ Object
321 322 323 |
# File 'lib/podio/active_podio/base.rb', line 321 def output_attribute_as_json(*attributes) self.json_attributes += attributes end |
.property(name, type = :string, options = {}) ⇒ Object
Defines the the supported attributes of the model
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 |
# File 'lib/podio/active_podio/base.rb', line 206 def property(name, type = :string, = {}) self.valid_attributes += [name] case type when :datetime define_datetime_accessor(name, ) when :date define_date_accessor(name) when :time define_time_accessor(name) when :integer define_integer_accessor(name) when :boolean define_generic_accessor(name, :setter => false) define_boolean_accessors(name) when :array define_array_accessors(name) when :float define_float_accessor(name) when :decimal define_decimal_accessor(name) else define_generic_accessor(name) end end |
Instance Method Details
#==(other) ⇒ Object Also known as: eql?
89 90 91 |
# File 'lib/podio/active_podio/base.rb', line 89 def ==(other) !self.nil? && !other.nil? && self.respond_to?(:id) && other.respond_to?(:id) && self.id == other.id end |
#[](attribute) ⇒ Object
75 76 77 78 |
# File 'lib/podio/active_podio/base.rb', line 75 def [](attribute) @attributes ||= {} @attributes[attribute.to_sym] end |
#[]=(attribute, value) ⇒ Object
80 81 82 83 84 85 86 87 |
# File 'lib/podio/active_podio/base.rb', line 80 def []=(attribute, value) @attributes ||= {} @attributes[attribute.to_sym] = value if @belongs_to.present? && @belongs_to[:model].present? && @belongs_to[:as].present? && value.present? @belongs_to[:model][@belongs_to[:as]] ||= {} @belongs_to[:model][@belongs_to[:as]][attribute.to_sym] = value end end |
#api_friendly_ref_type ⇒ Object
Override this in models where the class name doesn’t match the ref type
143 144 145 |
# File 'lib/podio/active_podio/base.rb', line 143 def api_friendly_ref_type self.class.name.demodulize.parameterize end |
#as_json(options = {}) ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/podio/active_podio/base.rb', line 98 def as_json(={}) ||= {} result = {} result.merge!(:id => self.id) if self.respond_to?(:id) if [:formatted] (self.valid_attributes + self.json_attributes).uniq.each do |name| result[name] = json_friendly_value(self.send(name), ) end unless [:nested] == false self._associations.each do |name, type| nested_value = self.send(name) unless nested_value.nil? = .except(:methods) if [:methods].present? && [:methods].respond_to?(:find) methods_hash = [:methods].find { |method| method.is_a?(Hash) } if methods_hash.present? nested_methods = methods_hash[name] .merge!(:methods => nested_methods) if nested_methods.present? end end case type when :has_one result[name] = nested_value.as_json() when :has_many result[name] = nested_value.collect { |assoc| assoc.as_json() } end end end end else result.merge!(self.attributes) end if [:methods] [:methods].each do |name| result[name] = json_friendly_value(self.send(name), .except(:methods) ) unless name.is_a?(Hash) end end result end |
#hash ⇒ Object
94 95 96 |
# File 'lib/podio/active_podio/base.rb', line 94 def hash self.id.hash if self.respond_to?(:id) end |
#initialize_attributes(attributes) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/podio/active_podio/base.rb', line 29 def initialize_attributes(attributes) attributes.each do |key, value| if self.respond_to?("#{key}=".to_sym) self.send("#{key}=".to_sym, value) else is_association_hash = value.is_a?(Hash) && self._associations.has_key?(key.to_sym) && self._associations[key.to_sym] == :has_one && (self.send(key.to_sym).respond_to?(:attributes) || self.send(key.to_sym).nil?) if valid_attributes.include?(key.to_sym) || is_association_hash # Initialize nested object to get correctly casted values set back, unless the given values are all blank if is_association_hash # Merge existing values with given values and do recursive initalize call to get values casted correctly if self.send(key.to_sym).present? existing_attributes = self.send(key.to_sym).try(:attributes) || {} value.reverse_merge!(existing_attributes) self.send(key.to_sym).initialize_attributes(value) else self.send(:[]=, key.to_sym, value) end attributes = self.send(key.to_sym).try(:attributes) if attributes.present? && any_values_present_recursive?(attributes.values) value = attributes else value = nil end end self.send(:[]=, key.to_sym, value) end end end end |
#new_record? ⇒ Boolean
65 66 67 |
# File 'lib/podio/active_podio/base.rb', line 65 def new_record? ! (self.respond_to?(:id) && self.id.present?) end |
#parent_model ⇒ Object
147 148 149 |
# File 'lib/podio/active_podio/base.rb', line 147 def parent_model @belongs_to[:model] if @belongs_to.present? end |
#persisted? ⇒ Boolean
61 62 63 |
# File 'lib/podio/active_podio/base.rb', line 61 def persisted? ! self.new_record? end |
#to_param ⇒ Object
69 70 71 72 73 |
# File 'lib/podio/active_podio/base.rb', line 69 def to_param local_id = self.id if self.respond_to?(:id) local_id = nil if local_id == self.object_id # Id still returns object_id in Ruby 1.8.7, JRuby and Rubinius local_id.try(:to_s) end |