Class: IB::Base
- Extended by:
- ActiveModel::Callbacks, ActiveModel::Naming
- Includes:
- ActiveModel::Serialization, ActiveModel::Serializers::JSON, ActiveModel::Validations
- Defined in:
- lib/ib/base.rb
Overview
Base class for tableless IB data Models, extends ActiveModel API
Class Method Summary collapse
- .attr_accessible(*args) ⇒ Object
-
.attr_protected(*args) ⇒ Object
Noop methods mocking ActiveRecord::Base macros.
-
.belongs_to(model, *args) ⇒ Object
ActiveRecord::Base association API mocks.
- .find(*args) ⇒ Object
- .has_many(models, *args) ⇒ Object
- .has_one(model, *args) ⇒ Object
-
.serialize(*properties) ⇒ Object
ActiveRecord::Base misc.
Instance Method Summary collapse
-
#[](key) ⇒ Object
ActiveModel-style read/write_attribute accessors.
- #[]=(key, val) ⇒ Object
-
#attributes ⇒ Object
ActiveModel API (for serialization).
- #attributes=(attrs) ⇒ Object
-
#initialize(attributes = {}, opts = {}) ⇒ Base
constructor
If a opts hash is given, keys are taken as attribute names, values as data.
- #new_record? ⇒ Boolean
- #save ⇒ Object (also: #save!)
- #to_model ⇒ Object
- #update_attribute(key, value) ⇒ Object
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
67 68 |
# File 'lib/ib/base.rb', line 67 def self.attr_accessible *args end |
.attr_protected(*args) ⇒ Object
Noop methods mocking ActiveRecord::Base macros
64 65 |
# File 'lib/ib/base.rb', line 64 def self.attr_protected *args end |
.belongs_to(model, *args) ⇒ Object
ActiveRecord::Base association API mocks
72 73 74 |
# File 'lib/ib/base.rb', line 72 def self.belongs_to model, *args attr_accessor model end |
.find(*args) ⇒ Object
89 90 91 |
# File 'lib/ib/base.rb', line 89 def self.find *args [] end |
.has_many(models, *args) ⇒ Object
80 81 82 83 84 85 86 87 |
# File 'lib/ib/base.rb', line 80 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
76 77 78 |
# File 'lib/ib/base.rb', line 76 def self.has_one model, *args attr_accessor model end |
.serialize(*properties) ⇒ Object
ActiveRecord::Base misc
99 100 |
# File 'lib/ib/base.rb', line 99 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
43 44 45 46 |
# File 'lib/ib/base.rb', line 43 def []= key, val # p key, val attributes[key.to_sym] = val end |
#attributes ⇒ Object
ActiveModel API (for serialization)
26 27 28 |
# File 'lib/ib/base.rb', line 26 def attributes @attributes ||= Hash.new #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
52 53 54 |
# File 'lib/ib/base.rb', line 52 def new_record? true end |
#save ⇒ Object Also known as: save!
56 57 58 |
# File 'lib/ib/base.rb', line 56 def save valid? end |
#to_model ⇒ Object
48 49 50 |
# File 'lib/ib/base.rb', line 48 def to_model self end |
#update_attribute(key, value) ⇒ Object
39 40 41 |
# File 'lib/ib/base.rb', line 39 def update_attribute key, value @attributes[key.to_sym] = value end |