Class: IB::Base

Inherits:
Object show all
Extended by:
ActiveModel::Callbacks, ActiveModel::Naming
Includes:
ActiveModel::Serialization, ActiveModel::Serializers::JSON, ActiveModel::Serializers::Xml, ActiveModel::Validations
Defined in:
lib/ib/base.rb

Overview

Base class for tableless IB data Models, extends ActiveModel API

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}, opts = {}) ⇒ Base

If a opts hash is given, keys are taken as attribute names, values as data. The model instance fields are then set automatically from the opts Hash.



16
17
18
19
20
21
22
# File 'lib/ib/base.rb', line 16

def initialize attributes={}, opts={}
  run_callbacks :initialize do
    error "Argument must be a Hash", :args unless attributes.is_a?(Hash)

    self.attributes = attributes # set_attribute_defaults is now after_init callback
  end
end

Class Method Details

.attr_accessible(*args) ⇒ Object



63
64
# File 'lib/ib/base.rb', line 63

def self.attr_accessible *args
end

.attr_protected(*args) ⇒ Object

Noop methods mocking ActiveRecord::Base macros



60
61
# File 'lib/ib/base.rb', line 60

def self.attr_protected *args
end

.belongs_to(model, *args) ⇒ Object

ActiveRecord::Base association API mocks



68
69
70
# File 'lib/ib/base.rb', line 68

def self.belongs_to model, *args
  attr_accessor model
end

.find(*args) ⇒ Object



85
86
87
# File 'lib/ib/base.rb', line 85

def self.find *args
  []
end

.has_many(models, *args) ⇒ Object



76
77
78
79
80
81
82
83
# File 'lib/ib/base.rb', line 76

def self.has_many models, *args
  attr_accessor models

  define_method(models) do
    self.instance_variable_get("@#{models}") ||
      self.instance_variable_set("@#{models}", [])
  end
end

.has_one(model, *args) ⇒ Object



72
73
74
# File 'lib/ib/base.rb', line 72

def self.has_one model, *args
  attr_accessor model
end

.serialize(*properties) ⇒ Object

ActiveRecord::Base misc



95
96
# File 'lib/ib/base.rb', line 95

def self.serialize *properties
end

Instance Method Details

#[](key) ⇒ Object

ActiveModel-style read/write_attribute accessors



35
36
37
# File 'lib/ib/base.rb', line 35

def [] key
  attributes[key.to_sym]
end

#[]=(key, val) ⇒ Object



39
40
41
42
# File 'lib/ib/base.rb', line 39

def []= key, val
  # p key, val
  attributes[key.to_sym] = val
end

#attributesObject

ActiveModel API (for serialization)



26
27
28
# File 'lib/ib/base.rb', line 26

def attributes
  @attributes ||= HashWithIndifferentAccess.new
end

#attributes=(attrs) ⇒ Object



30
31
32
# File 'lib/ib/base.rb', line 30

def attributes= attrs
  attrs.keys.each { |key| self.send("#{key}=", attrs[key]) }
end

#new_record?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/ib/base.rb', line 48

def new_record?
  true
end

#saveObject Also known as: save!



52
53
54
# File 'lib/ib/base.rb', line 52

def save
  valid?
end

#to_modelObject



44
45
46
# File 'lib/ib/base.rb', line 44

def to_model
  self
end