Module: Moneybird::Resource::ClassMethods

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



51
52
53
# File 'lib/moneybird/resource.rb', line 51

def attributes
  @attributes
end

#loggerObject



58
59
60
61
62
# File 'lib/moneybird/resource.rb', line 58

def logger
  @logger ||= begin
    Logger.new(STDOUT)
  end
end

#nillable_attributesObject (readonly)

Returns the value of attribute nillable_attributes.



51
52
53
# File 'lib/moneybird/resource.rb', line 51

def nillable_attributes
  @nillable_attributes
end

Instance Method Details

#build(attributes) ⇒ Object



54
55
56
# File 'lib/moneybird/resource.rb', line 54

def build(attributes)
  new(attributes)
end

#has_attributes(attributes) ⇒ Object



68
69
70
# File 'lib/moneybird/resource.rb', line 68

def has_attributes(attributes)
  attr_accessor(*@attributes = attributes)
end

#has_boolean_attributes(attributes) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/moneybird/resource.rb', line 76

def has_boolean_attributes(attributes)
  attributes.each do |attribute|
    define_method(attribute) do
      input = instance_variable_get('@' + attribute)
      if input.kind_of?(String)
        if input == 'true'
          true
        elsif input == 'false'
          false
        else
          nil
        end
      else
        input
      end
    end
  end
end

#has_datetime_attributes(attributes) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/moneybird/resource.rb', line 108

def has_datetime_attributes(attributes)
  attributes.each do |attribute|
    define_method(attribute) do
      input = instance_variable_get('@' + attribute)
      if input.kind_of?(Time)
        input
      elsif input.kind_of?(Date)
        input.to_time
      elsif input.kind_of?(String)
        input.strip == '' ? nil : Time.parse(input)
      end
    end
  end
end

#has_integer_attributes(attributes) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/moneybird/resource.rb', line 95

def has_integer_attributes(attributes)
  attributes.each do |attribute|
    define_method(attribute) do
      input = instance_variable_get('@' + attribute)
      if input.kind_of?(String)
        input.to_i
      else
        input
      end
    end
  end
end

#has_nillable_attributes(attributes) ⇒ Object



72
73
74
# File 'lib/moneybird/resource.rb', line 72

def has_nillable_attributes(attributes)
  @nillable_attributes = attributes
end

#resourceObject



64
65
66
# File 'lib/moneybird/resource.rb', line 64

def resource
  self.name.split('::').last.underscore
end