Class: Tempo::Model::Base
- Inherits:
-
Object
- Object
- Tempo::Model::Base
- Defined in:
- lib/tempo/models/base.rb
Instance Attribute Summary collapse
-
#id ⇒ Object
readonly
Returns the value of attribute id.
Class Method Summary collapse
- .delete(instance) ⇒ Object
-
.file ⇒ Object
example: Tempo::Model::Animal -> tempo_animals.yaml.
-
.find(key, value) ⇒ Object
example: Tempo::Model.find(“id”, 1).
-
.find_by_id(id) ⇒ Object
find by id should be exact, so we remove the array wrapper.
-
.id_counter ⇒ Object
Maintain an array of unique ids for the class.
- .ids ⇒ Object
- .index ⇒ Object
- .method_missing(meth, *args, &block) ⇒ Object
-
.read_from_file(options = {}) ⇒ Object
pass custom directory through in options.
- .run_find_by_method(attrs, *args, &block) ⇒ Object
- .run_sort_by_method(attribute, args = @index.clone, &block) ⇒ Object
-
.save_to_file(options = {}) ⇒ Object
pass custom directory through in options.
Instance Method Summary collapse
- #delete ⇒ Object
-
#freeze_dry ⇒ Object
record the state of all instance variables as a hash.
-
#initialize(options = {}) ⇒ Base
constructor
class methods.
Constructor Details
#initialize(options = {}) ⇒ Base
class methods
127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/tempo/models/base.rb', line 127 def initialize(={}) id_candidate = [:id] if !id_candidate @id = self.class.next_id elsif self.class.ids.include? id_candidate raise IdentityConflictError, "Id #{id_candidate} already exists" else @id = id_candidate end self.class.add_id @id self.class.add_to_index self end |
Instance Attribute Details
#id ⇒ Object (readonly)
Returns the value of attribute id.
17 18 19 |
# File 'lib/tempo/models/base.rb', line 17 def id @id end |
Class Method Details
.delete(instance) ⇒ Object
120 121 122 123 124 |
# File 'lib/tempo/models/base.rb', line 120 def delete(instance) id = instance.id index.delete( instance ) ids.delete( id ) end |
.file ⇒ Object
example: Tempo::Model::Animal -> tempo_animals.yaml
40 41 42 |
# File 'lib/tempo/models/base.rb', line 40 def file FileRecord::FileUtility.new(self).filename end |
.find(key, value) ⇒ Object
example: Tempo::Model.find(“id”, 1)
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/tempo/models/base.rb', line 100 def find(key, value) key = "@#{key}".to_sym matches = [] index.each do |i| stored_value = i.instance_variable_get( key ) if stored_value.kind_of? String if value.kind_of? Regexp matches << i if value.match stored_value else matches << i if stored_value.downcase.include? value.to_s.downcase end elsif stored_value.kind_of? Integer matches << i if stored_value == value.to_i end end matches end |
.find_by_id(id) ⇒ Object
find by id should be exact, so we remove the array wrapper
93 94 95 96 |
# File 'lib/tempo/models/base.rb', line 93 def find_by_id(id) matches = find "id", id match = matches[0] end |
.id_counter ⇒ Object
Maintain an array of unique ids for the class. Initialize new members with the next numberical id Ids can be assigned on init (for the purpose of reading in records of previous instances). An error will be raised if there is already an instance with that id.
27 28 29 |
# File 'lib/tempo/models/base.rb', line 27 def id_counter @id_counter ||= 1 end |
.ids ⇒ Object
31 32 33 |
# File 'lib/tempo/models/base.rb', line 31 def ids @ids ||= [] end |
.index ⇒ Object
35 36 37 |
# File 'lib/tempo/models/base.rb', line 35 def index @index ||= [] end |
.method_missing(meth, *args, &block) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/tempo/models/base.rb', line 54 def method_missing(meth, *args, &block) if meth.to_s =~ /^find_by_(.+)$/ run_find_by_method($1, *args, &block) elsif meth.to_s =~ /^sort_by_(.+)$/ run_sort_by_method($1, *args, &block) else super end end |
.read_from_file(options = {}) ⇒ Object
pass custom directory through in options
50 51 52 |
# File 'lib/tempo/models/base.rb', line 50 def read_from_file(={}) FileRecord::Record.read_model self, end |
.run_find_by_method(attrs, *args, &block) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/tempo/models/base.rb', line 73 def run_find_by_method(attrs, *args, &block) # Make an array of attribute names attrs = attrs.split('_and_') attrs_with_args = [attrs, args].transpose filtered = index.clone attrs_with_args.each do | kv | matches = find kv[0], kv[1] return matches if matches.empty? matches.each do |match| matches.delete match unless filtered.include? match filtered = matches end end filtered end |
.run_sort_by_method(attribute, args = @index.clone, &block) ⇒ Object
66 67 68 69 70 71 |
# File 'lib/tempo/models/base.rb', line 66 def run_sort_by_method(attribute, args=@index.clone, &block) attr = "@#{attribute}".to_sym args.sort! { |a,b| a.instance_variable_get( attr ) <=> b.instance_variable_get( attr ) } return args unless block block.call args end |
.save_to_file(options = {}) ⇒ Object
pass custom directory through in options
45 46 47 |
# File 'lib/tempo/models/base.rb', line 45 def save_to_file(={}) FileRecord::Record.save_model self, end |
Instance Method Details
#delete ⇒ Object
152 153 154 |
# File 'lib/tempo/models/base.rb', line 152 def delete self.class.delete self end |
#freeze_dry ⇒ Object
record the state of all instance variables as a hash
141 142 143 144 145 146 147 148 149 150 |
# File 'lib/tempo/models/base.rb', line 141 def freeze_dry record = {} state = instance_variables state.each do |attr| key = attr[1..-1].to_sym val = instance_variable_get attr record[key] = val end record end |