Class: ChargeBee::Model

Inherits:
Object
  • Object
show all
Defined in:
lib/chargebee/models/model.rb

Direct Known Subclasses

Addon, Addon::Tier, Address, AdvanceInvoiceSchedule, AdvanceInvoiceSchedule::FixedIntervalSchedule, AdvanceInvoiceSchedule::SpecificDatesSchedule, Card, Comment, Contact, ContractTerm, Coupon, CouponCode, CouponSet, CreditNote, CreditNote::Allocation, CreditNote::Discount, CreditNote::LineItem, CreditNote::LineItemDiscount, CreditNote::LineItemTax, CreditNote::LineItemTier, CreditNote::LinkedRefund, CreditNote::Tax, CreditNoteEstimate, CreditNoteEstimate::Discount, CreditNoteEstimate::LineItem, CreditNoteEstimate::LineItemDiscount, CreditNoteEstimate::LineItemTax, CreditNoteEstimate::LineItemTier, CreditNoteEstimate::Tax, Customer, Customer::Balance, Customer::BillingAddress, Customer::ChildAccountAccess, Customer::Contact, Customer::ParentAccountAccess, Customer::PaymentMethod, Customer::ReferralUrl, Customer::Relationship, Download, Estimate, Event, Event::Webhook, Export, Export::Download, Gift, Gift::GiftReceiver, Gift::GiftTimeline, Gift::Gifter, Hierarchy, HostedPage, Invoice, Invoice::AdjustmentCreditNote, Invoice::AppliedCredit, Invoice::BillingAddress, Invoice::Discount, Invoice::DunningAttempt, Invoice::IssuedCreditNote, Invoice::LineItem, Invoice::LineItemDiscount, Invoice::LineItemTax, Invoice::LineItemTier, Invoice::LinkedOrder, Invoice::LinkedPayment, Invoice::Note, Invoice::ShippingAddress, Invoice::Tax, InvoiceEstimate, InvoiceEstimate::Discount, InvoiceEstimate::LineItem, InvoiceEstimate::LineItemDiscount, InvoiceEstimate::LineItemTax, InvoiceEstimate::LineItemTier, InvoiceEstimate::Tax, Order, Order::BillingAddress, Order::LineItemDiscount, Order::LineItemTax, Order::LinkedCreditNote, Order::OrderLineItem, Order::ShippingAddress, PaymentIntent, PaymentIntent::PaymentAttempt, PaymentSource, PaymentSource::AmazonPayment, PaymentSource::BankAccount, PaymentSource::Card, PaymentSource::Paypal, Plan, Plan::ApplicableAddon, Plan::AttachedAddon, Plan::EventBasedAddon, Plan::Tier, PortalSession, PortalSession::LinkedCustomer, PromotionalCredit, Quote, Quote::BillingAddress, Quote::Discount, Quote::LineItem, Quote::LineItemDiscount, Quote::LineItemTax, Quote::ShippingAddress, Quote::Tax, QuoteLineGroup, QuoteLineGroup::Discount, QuoteLineGroup::LineItem, QuoteLineGroup::LineItemDiscount, QuoteLineGroup::LineItemTax, QuoteLineGroup::Tax, QuotedSubscription, QuotedSubscription::Addon, QuotedSubscription::Coupon, QuotedSubscription::EventBasedAddon, ResourceMigration, SiteMigrationDetail, Subscription, Subscription::Addon, Subscription::ChargedEventBasedAddon, Subscription::ContractTerm, Subscription::Coupon, Subscription::EventBasedAddon, Subscription::ReferralInfo, Subscription::ShippingAddress, SubscriptionEstimate, SubscriptionEstimate::ContractTerm, SubscriptionEstimate::ShippingAddress, ThirdPartyPaymentMethod, TimeMachine, Token, Transaction, Transaction::LinkedCreditNote, Transaction::LinkedInvoice, Transaction::LinkedPayment, Transaction::LinkedRefund, UnbilledCharge, UnbilledCharge::Tier, VirtualBankAccount

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(values, sub_types = {}, dependant_types = {}) ⇒ Model

Returns a new instance of Model.



6
7
8
9
10
# File 'lib/chargebee/models/model.rb', line 6

def initialize(values, sub_types={}, dependant_types={})
  @values = values
  @sub_types = sub_types
  @dependant_types = dependant_types
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args, &block) ⇒ Object



43
44
45
46
47
48
49
50
51
# File 'lib/chargebee/models/model.rb', line 43

def method_missing(m, *args, &block)
  if(@values.has_key?(m))
      return @values[m]
  elsif(m[0,3] == "cf_") # All the custom fields start with prefix cf_. 
      return nil
  end
  puts "There's no method called #{m} #{args} here -- please try again."
  puts @values
end

Class Method Details

.construct(values, sub_types = {}, dependant_types = {}) ⇒ Object



64
65
66
67
68
69
70
# File 'lib/chargebee/models/model.rb', line 64

def self.construct(values, sub_types = {}, dependant_types = {})
  if(values != nil)
    obj = self.new(values, sub_types, dependant_types)
    obj.load(values)
    obj
  end
end

.uri_path(*paths) ⇒ Object



53
54
55
56
57
58
59
60
61
62
# File 'lib/chargebee/models/model.rb', line 53

def self.uri_path(*paths) 
  url = ""
  for path in paths
      if(path.nil? || path.strip.length < 1) 
         raise "Id is empty or nil" 
      end
      url = "#{url}/#{URI.encode(path.strip)}"
  end
  return url
end

Instance Method Details

#init_dependant(obj, type, sub_types = {}) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/chargebee/models/model.rb', line 72

def init_dependant(obj, type, sub_types = {})
  instance_eval do
    if(obj[type] != nil)
      case obj
      when Hash
        if(@dependant_types[type] != nil)
          dependant_obj = @dependant_types[type].construct(obj[type], sub_types)
          instance_variable_set("@#{type}", dependant_obj)
        end
      end
    end
  end
end

#init_dependant_list(obj, type, sub_types = {}) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/chargebee/models/model.rb', line 86

def init_dependant_list(obj, type, sub_types = {})
  instance_eval do
    if(obj[type] != nil)
      case obj[type]
      when Array
        if(@dependant_types[type] != nil)
          set_val = obj[type].map {|dt| @dependant_types[type].construct(dt, sub_types)}
          instance_variable_set("@#{type}", set_val)
        end
      end
    end
  end
end

#inspectObject



16
17
18
# File 'lib/chargebee/models/model.rb', line 16

def inspect()
  "#<#{self.class}:0x#{self.object_id.to_s(16)} > JSON: " + JSON.pretty_generate(@values)
end

#load(values) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/chargebee/models/model.rb', line 20

def load(values)
  instance_eval do
    values.each do |k, v|
      set_val = nil
      next if v.is_a?(Hash) && @dependant_types[k] != nil

      set_val = if v.is_a?(Hash)
                  (@sub_types[k] != nil) ? @sub_types[k].construct(v) : v
                elsif v.is_a?(Array)
                  if @sub_types[k] != nil
                    v.map { |item| @sub_types[k].construct(item)}
                  else
                    v
                  end
                else
                  v
                end

      instance_variable_set("@#{k}", set_val)
    end
  end
end

#to_s(*args) ⇒ Object



12
13
14
# File 'lib/chargebee/models/model.rb', line 12

def to_s(*args) 
  JSON.pretty_generate(@values) 
end