Class: SimpleCrowd::CrowdEntity
- Inherits:
-
Object
- Object
- SimpleCrowd::CrowdEntity
- Defined in:
- lib/simple_crowd/crowd_entity.rb
Class Method Summary collapse
- .attribute(attr_name, opts = {}) ⇒ Object
- .attributes ⇒ Object
- .defaults ⇒ Object
- .from_soap(data) ⇒ Object
- .properties ⇒ Object
- .property(property_name, opts = {}) ⇒ Object
- .search_restriction_for(property_key) ⇒ Object
- .soap_key_for(property_key) ⇒ Object
Instance Method Summary collapse
- #[](key) ⇒ Object
- #clean ⇒ Object
- #dirty?(prop = nil) ⇒ Boolean
- #dirty_attributes ⇒ Object
- #dirty_properties ⇒ Object
- #errors ⇒ Object
-
#initialize(attrs = {}) ⇒ CrowdEntity
constructor
A new instance of CrowdEntity.
- #inspect ⇒ Object
- #to_hash ⇒ Object
- #to_soap ⇒ Object
- #update(properties_and_attributes) ⇒ Object
- #valid? ⇒ Boolean
Constructor Details
#initialize(attrs = {}) ⇒ CrowdEntity
Returns a new instance of CrowdEntity.
4 5 6 7 8 9 10 11 12 13 |
# File 'lib/simple_crowd/crowd_entity.rb', line 4 def initialize(attrs={}) self.class.defaults.each do |key, val| send(:"#{key.to_s}=", val) end attrs.each do |key, val| send(:"#{key.to_s}=", val) if self.respond_to?("#{key.to_s}=", true) end dirty_properties.clear end |
Class Method Details
.attribute(attr_name, opts = {}) ⇒ Object
54 55 56 57 58 59 |
# File 'lib/simple_crowd/crowd_entity.rb', line 54 def self.attribute(attr_name, opts={}) attr_name = attr_name.to_sym @attributes ||= [] @attributes << attr_name self.property(attr_name, opts) end |
.attributes ⇒ Object
65 66 67 |
# File 'lib/simple_crowd/crowd_entity.rb', line 65 def self.attributes @attributes.freeze end |
.defaults ⇒ Object
69 70 71 |
# File 'lib/simple_crowd/crowd_entity.rb', line 69 def self.defaults @defaults.freeze end |
.from_soap(data) ⇒ Object
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/simple_crowd/crowd_entity.rb', line 124 def self.from_soap(data) data = data.dup if data # Merge attributes into the main hash if data && data[:attributes] && data[:attributes][:soap_attribute] attrs = {} attrs = data[:attributes][:soap_attribute].inject({}) do |hash, attr| hash[attr[:name]] = attr[:values][:string] hash end data.delete :attributes data.merge! attrs end # Clean soap values data.each do |(key, val)| if val.is_a?(Hash) && val[:"@xmlns"] val.delete(:"@xmlns") data[key] = nil if val.empty? end end # Map soap values to property values if @soap_to_property_map data = data.inject({}) do |hash, (key, val)| key = :"#{key}" if @soap_to_property_map.has_key?(key) hash[@soap_to_property_map[key]] = val else hash[key] = val end hash end end self.new data end |
.properties ⇒ Object
61 62 63 |
# File 'lib/simple_crowd/crowd_entity.rb', line 61 def self.properties @properties.freeze end |
.property(property_name, opts = {}) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/simple_crowd/crowd_entity.rb', line 15 def self.property(property_name, opts={}) property_name = property_name.to_sym @properties ||= [] @properties << property_name unless @properties.include?(property_name) class_eval <<-PROPERTY, __FILE__, __LINE__ + 1 def #{property_name} @#{property_name} end def #{property_name}=(val) (dirty_properties << :#{property_name}).uniq! unless val == @#{property_name} @#{property_name} = val end PROPERTY if opts[:immutable] private :"#{property_name}=" end if opts[:map_soap] v = :"#{opts[:map_soap]}" @soap_to_property_map ||= {} @property_to_soap_map ||= {} @soap_to_property_map[v] = property_name @property_to_soap_map[property_name] = v end if opts[:search_restriction] @property_to_search_restriction_map ||= {} @property_to_search_restriction_map[property_name] = opts[:search_restriction] end if opts[:default] @defaults ||= {} @defaults[property_name] = opts[:default] end end |
.search_restriction_for(property_key) ⇒ Object
190 191 192 193 194 195 196 |
# File 'lib/simple_crowd/crowd_entity.rb', line 190 def self.search_restriction_for(property_key) property_key = :"#{property_key}" if @property_to_search_restriction_map && @property_to_search_restriction_map.has_key?(property_key) return @property_to_search_restriction_map[property_key] end property_key end |
.soap_key_for(property_key) ⇒ Object
182 183 184 185 186 187 188 |
# File 'lib/simple_crowd/crowd_entity.rb', line 182 def self.soap_key_for(property_key) property_key = :"#{property_key}" if @property_to_soap_map && @property_to_soap_map.has_key?(property_key) return @property_to_soap_map[property_key] end property_key end |
Instance Method Details
#[](key) ⇒ Object
96 97 98 |
# File 'lib/simple_crowd/crowd_entity.rb', line 96 def [](key) respond_to?(:"#{key}") ? send(:"#{key}") : nil end |
#clean ⇒ Object
92 93 94 |
# File 'lib/simple_crowd/crowd_entity.rb', line 92 def clean @dirty_properties.clear end |
#dirty?(prop = nil) ⇒ Boolean
81 82 83 |
# File 'lib/simple_crowd/crowd_entity.rb', line 81 def dirty?(prop=nil) prop.nil? ? !@dirty_properties.empty? : @dirty_properties.include?(prop) end |
#dirty_attributes ⇒ Object
77 78 79 |
# File 'lib/simple_crowd/crowd_entity.rb', line 77 def dirty_attributes dirty_properties & self.class.attributes end |
#dirty_properties ⇒ Object
73 74 75 |
# File 'lib/simple_crowd/crowd_entity.rb', line 73 def dirty_properties @dirty_properties ||= Array.new end |
#errors ⇒ Object
104 105 106 |
# File 'lib/simple_crowd/crowd_entity.rb', line 104 def errors {} end |
#inspect ⇒ Object
115 116 117 118 119 120 121 122 |
# File 'lib/simple_crowd/crowd_entity.rb', line 115 def inspect ret = "#<#{self.class.to_s}" self.class.properties.each do |key| ret << " #{key}=#{self.instance_variable_get("@#{key}").inspect}" end ret << ">" ret end |
#to_hash ⇒ Object
108 109 110 111 112 113 |
# File 'lib/simple_crowd/crowd_entity.rb', line 108 def to_hash (self.class.properties || []).inject({}) do |hash, key| hash[key] = send(key) if respond_to?(key) hash end end |
#to_soap ⇒ Object
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/simple_crowd/crowd_entity.rb', line 162 def to_soap properties = self.class.properties || [] attributes = self.class.attributes || [] data = {} data[:attributes] = {:SOAPAttribute => []} unless attributes.empty? properties.each do |prop| key = self.class.soap_key_for(prop) val = send(prop) if attributes.include?(prop) data[:attributes][:SOAPAttribute] << {:name => key, :values => {:string => val}} else data[key] = val end end data end |
#update(properties_and_attributes) ⇒ Object
85 86 87 88 89 90 |
# File 'lib/simple_crowd/crowd_entity.rb', line 85 def update(properties_and_attributes) return unless properties_and_attributes properties_and_attributes.each do |key, val| send(:"#{key.to_s}=", val) if self.respond_to?("#{key.to_s}=") end end |
#valid? ⇒ Boolean
100 101 102 |
# File 'lib/simple_crowd/crowd_entity.rb', line 100 def valid? errors.empty? end |