Class: Breeze::ActiveYaml
- Inherits:
-
Object
- Object
- Breeze::ActiveYaml
- Extended by:
- ActiveModel::Naming
- Includes:
- ActiveModel::Conversion
- Defined in:
- app/models/breeze/active_yaml.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
data of the instance, hash.
Class Method Summary collapse
-
.all ⇒ Object
return all yaml data converted.
-
.append(data) ⇒ Object
create a new data point by adding the hash.
- .define_access(field) ⇒ Object
-
.define_association(assoc) ⇒ Object
aka belongs_to define a method by given name that used id value and name of association to get a model instance of the id.
- .delete(id) ⇒ Object
- .dir_id(id, inc) ⇒ Object
-
.fields(*fields) ⇒ Object
class defines fields, we define access methods and finder for each.
- .find(id) ⇒ Object
- .find_all(field, value) ⇒ Object
- .find_by(field, value) ⇒ Object
- .first ⇒ Object
-
.full_path ⇒ Object
filename for the data (load and save) made up of the set dir (set_root_path) plus tableized class name.
- .largest ⇒ Object
-
.load_file ⇒ Object
load the yaml data from path and class_name.
- .next_id(id) ⇒ Object
- .previous_id(id) ⇒ Object
- .primary_key ⇒ Object
- .reload ⇒ Object
- .save_all ⇒ Object
-
.set_root_path(path) ⇒ Object
loads the data from path, name implicit in class_name.
- .the_meta_class ⇒ Object
Instance Method Summary collapse
- #id ⇒ Object
- #id=(new_id) ⇒ Object
-
#initialize(data) ⇒ ActiveYaml
constructor
retain the given hash data instance.
- #persisted? ⇒ Boolean
Constructor Details
#initialize(data) ⇒ ActiveYaml
retain the given hash data instance
6 7 8 9 |
# File 'app/models/breeze/active_yaml.rb', line 6 def initialize(data) @data = data # some checks, eg values are only fields end |
Instance Attribute Details
#data ⇒ Object (readonly)
data of the instance, hash
3 4 5 |
# File 'app/models/breeze/active_yaml.rb', line 3 def data @data end |
Class Method Details
.all ⇒ Object
return all yaml data converted
32 33 34 35 |
# File 'app/models/breeze/active_yaml.rb', line 32 def all self.reload if self.yaml_file.blank? yaml_file.collect{|item| self.new(item) } end |
.append(data) ⇒ Object
create a new data point by adding the hash
141 142 143 144 145 146 147 |
# File 'app/models/breeze/active_yaml.rb', line 141 def append(data) largest = 0 yaml_file.each { |item| largest = item[:id] if item[:id] > largest } data[:id] = largest + 1 yaml_file << data largest + 1 end |
.define_access(field) ⇒ Object
87 88 89 90 91 92 93 94 95 96 |
# File 'app/models/breeze/active_yaml.rb', line 87 def define_access(field) define_method(field) do data[field] end define_method(:"#{field}=") do |new_val| data[field] = new_val end assoc , id = field.to_s.split("_") # ie field image_id define_association(assoc) if id == "id" #creates method image end |
.define_association(assoc) ⇒ Object
aka belongs_to define a method by given name that used id value and name of association to get a model instance of the id
101 102 103 104 105 106 107 |
# File 'app/models/breeze/active_yaml.rb', line 101 def define_association(assoc) clazz = "Breeze::#{assoc.to_s.capitalize}".constantize define_method(assoc) do id = data[:"#{assoc}_id"] clazz.find( id ) end end |
.delete(id) ⇒ Object
153 154 155 156 |
# File 'app/models/breeze/active_yaml.rb', line 153 def delete(id) yaml_file.delete_if{|record| record[:id] == id.to_i} true end |
.dir_id(id, inc) ⇒ Object
67 68 69 70 71 72 73 74 |
# File 'app/models/breeze/active_yaml.rb', line 67 def dir_id(id , inc) while((id > 0) && (id < 300)) do id += inc if(is = find_by(:id , id)) return is end end end |
.fields(*fields) ⇒ Object
class defines fields, we define access methods and finder for each
110 111 112 113 114 115 116 117 118 119 |
# File 'app/models/breeze/active_yaml.rb', line 110 def fields(*fields) self.field_names = fields raise "Fields must be Array not #{fields.class}" unless fields.is_a?(Array) fields.each do |field| define_access(field) .define_method(:"find_by_#{field}") do |arg| find_by(field , arg) end end end |
.find(id) ⇒ Object
54 55 56 57 |
# File 'app/models/breeze/active_yaml.rb', line 54 def find( id ) id = id.to_i unless id.is_a?(Integer) find_by( :id , id ) end |
.find_all(field, value) ⇒ Object
82 83 84 85 |
# File 'app/models/breeze/active_yaml.rb', line 82 def find_all( field , value ) found = yaml_file.find_all { |record| record[field] == value } found.collect {|f| self.new(f)} end |
.find_by(field, value) ⇒ Object
76 77 78 79 80 |
# File 'app/models/breeze/active_yaml.rb', line 76 def find_by( field , value ) found = yaml_file.detect { |record| record[field] == value } return nil unless found self.new(found) end |
.first ⇒ Object
50 51 52 |
# File 'app/models/breeze/active_yaml.rb', line 50 def first self.new(yaml_file.first) end |
.full_path ⇒ Object
filename for the data (load and save) made up of the set dir (set_root_path) plus tableized class name
128 129 130 131 132 |
# File 'app/models/breeze/active_yaml.rb', line 128 def full_path mod , filename = self.name.split("::").collect{|p| p.underscore} full_name = mod + "/" + filename.tableize + ".yml" File.join(self.path , full_name ) end |
.largest ⇒ Object
134 135 136 137 138 |
# File 'app/models/breeze/active_yaml.rb', line 134 def largest largest = 0 yaml_file.each { |item| largest = item[:id] if item[:id] > largest } largest end |
.load_file ⇒ Object
load the yaml data from path and class_name
122 123 124 |
# File 'app/models/breeze/active_yaml.rb', line 122 def load_file YAML.load(File.read(full_path) , permitted_classes: [Time , Symbol],aliases: true) end |
.next_id(id) ⇒ Object
59 60 61 |
# File 'app/models/breeze/active_yaml.rb', line 59 def next_id(id) dir_id(id , 1) end |
.previous_id(id) ⇒ Object
63 64 65 |
# File 'app/models/breeze/active_yaml.rb', line 63 def previous_id(id) dir_id(id , -1) end |
.primary_key ⇒ Object
37 38 39 |
# File 'app/models/breeze/active_yaml.rb', line 37 def primary_key "id" end |
.reload ⇒ Object
46 47 48 |
# File 'app/models/breeze/active_yaml.rb', line 46 def reload self.yaml_file = load_file end |
.save_all ⇒ Object
149 150 151 |
# File 'app/models/breeze/active_yaml.rb', line 149 def save_all File.write( full_path , yaml_file.to_yaml) end |
.set_root_path(path) ⇒ Object
loads the data from path, name implicit in class_name
42 43 44 |
# File 'app/models/breeze/active_yaml.rb', line 42 def set_root_path(path) self.path = path end |
.the_meta_class ⇒ Object
158 159 160 161 162 |
# File 'app/models/breeze/active_yaml.rb', line 158 def class << self self end end |
Instance Method Details
#id ⇒ Object
11 12 13 |
# File 'app/models/breeze/active_yaml.rb', line 11 def id data[:id] end |
#id=(new_id) ⇒ Object
14 15 16 17 |
# File 'app/models/breeze/active_yaml.rb', line 14 def id=(new_id) raise "Id must be number not (#{new_id})" unless new_id.is_a?(Integer) data[:id] = new_id end |
#persisted? ⇒ Boolean
22 23 24 |
# File 'app/models/breeze/active_yaml.rb', line 22 def persisted? true end |