Class: PipeRocket::Entity
- Inherits:
-
Object
- Object
- PipeRocket::Entity
- Defined in:
- lib/pipe_rocket/entity.rb
Direct Known Subclasses
Deal, Field, File, MailMessage, Note, Organization, Person, Pipeline, Stage, User
Instance Method Summary collapse
-
#assign_custom_fields(key_name_hash, entity_hash) ⇒ Object
Assign custom fields, using their names but not keys.
-
#initialize(hash) ⇒ Entity
constructor
A new instance of Entity.
-
#transform_field_name(key, name) ⇒ Object
Override custom field name if it present in CUSTOM_FIELD_NAMES.
Constructor Details
#initialize(hash) ⇒ Entity
Returns a new instance of Entity.
3 4 5 6 7 8 9 10 |
# File 'lib/pipe_rocket/entity.rb', line 3 def initialize(hash) hash.each do |k,v| if k[0] =~ /[a-zA-Z_]/ instance_variable_set("@#{k}",v) self.class.class_eval {attr_accessor k} end end end |
Instance Method Details
#assign_custom_fields(key_name_hash, entity_hash) ⇒ Object
Assign custom fields, using their names but not keys
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/pipe_rocket/entity.rb', line 13 def assign_custom_fields(key_name_hash, entity_hash) names = key_name_hash.map do |key, field| name = field.name name = name.underscore.gsub(' ','_') name = name.gsub('%','percent').gsub(/[^a-zA-Z0-9_]/,'') name = transform_field_name(key, name) if field.field_type == 'enum' option_id = entity_hash[key] res_field = field.dup res_field.option_id = option_id instance_variable_set("@#{name}", res_field) else instance_variable_set("@#{name}", entity_hash[key]) end name end self.class.class_eval {attr_accessor *names} end |
#transform_field_name(key, name) ⇒ Object
Override custom field name if it present in CUSTOM_FIELD_NAMES
33 34 35 36 37 38 |
# File 'lib/pipe_rocket/entity.rb', line 33 def transform_field_name(key, name) hash = ::CUSTOM_FIELD_NAMES class_name = self.class.name.demodulize.underscore.to_sym return name if hash.nil? || hash[class_name].nil? || hash[class_name][key.to_sym].nil? hash[class_name][key.to_sym] end |