Module: MoySklad::Client::CustomAttrBehavior
- Defined in:
- lib/moy_sklad/client/attribute.rb
Instance Method Summary collapse
-
#empty? ⇒ Boolean
Check is nested resource empty or not.
-
#get_attribute(info) ⇒ Object
Set attribute in “attributes” array of the object.
-
#remove_attr(attribute) ⇒ Object
Remove attribute from object.
-
#set_attribute(info, value) ⇒ Object
Get attrubute from the “attributes” array of the object.
-
#to_a(type) ⇒ Object
Get object attribute as array.
Instance Method Details
#empty? ⇒ Boolean
Check is nested resource empty or not
110 111 112 |
# File 'lib/moy_sklad/client/attribute.rb', line 110 def empty? attributes.empty? end |
#get_attribute(info) ⇒ Object
Set attribute in “attributes” array of the object.
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# 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) ⇒ Object
Remove attribute from object
115 116 117 118 |
# 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) ⇒ Object
Get attrubute from the “attributes” array of the object.
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# 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) ⇒ Object
Get object attribute as array. object.to_a(:some_type) will always return array and of course it can be empty.
99 100 101 102 103 104 105 106 107 |
# 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 |