Class: IB::Base
- 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
- .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
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.
14 15 16 17 18 19 20 |
# File 'lib/ib/base.rb', line 14 def initialize attributes={}, opts={} run_callbacks :initialize do error "Argument must be a Hash", :args unless attributes.is_a?(Hash) self.attributes = default_attributes.merge(attributes) end end |
Class Method Details
.attr_accessible(*args) ⇒ Object
61 62 |
# File 'lib/ib/base.rb', line 61 def self.attr_accessible *args end |
.attr_protected(*args) ⇒ Object
Noop methods mocking ActiveRecord::Base macros
58 59 |
# File 'lib/ib/base.rb', line 58 def self.attr_protected *args end |
.belongs_to(model, *args) ⇒ Object
ActiveRecord::Base association API mocks
66 67 68 |
# File 'lib/ib/base.rb', line 66 def self.belongs_to model, *args attr_accessor model end |
.find(*args) ⇒ Object
83 84 85 |
# File 'lib/ib/base.rb', line 83 def self.find *args [] end |
.has_many(models, *args) ⇒ Object
74 75 76 77 78 79 80 81 |
# File 'lib/ib/base.rb', line 74 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
70 71 72 |
# File 'lib/ib/base.rb', line 70 def self.has_one model, *args attr_accessor model end |
.serialize(*properties) ⇒ Object
ActiveRecord::Base misc
93 94 |
# File 'lib/ib/base.rb', line 93 def self.serialize *properties end |
Instance Method Details
#[](key) ⇒ Object
ActiveModel-style read/write_attribute accessors
33 34 35 |
# File 'lib/ib/base.rb', line 33 def [] key attributes[key.to_sym] end |
#[]=(key, val) ⇒ Object
37 38 39 40 |
# File 'lib/ib/base.rb', line 37 def []= key, val # p key, val attributes[key.to_sym] = val end |
#attributes ⇒ Object
ActiveModel API (for serialization)
24 25 26 |
# File 'lib/ib/base.rb', line 24 def attributes @attributes ||= HashWithIndifferentAccess.new end |
#attributes=(attrs) ⇒ Object
28 29 30 |
# File 'lib/ib/base.rb', line 28 def attributes= attrs attrs.keys.each { |key| self.send("#{key}=", attrs[key]) } end |
#new_record? ⇒ Boolean
46 47 48 |
# File 'lib/ib/base.rb', line 46 def new_record? true end |
#save ⇒ Object Also known as: save!
50 51 52 |
# File 'lib/ib/base.rb', line 50 def save valid? end |
#to_model ⇒ Object
42 43 44 |
# File 'lib/ib/base.rb', line 42 def to_model self end |