Class: StaticModel::Base

Inherits:
Object
  • Object
show all
Includes:
ActiveRecord, Associations, Comparable
Defined in:
lib/static_model/base.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Comparable

#<=>

Methods included from ActiveRecord

#destroyed?, #new_record?

Methods included from Associations

included

Constructor Details

#initialize(attribute_hash = {}, force_load = true) ⇒ Base

Returns a new instance of Base.



17
18
19
20
21
22
23
# File 'lib/static_model/base.rb', line 17

def initialize(attribute_hash = {}, force_load = true)
  self.class.load if force_load
  raise(StaticModel::BadOptions, "Initializing a model is done with a Hash {} given #{attribute_hash.inspect}") unless attribute_hash.is_a?(Hash)
  @id = attribute_hash.delete('id') || attribute_hash.delete(:id) || self.class.next_id
  self.attributes = attribute_hash
  self.class.last_id = @id
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args) ⇒ Object (private)



233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'lib/static_model/base.rb', line 233

def method_missing(meth, *args)
  attribute_name = meth.to_s.gsub(/=$/,'')
  if has_attribute?(attribute_name)
    if meth.to_s =~ /=$/
      # set
      return set_attribute(attribute_name, args[0])
    else
      # get
      return get_attribute(attribute_name)
    end
  end
  super
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



15
16
17
# File 'lib/static_model/base.rb', line 15

def id
  @id
end

Class Method Details

.[](id) ⇒ Object



91
92
93
# File 'lib/static_model/base.rb', line 91

def [](id)
  find_by_id(id)
end

.attribute(name, options = {}) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/static_model/base.rb', line 29

def self.attribute(name, options = {})
  options = {
    :default => nil,
    :freeze => false
  }.merge(options)
  @defined_attributes ||= {}
  @defined_attributes[name.to_s] = options

  module_eval <<-EOT
    def #{name}
      @attributes['#{name}'] || #{options[:default].inspect}
    end

    def #{name}=(value)
      if !#{options[:freeze].inspect}
        @attributes['#{name}'] = value
      end
    end

    def #{name}?
      !!#{name}
    end
  EOT
end

.class_attribute(name) ⇒ Object



174
175
176
# File 'lib/static_model/base.rb', line 174

def class_attribute(name)
  class_attributes[name]
end

.class_attributesObject



169
170
171
172
# File 'lib/static_model/base.rb', line 169

def class_attributes
  load
  @class_attributes ||= {}
end

.countObject



165
166
167
# File 'lib/static_model/base.rb', line 165

def count
  records.length
end

.data_fileObject



153
154
155
# File 'lib/static_model/base.rb', line 153

def data_file
  @data_file ||= default_data_file_path
end

.defined_attributesObject



54
55
56
# File 'lib/static_model/base.rb', line 54

def self.defined_attributes
  @defined_attributes || {}
end

.find(what, *args, &block) ⇒ Object



76
77
78
79
80
81
82
83
# File 'lib/static_model/base.rb', line 76

def find(what, *args, &block)
  case what
  when Symbol
    send("find_#{what}")
  when Integer
    find_by_id(what)
  end
end

.find_allObject Also known as: all



95
96
97
# File 'lib/static_model/base.rb', line 95

def find_all
  records
end

.find_all_by(attribute, value) ⇒ Object



110
111
112
# File 'lib/static_model/base.rb', line 110

def find_all_by(attribute, value)
  records.find_all {|r| r.has_attribute?(attribute) ? (r.send(attribute) == value) : false }
end

.find_by_id(id) ⇒ Object



85
86
87
88
89
# File 'lib/static_model/base.rb', line 85

def find_by_id(id)
  record = records.detect {|r| r.id == id }
  raise(StaticModel::RecordNotFound, "Could not find record with id = #{id}") unless record
  record
end

.find_firstObject Also known as: first



100
101
102
# File 'lib/static_model/base.rb', line 100

def find_first
  records[0]
end

