Class: Partitional::Model
- Inherits:
-
Object
- Object
- Partitional::Model
- Includes:
- ActiveModel::Model
- Defined in:
- lib/partitional/model.rb
Instance Attribute Summary collapse
-
#mapping ⇒ Object
Returns the value of attribute mapping.
-
#record ⇒ Object
Returns the value of attribute record.
Class Method Summary collapse
- .attr_accessor(*attrs) ⇒ Object
- .attr_define(*attrs) ⇒ Object
- .attr_reader(*attrs) ⇒ Object
- .attr_writer(*attrs) ⇒ Object
- .attributes ⇒ Object
Instance Method Summary collapse
Instance Attribute Details
#mapping ⇒ Object
Returns the value of attribute mapping.
7 8 9 |
# File 'lib/partitional/model.rb', line 7 def mapping @mapping end |
#record ⇒ Object
Returns the value of attribute record.
7 8 9 |
# File 'lib/partitional/model.rb', line 7 def record @record end |
Class Method Details
.attr_accessor(*attrs) ⇒ Object
38 39 40 41 |
# File 'lib/partitional/model.rb', line 38 def self.attr_accessor(*attrs) attr_reader(*attrs) attr_writer(*attrs) end |
.attr_define(*attrs) ⇒ Object
13 14 15 16 |
# File 'lib/partitional/model.rb', line 13 def self.attr_define(*attrs) attributes.concat(attrs.map(&:to_sym)) attributes.uniq! end |
.attr_reader(*attrs) ⇒ Object
18 19 20 21 22 23 24 25 26 |
# File 'lib/partitional/model.rb', line 18 def self.attr_reader(*attrs) attr_define(*attrs) attrs.each do |attr| attr = attr.to_s.to_sym define_method(attr) do self[attr] end end end |
.attr_writer(*attrs) ⇒ Object
28 29 30 31 32 33 34 35 36 |
# File 'lib/partitional/model.rb', line 28 def self.attr_writer(*attrs) attr_define(*attrs) attrs.each do |attr| attr = attr.to_s.to_sym define_method(:"#{attr}=") do |val| self[attr] = val end end end |
.attributes ⇒ Object
9 10 11 |
# File 'lib/partitional/model.rb', line 9 def self.attributes @attributes ||= [] end |
Instance Method Details
#[](attr) ⇒ Object
43 44 45 46 47 |
# File 'lib/partitional/model.rb', line 43 def [](attr) attr = attr.to_s.to_sym reader = (mapping || {}).fetch(attr) { attr } record ? record_send(reader) : instance_variable_get(:"@#{attr}") end |
#[]=(attr, val) ⇒ Object
49 50 51 52 53 |
# File 'lib/partitional/model.rb', line 49 def []=(attr, val) attr = attr.to_s.to_sym writer = (mapping || {}).fetch(attr) { attr } record ? record_send(:"#{writer}=", val) : instance_variable_set(:"@#{attr}", val) end |
#as_json ⇒ Object
55 56 57 58 59 60 61 |
# File 'lib/partitional/model.rb', line 55 def as_json attrs = self.class.attributes.map do |key| [key, send(key)] end Hash[attrs] end |