Class: Colppy::Resource

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
lib/colppy/resource.rb

Direct Known Subclasses

Company, Customer, Invoice, Product, User

Constant Summary

Constants included from Utils

Utils::ATTRIBUTES_MAPPER, Utils::DATA_KEYS_SETTERS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils

rename_params_hash

Constructor Details

#initialize(params) ⇒ Resource

Returns a new instance of Resource.



32
33
34
35
36
37
38
# File 'lib/colppy/resource.rb', line 32

def initialize(params)
  @data = rename_params_hash(
    params,
    self.class::ATTRIBUTES_MAPPER,
    self.class::DATA_KEYS_SETTERS
  )
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



3
4
5
# File 'lib/colppy/resource.rb', line 3

def data
  @data
end

Instance Method Details

#[]=(key, value) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/colppy/resource.rb', line 47

def []=(key, value)
  key_sym = key.to_sym
  if PROTECTED_DATA_KEYS.include?(key_sym)
    raise ResourceError.new("You cannot change any of this values: #{PROTECTED_DATA_KEYS.join(", ")} manually")
  end
  if DATA_KEYS_SETTERS.include?[key]
    @data[key_sym] = value
  elsif new_key = ATTRIBUTES_MAPPER[key_sym]
    @data[new_key] = value if DATA_KEYS_SETTERS.include?(new_key)
  else
    raise DataError.new("There is no attribute named :#{key_sym}, or you cannot change it manually this way. Try initializing another #{self.class.name} with that attribute as the argument")
  end
end

#inspectObject



40
41
42
43
44
45
# File 'lib/colppy/resource.rb', line 40

def inspect
  formatted_attrs = attr_inspect.map do |attr|
    "#{attr}: #{send(attr).inspect}"
  end
  "#<#{self.class.name} #{formatted_attrs.join(", ")}>"
end