Class: Kaui::Base

Inherits:
Object
  • Object
show all
Extended by:
ActiveModel::Naming
Includes:
ActiveModel::Conversion, ActiveModel::Validations
Defined in:
app/models/kaui/base.rb

Constant Summary collapse

@@attribute_names =
{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Base

Returns a new instance of Base.



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'app/models/kaui/base.rb', line 35

def initialize(attributes = {})
  # We can come here either from the Killbill API (attributes will be a JSON hash,
  # with camel cased keys) or from e.g. Rails forms (attributes will be a hash with
  # snake cased keys).
  # Either way, convert the keys to snake case as our attributes are snake cased.
  self.attributes = Kaui::Base.convert_hash_keys(attributes)

  # Make has_many associations return [] instead of nil by default
  @@attribute_names[self.class.name].each do |name, type_desc|
    val = send("#{name}")
    send("#{name}=", []) if val.nil? and !type_desc.nil? and type_desc[:cardinality] == :many
  end

  # Mark the record as persisted if we have an id
  @persisted = to_param.present?

  # Errors for form validations
  @errors = ActiveModel::Errors.new(self)
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



6
7
8
# File 'app/models/kaui/base.rb', line 6

def errors
  @errors
end

Class Method Details

.allObject



160
161
162
# File 'app/models/kaui/base.rb', line 160

def self.all
  []
end

.camelize(value) ⇒ Object



107
108
109
110
111
112
113
114
115
116
# File 'app/models/kaui/base.rb', line 107

def self.camelize(value)
  case value
    when Array
      value.map {|v| camelize(v) }
    when Hash
      value.inject({}) {|result, (k, v)| result.merge!(k.to_s.camelize(:lower).to_sym => camelize(v)) }
    else
      value
  end
end

.convert_hash_keys(value) ⇒ Object

Convert a hash with camel cased keys into a hash with snake cased keys:

{ :accountId => 12 } becomes { :account_id => 12 }


122
123
124
125
126
127
128
129
# File 'app/models/kaui/base.rb', line 122

def self.convert_hash_keys(value)
  case value
    when Hash
      Hash[value.map { |k, v| [k.to_s.underscore.to_sym, convert_hash_keys(v)] }]
    else
      value
   end
end

.countObject



164
165
166
# File 'app/models/kaui/base.rb', line 164

def self.count
  all.count
end

.define_attr(*args) ⇒ Object



10
11
12
13
14
15
16
# File 'app/models/kaui/base.rb', line 10

def self.define_attr(*args)
  send("attr_accessor".to_sym, *args)
  args.each do |attr_name|
    @@attribute_names[self.name] = {} unless @@attribute_names[self.name]
    @@attribute_names[self.name][attr_name.to_sym] = { :cardinality => :one }
  end
end

.find(id) ⇒ Object



168
169
170
# File 'app/models/kaui/base.rb', line 168

def self.find(id)
  nil
end

.from_json(json_text) ⇒ Object



30
31
32
33
# File 'app/models/kaui/base.rb', line 30

def self.from_json(json_text)
  json_hash = ActiveSupport::JSON.decode(json_text)
  return self.new(json_hash)
end

.has_many(attr_name, type = nil) ⇒ Object



24
25
26
27
28
# File 'app/models/kaui/base.rb', line 24

def self.has_many(attr_name, type = nil)
  send("attr_accessor".to_sym, attr_name)
  @@attribute_names[self.name] = {} unless @@attribute_names[self.name]
  @@attribute_names[self.name][attr_name.to_sym] = { :type => type, :cardinality => :many }
end

.has_one(attr_name, type = nil) ⇒ Object



18
19
20
21
22
# File 'app/models/kaui/base.rb', line 18

def self.has_one(attr_name, type = nil)
  send("attr_accessor".to_sym, attr_name)
  @@attribute_names[self.name] = {} unless @@attribute_names[self.name]
  @@attribute_names[self.name][attr_name.to_sym] = { :type => type, :cardinality => :one }
end

.human_attribute_name(attr, options = {}) ⇒ Object



152
153
154
# File 'app/models/kaui/base.rb', line 152

def self.human_attribute_name(attr, options = {})
  attr
end

.lookup_ancestorsObject



156
157
158
# File 'app/models/kaui/base.rb', line 156

def self.lookup_ancestors
  [self]
end

.to_money(amount, currency) ⇒ Object



99
100
101
102
103
104
105
# File 'app/models/kaui/base.rb', line 99

def self.to_money(amount, currency)
  begin
    return Money.new(amount.to_f * 100, currency)
  rescue => e
  end if currency.present?
  Money.new(amount.to_f * 100, "USD")
end

Instance Method Details

#==(other) ⇒ Object



131
132
133
# File 'app/models/kaui/base.rb', line 131

def ==(other)
  !other.nil? && self.class == other.class && self.to_hash == other.to_hash
end

#attributes=(values) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'app/models/kaui/base.rb', line 55

def attributes=(values)
  values.each do |name, value|
    type_desc = @@attribute_names[self.class.name][name.to_sym]
    unless type_desc.nil?
      type = type_desc[:type]
      if type_desc[:cardinality] == :many && !type.nil? && value.is_a?(Array)
        newValue = []
        value.each do |val|
          if val.is_a?(Hash)
            newValue << type.to_s.constantize.new(val)
          else
            newValue << val
          end
        end
        value = newValue
      elsif type_desc[:cardinality] == :one && !type.nil? && value.is_a?(Hash)
        value = type.to_s.constantize.new(value)
      end
    end
    send("#{name}=", value)
  end
end

#destroyObject



182
183
184
185
# File 'app/models/kaui/base.rb', line 182

def destroy
  @errors.add(:destroy, 'Destroying this object is not yet supported')
  false
end

#new_record?Boolean

Returns:

  • (Boolean)


139
140
141
# File 'app/models/kaui/base.rb', line 139

def new_record?
  !persisted?
end

#persisted?Boolean

Returns:

  • (Boolean)


135
136
137
# File 'app/models/kaui/base.rb', line 135

def persisted?
  @persisted
end

#read_attribute_for_validation(attr) ⇒ Object



148
149
150
# File 'app/models/kaui/base.rb', line 148

def read_attribute_for_validation(attr)
  send(attr)
end

#saveObject



172
173
174
175
# File 'app/models/kaui/base.rb', line 172

def save
  @errors.add(:save, 'Saving this object is not yet supported')
  false
end

#to_hashObject



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'app/models/kaui/base.rb', line 78

def to_hash
  result = {}
  @@attribute_names[self.class.name].each do |name, type_desc|
    val = send("#{name}")
    unless val.nil? || type_desc.nil?
      type = type_desc[:type]
      if type_desc[:cardinality] == :many && !type.nil? && val.is_a?(Array)
        newVal = []
        val.each do |curVal|
          newVal << curVal.as_json(:root => false)
        end
        val = newVal
      elsif type_desc[:cardinality] == :one && !type.nil?
        val = val.as_json(:root => false)
      end
      result[name.to_s] = val
    end
  end
  result
end

#to_paramObject



143
144
145
146
# File 'app/models/kaui/base.rb', line 143

def to_param
  # id is a string (killbill UUID)
  @id
end

#update_attributes(tag_definition) ⇒ Object



177
178
179
180
# File 'app/models/kaui/base.rb', line 177

def update_attributes(tag_definition)
  @errors.add(:update, 'Updating this object is not yet supported')
  false
end