Class: DataFiles::ActiveData

Inherits:
Object
  • Object
show all
Defined in:
lib/data_files/active_data.rb

Overview

Base class for data querying and manipulation.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs = {}) ⇒ ActiveData

Returns a new instance of ActiveData.



77
78
79
80
# File 'lib/data_files/active_data.rb', line 77

def initialize(attrs = {})
  @_id = nil
  attrs.each { |key, value| send("#{key}=", value) }
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



6
7
8
# File 'lib/data_files/active_data.rb', line 6

def errors
  @errors
end

Class Method Details

.allObject



8
9
10
11
12
# File 'lib/data_files/active_data.rb', line 8

def self.all
  data.collect do |item|
    new(item)
  end
end

.attributesObject



58
59
60
# File 'lib/data_files/active_data.rb', line 58

def self.attributes
  class_variable_get(:@@attributes)
end

.dataObject



50
51
52
# File 'lib/data_files/active_data.rb', line 50

def self.data
  class_variable_get(:@@data)
end

.data=(value) ⇒ Object



54
55
56
# File 'lib/data_files/active_data.rb', line 54

def self.data=(value)
  class_variable_set(:@@data, value)
end

.find_by(conditions) ⇒ Object



32
33
34
35
36
37
38
39
# File 'lib/data_files/active_data.rb', line 32

def self.find_by(conditions)
  results = where(conditions)
  if results.size.positive?
    results.first
  else
    nil
  end
end

.firstObject



14
15
16
# File 'lib/data_files/active_data.rb', line 14

def self.first
  new(data.first)
end

.lastObject



18
19
20
# File 'lib/data_files/active_data.rb', line 18

def self.last
  new(data.last)
end

.sort_by_primary_keyObject



66
67
68
69
70
71
72
73
74
75
# File 'lib/data_files/active_data.rb', line 66

def self.sort_by_primary_key
  self.data = data.sort_by do |item|
    primary_key_value = item.values.first
    if primary_key_value.is_a? String
      primary_key_value.downcase
    else
      primary_key_value
    end
  end
end

.typesObject



62
63
64
# File 'lib/data_files/active_data.rb', line 62

def self.types
  class_variable_get(:@@types)
end

.where(conditions) ⇒ Object



22
23
24
25
26
27
28
29
30
# File 'lib/data_files/active_data.rb', line 22

def self.where(conditions)
  all.select do |item|
    selected = true
    conditions.each do |key, value|
      selected = false if item.send(key) != value
    end
    selected
  end
end

.write_yamlObject



41
42
43
44
45
46
47
48
# File 'lib/data_files/active_data.rb', line 41

def self.write_yaml
  sort_by_primary_key
  item_attributes = all.collect(&:attributes)

  File.open("data/#{name.downcase}s.yml", 'w') do |file|
    file.write(item_attributes.to_yaml)
  end
end

Instance Method Details

#attributesObject



82
83
84
85
86
87
88
# File 'lib/data_files/active_data.rb', line 82

def attributes
  attributes_hash = {}
  self.class.attributes.each do |attr|
    attributes_hash[attr] = send(attr) unless attr == '_id'
  end
  attributes_hash
end

#inspectObject



100
101
102
# File 'lib/data_files/active_data.rb', line 100

def inspect
  to_s
end

#saveObject



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/data_files/active_data.rb', line 129

def save
  return false unless valid?

  strip

  self.class.data = self.class.data.map do |item|
    if item['_id'] == @_id
      attributes.merge('_id' => @_id)
    else
      item
    end
  end

  if @_id.nil?
    @_id = next_id
    self.class.data << attributes.merge('_id' => @_id)
  end

  self.class.write_yaml
  true
end

#stripObject



151
152
153
154
155
156
# File 'lib/data_files/active_data.rb', line 151

def strip
  self.class.attributes.each do |attr|
    send("#{attr}=", send(attr).strip) if send(attr).is_a? String
  end
  self
end

#to_sObject



90
91
92
93
94
95
96
97
98
# File 'lib/data_files/active_data.rb', line 90

def to_s
  joined_attributes = self.class.attributes.collect do |attr|
    val = send(attr)
    val = "\"#{val}\"" if val.is_a? String
    "#{attr}: #{val.nil? ? 'nil' : val}"
  end.join(', ')

  "#<#{self.class} #{joined_attributes}>"
end

#valid?Boolean

Returns:

  • (Boolean)


104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/data_files/active_data.rb', line 104

def valid?
  @errors = []

  primary_key = self.class.attributes.first
  primary_key_values = self.class.data.collect do |item|
    { item['_id'] => item[primary_key] }
  end

  primary_key_values.each do |item|
    item.each do |key, value|
      if key != @_id && value == send(primary_key)
        @errors << "#{self.class.name} with #{primary_key} #{send(primary_key)} already exists"
      end
    end
  end

  attributes.each do |key, value|
    unless self.class.types[key].include?(value.class.name)
      @errors << type_validation_error_message(key, self.class.types[key])
    end
  end

  @errors.size.zero?
end