.find_first_by(attribute, value) ⇒ Object Also known as: find_by



114
115
116
# File 'lib/static_model/base.rb', line 114

def find_first_by(attribute, value)
  records.find {|r| r.has_attribute?(attribute) ? (r.send(attribute) == value) : false }
end

.find_lastObject Also known as: last



105
106
107
# File 'lib/static_model/base.rb', line 105

def find_last
  records[records.length-1]
end

.find_last_by(attribute, value) ⇒ Object



119
120
121
# File 'lib/static_model/base.rb', line 119

def find_last_by(attribute, value)
  records.reverse.find {|r| r.has_attribute?(attribute) ? (r.send(attribute) == value) : false }
end

.last_idObject



187
188
189
# File 'lib/static_model/base.rb', line 187

def last_id
  @last_id ||= 0
end

.last_id=(new_last_id) ⇒ Object



191
192
193
# File 'lib/static_model/base.rb', line 191

def last_id=(new_last_id)
  @last_id = new_last_id if new_last_id > self.last_id
end

.load(reload = false) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/static_model/base.rb', line 123

def load(reload = false)
  return if loaded? && !reload
  begin
    raw_data = File.open(data_file) {|f| f.read }
    parsed_data = ERB.new(raw_data).result
    data = YAML::load(parsed_data)
  rescue
    raise(StaticModel::BadDataFile, "The data file you specified '#{data_file}' was not in a readable format.")
  end
  records = []
  if data.is_a?(Hash) && data.has_key?('records')
    records = data.delete('records')
    @class_attributes = data
    @class_attributes.make_indifferent! if @class_attributes.respond_to?(:make_indifferent!)
  elsif data.is_a?(Array)
    records = data
  end
  @last_id = 0
  @records = records && !records.empty? ? records.dup.collect {|r| new(r, false) } : []
  @loaded = true
end

.load_pathObject



7
8
9
# File 'lib/static_model/base.rb', line 7

def self.load_path
  @@load_path ||= File.join('config', 'data')
end

.load_path=(path) ⇒ Object



11
12
13
# File 'lib/static_model/base.rb', line 11

def self.load_path=(path)
  @@load_path = path
end

.loaded?Boolean

Returns:

  • (Boolean)


149
150
151
# File 'lib/static_model/base.rb', line 149

def loaded?
  @loaded ||= false
end

.next_idObject



183
184
185
# File 'lib/static_model/base.rb', line 183

def next_id
  last_id + 1
end

.recordsObject



178
179
180
181
# File 'lib/static_model/base.rb', line 178

def records
  load
  @records
end

.reload!Object



145
146
147
# File 'lib/static_model/base.rb', line 145

def reload!
  load(true)
end

.set_data_file(file_path) ⇒ Object



157
158
159
160
161
162
163
# File 'lib/static_model/base.rb', line 157

def set_data_file(file_path)
  raise(StaticModel::DataFileNotFound, "Could not find data file #{file_path}") unless File.readable?(file_path)
  @data_file = file_path
  # force reload
  @loaded = false
  @records = nil
end

Instance Method Details

#attribute_namesObject



66
67
68
# File 'lib/static_model/base.rb', line 66

def attribute_names
  (attributes.keys | self.class.class_attributes.keys | self.class.defined_attributes.keys).collect {|k| k.to_s }
end

#attributesObject



58
59
60
# File 'lib/static_model/base.rb', line 58

def attributes
  @attributes ||= {}
end

#attributes=(attribute_hash) ⇒ Object



62
63
64
# File 'lib/static_model/base.rb', line 62

def attributes=(attribute_hash)
  attribute_hash.each {|k,v| set_attribute(k,v) }
end

#has_attribute?(name) ⇒ Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/static_model/base.rb', line 70

def has_attribute?(name)
  name.to_s == 'id' || attribute_names.include?(name.to_s)
end

#to_sObject



25
26
27
# File 'lib/static_model/base.rb', line 25

def to_s
  "<#{self.class} #{@attributes.inspect}>"
end