Skip to content

Commit

Permalink
convert constants to class methods
Browse files Browse the repository at this point in the history
  • Loading branch information
jbender committed May 18, 2016
1 parent bbc4335 commit 0297fbe
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 74 deletions.
74 changes: 40 additions & 34 deletions motion/address_book/ab/models/multi_valued_attribute.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,42 @@ module AB
class MultiValuedAttribute
INITIALIZATION_ERROR =
"MultiValuedAttribute must be initialized with an ABMultiValue or Hash"
LABEL_MAP = {
:mobile => KABPersonPhoneMobileLabel,
:iphone => KABPersonPhoneIPhoneLabel,
:main => KABPersonPhoneMainLabel,
:home_fax => KABPersonPhoneHomeFAXLabel,
:work_fax => KABPersonPhoneWorkFAXLabel,
:pager => KABPersonPhonePagerLabel,
:work => KABWorkLabel,
:home => KABHomeLabel,
:other => KABOtherLabel,
:home_page => KABPersonHomePageLabel,
:anniversary => KABPersonAnniversaryLabel
}
PROPERTY_MAP = {
KABPersonAddressStreetKey => :street,
KABPersonAddressCityKey => :city,
KABPersonAddressStateKey => :state,
KABPersonAddressZIPKey => :postalcode,
KABPersonAddressCountryKey => :country,
KABPersonAddressCountryCodeKey => :country_code,

KABPersonSocialProfileServiceKey => :service,
KABPersonSocialProfileURLKey => :url,
KABPersonSocialProfileUsernameKey => :username,
KABPersonSocialProfileUserIdentifierKey => :userid,

# these keys are identical to the SocialProfile keys above
KABPersonInstantMessageServiceKey => :service,
KABPersonInstantMessageUsernameKey => :username
}

def self.LABEL_MAP
{
:mobile => KABPersonPhoneMobileLabel,
:iphone => KABPersonPhoneIPhoneLabel,
:main => KABPersonPhoneMainLabel,
:home_fax => KABPersonPhoneHomeFAXLabel,
:work_fax => KABPersonPhoneWorkFAXLabel,
:pager => KABPersonPhonePagerLabel,
:work => KABWorkLabel,
:home => KABHomeLabel,
:other => KABOtherLabel,
:home_page => KABPersonHomePageLabel,
:anniversary => KABPersonAnniversaryLabel
}
end

def self.PROPERTY_MAP
{
KABPersonAddressStreetKey => :street,
KABPersonAddressCityKey => :city,
KABPersonAddressStateKey => :state,
KABPersonAddressZIPKey => :postalcode,
KABPersonAddressCountryKey => :country,
KABPersonAddressCountryCodeKey => :country_code,

KABPersonSocialProfileServiceKey => :service,
KABPersonSocialProfileURLKey => :url,
KABPersonSocialProfileUsernameKey => :username,
KABPersonSocialProfileUserIdentifierKey => :userid,

# these keys are identical to the SocialProfile keys above
KABPersonInstantMessageServiceKey => :service,
KABPersonInstantMessageUsernameKey => :username
}
end

def initialize(value_array_or_record)
@values = []
Expand Down Expand Up @@ -96,7 +102,7 @@ def with_label(label)

def ab_dictionary_from_hash(hash)
ab_record = {}
PROPERTY_MAP.each do |ab_key, ruby_key|
self.class.PROPERTY_MAP.each do |ab_key, ruby_key|
ab_record[ab_key] = hash[ruby_key] if hash[ruby_key]
end
ab_record.empty? ? nil : ab_record
Expand Down Expand Up @@ -126,7 +132,7 @@ def hash_from_ab_index(index)

def hash_from_ab_dictionary(dictionary)
hash = {}
PROPERTY_MAP.each do |ab_key, attr_key|
self.class.PROPERTY_MAP.each do |ab_key, attr_key|
hash[attr_key] = dictionary[ab_key] if dictionary[ab_key]
end
hash.empty? ? nil : hash
Expand All @@ -137,13 +143,13 @@ def label_at_index(index)
end

def labeled_hash_at_index(index)
label = LABEL_MAP.invert[label_at_index(index)]
label = self.class.LABEL_MAP.invert[label_at_index(index)]
hash = hash_from_ab_index(index)
hash.merge(label: label)
end

def localized_label(symbol)
LABEL_MAP[symbol] || symbol.to_s
self.class.LABEL_MAP[symbol] || symbol.to_s
end

def parse_value_array!(value_array)
Expand Down
91 changes: 51 additions & 40 deletions motion/address_book/ab/models/person.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,50 @@ module AB
class Person
include AddressBook::Shared::Hashable

