module MoySklad::Client::CustomAttrBehavior
Public Instance Methods
empty?()
click to toggle source
Check is nested resource empty or not
# File lib/moy_sklad/client/attribute.rb, line 110 def empty? attributes.empty? end
get_attribute(info)
click to toggle source
Set attribute in “attributes” array of the object.
@attr info [Hash/String] hash with at least [:uuid] key or String with uuid.
if hash containts [:value] key then value will be returned instead of plceholder object.
# File lib/moy_sklad/client/attribute.rb, line 76 def get_attribute(info) uuid = info if info.is_a?(String) && (info.length == 36) if info.is_a?(Hash) info = HashWithIndifferentAccess.new(info) uuid = info[:uuid] if info.is_a?(Hash) && (!info[:uuid].nil? && info[:uuid].length == 36) end fail ArgumentError, "Argument should be uuid string or hash with [:uuid] key" if uuid.nil? a = find_object(:attribute, :metadataUuid, uuid) if info.is_a?(Hash) && info.has_key?(:value) return a.send(info[:value]) if a.respond_to?(info[:value]) nil else a end end
remove_attr(attribute)
click to toggle source
Remove attribute from object
# File lib/moy_sklad/client/attribute.rb, line 115 def remove_attr(attribute) attributes.delete(attribute) known_attributes.delete(attribute) end
set_attribute(info, value)
click to toggle source
Get attrubute from the “attributes” array of the object.
@attr info [Hash] attribute info hash, should have keys: {:uuid, :value} @attr value [Object] value to set
# File lib/moy_sklad/client/attribute.rb, line 48 def set_attribute(info, value) fail ArgumentError, "Argument should be hash with at least [:uuid, :value] keys" unless info.is_a?(Hash) info = HashWithIndifferentAccess.new(info) fail ArgumentError, "You must provide keys: [:uuid, :value]" unless info.has_key?(:uuid) || info.has_key?(:value) v = find_object(:attribute, :metadataUuid, info[:uuid]) if v.nil? data = { metadataUuid: info[:uuid] } data[info[:value]] = value a = create_and_load_resource('Attribute', data) if self.to_a(:attribute).empty? self.attribute = [a] else self.attribute << a end else v.send("#{info[:value]}=".to_sym, value) end end
to_a(type)
click to toggle source
Get object attribute as array. object.to_a(:some_type) will always return array and of course it can be empty.
# File lib/moy_sklad/client/attribute.rb, line 99 def to_a(type) value = self.send(type) return [] if value.nil? || value.is_a?(MoySklad::Client::Attribute::MissingAttr) # Convert self.send("#{type}=", [value]) unless value.is_a?(Array) self.send(type) end