Class: Breeze::ActiveYaml

Inherits:
Object
  • Object
show all
Extended by:
ActiveModel::Naming
Includes:
ActiveModel::Conversion
Defined in:
app/models/breeze/active_yaml.rb

Direct Known Subclasses

ActiveBase, SharedBase

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#dataObject (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

.allObject

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



118
119
120
121
122
123
124
# File 'app/models/breeze/active_yaml.rb', line 118

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



70
71
72
73
74
75
76
77
78
79
# File 'app/models/breeze/active_yaml.rb', line 70

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



84
85
86
87
88
89
90
# File 'app/models/breeze/active_yaml.rb', line 84

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



130
131
132
133
# File 'app/models/breeze/active_yaml.rb', line 130

def delete(id)
  yaml_file.delete_if{|record| record[:id] == id.to_i}
  true
end

.fields(*fields) ⇒ Object

class defines fields, we define access methods and finder for each



93
94
95
96
97
98
99
100
101
102
# File 'app/models/breeze/active_yaml.rb', line 93

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)
    the_meta_class.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



65
66
67
68
# File 'app/models/breeze/active_yaml.rb', line 65

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



59
60
61
62
63
# File 'app/models/breeze/active_yaml.rb', line 59

def find_by( field , value )
  found = yaml_file.detect { |record| record[field] == value }
  return nil unless found
  self.new(found)
end

.firstObject



50
51
52
# File 'app/models/breeze/active_yaml.rb', line 50

def first
  self.new(yaml_file.first)
end

.full_pathObject

filename for the data (load and save) made up of the set dir (set_root_path) plus tableized class name



111
112
113
114
115
# File 'app/models/breeze/active_yaml.rb', line 111

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

.load_fileObject

load the yaml data from path and class_name



105
106
107
# File 'app/models/breeze/active_yaml.rb', line 105

def load_file
  YAML.load(File.read(full_path) , permitted_classes: [Time , Symbol],aliases: true)
end

.primary_keyObject



37
38
39
# File 'app/models/breeze/active_yaml.rb', line 37

def primary_key
  "id"
end

.reloadObject



46
47
48
# File 'app/models/breeze/active_yaml.rb', line 46

def reload
  self.yaml_file = load_file
end

.save_allObject



126
127
128
# File 'app/models/breeze/active_yaml.rb', line 126

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_classObject



135
136
137
138
139
# File 'app/models/breeze/active_yaml.rb', line 135

def the_meta_class
  class << self
    self
  end
end

Instance Method Details

#idObject



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

Returns:

  • (Boolean)


22
23
24
# File 'app/models/breeze/active_yaml.rb', line 22

def persisted?
  true
end