MULTI_VALUE_PROPERTY_MAP = {
3 => :phones, # KABPersonPhoneProperty
4 => :emails, # KABPersonEmailProperty
5 => :addresses, # KABPersonAddressProperty
12 => :dates, # KABPersonDateProperty
13 => :im_profiles, # KABPersonInstantMessageProperty
22 => :urls, # KABPersonURLProperty
23 => :related_names, # KABPersonRelatedNamesProperty
46 => :social_profiles # KABPersonSocialProfileProperty
}
PROPERTY_MAP = {
0 => :first_name, # KABPersonFirstNameProperty
1 => :last_name, # KABPersonLastNameProperty
6 => :middle_name, # KABPersonMiddleNameProperty
# n/a => :first_name_phonetic, # KABPersonFirstNamePhoneticProperty
# n/a => :last_name_phonetic, # KABPersonLastNamePhoneticProperty
# n/a => :middle_name_phonetic, # KABPersonMiddleNamePhoneticProperty
10 => :organization, # KABPersonOrganizationProperty
11 => :department, # KABPersonDepartmentProperty
14 => :note, # KABPersonNoteProperty
17 => :birthday, # KABPersonBirthdayProperty
18 => :job_title, # KABPersonJobTitleProperty
19 => :nickname, # KABPersonNicknameProperty
20 => :prefix, # KABPersonPrefixProperty
21 => :suffix, # KABPersonSuffixProperty
26 => :creation_date, # KABPersonCreationDateProperty
27 => :modification_date # KABPersonModificationDateProperty
}
TYPE_MAP = {
0 => :person, # KABPersonKindPerson
1 => :organization # KABPersonKindOrganization
}
ALL_PROPERTIES = PROPERTY_MAP.merge(MULTI_VALUE_PROPERTY_MAP)
def self.MULTI_VALUE_PROPERTY_MAP
{
KABPersonPhoneProperty => :phones,
KABPersonEmailProperty => :emails,
KABPersonAddressProperty => :addresses,
KABPersonDateProperty => :dates,
KABPersonInstantMessageProperty => :im_profiles,
KABPersonURLProperty => :urls,
KABPersonRelatedNamesProperty => :related_names,
KABPersonSocialProfileProperty => :social_profiles
}
end

def self.PROPERTY_MAP
{
KABPersonFirstNameProperty => :first_name,
KABPersonLastNameProperty => :last_name,
KABPersonMiddleNameProperty => :middle_name,
# KABPersonFirstNamePhoneticProperty => :first_name_phonetic,
# KABPersonLastNamePhoneticProperty => :last_name_phonetic,
# KABPersonMiddleNamePhoneticProperty => :middle_name_phonetic,
KABPersonOrganizationProperty => :organization,
KABPersonDepartmentProperty => :department,
KABPersonNoteProperty => :note,
KABPersonBirthdayProperty => :birthday,
KABPersonJobTitleProperty => :job_title,
KABPersonNicknameProperty => :nickname,
KABPersonPrefixProperty => :prefix,
KABPersonSuffixProperty => :suffix,
KABPersonCreationDateProperty => :creation_date,
KABPersonModificationDateProperty => :modification_date
}
end

def self.TYPE_MAP
{
KABPersonKindPerson => :person,
KABPersonKindOrganization => :organization
}
end

def self.ALL_PROPERTIES
self.PROPERTY_MAP.merge(self.MULTI_VALUE_PROPERTY_MAP)
end

attr_accessor(
# Single-values
Expand Down Expand Up @@ -122,7 +133,7 @@ def save!
instance_variables.each do |variable|
key = Shared::Utilities.variable_as_sym(var)
variable_sym = variable_as_sym(variable)
ab_field = ALL_PROPERTIES.invert[variable_sym]
ab_field = self.class.ALL_PROPERTIES.invert[variable_sym]
next unless ab_field
new_value = instance_variable_get(variable_sym)
new_value ? set_field(ab_field, new_value) : remove_field(ab_field)
Expand Down Expand Up @@ -165,15 +176,15 @@ def get_field(ab_field)
end

def multi_valued_field?(ab_field)
MULTI_VALUE_PROPERTY_MAP.keys.include? ab_field
self.class.MULTI_VALUE_PROPERTY_MAP.keys.include? ab_field
end

def parse_record!(ab_record)
@record_reference = ab_record

@type = TYPE_MAP[get_field(KABPersonKindProperty)]
@type = self.class.TYPE_MAP[get_field(KABPersonKindProperty)]

ALL_PROPERTIES.each do |ab_field, attribute|
self.class.ALL_PROPERTIES.each do |ab_field, attribute|
record_property = get_field(ab_field)
next unless record_property
instance_variable_set("@#{attribute}".to_sym, record_property)
Expand All @@ -185,9 +196,9 @@ def parse_record!(ab_record)
def parse_hash!(hash)
record_reference # Initializes a new record

@type = TYPE_MAP.invert[hash[:type]]
@type = self.class.TYPE_MAP.invert[hash[:type]]

ALL_PROPERTIES.each do |_ab_field, attribute|
self.class.ALL_PROPERTIES.each do |_ab_field, attribute|
instance_variable_set("@#{attribute}".to_sym, hash[attribute])
end

Expand Down Expand Up @@ -221,7 +232,7 @@ def set_field(ab_field, value)
end

def single_valued_field?(ab_field)
PROPERTY_MAP.keys.include? ab_field
self.class.PROPERTY_MAP.keys.include? ab_field
end
end
end
Expand Down

0 comments on commit 0297fbe

Please sign in to comment.