Class: Jaysus::Base

Inherits:
Object
  • Object
show all
Extended by:
ActiveModel::Naming
Includes:
ActiveModel::Serialization, ActiveModel::Serializers::JSON
Defined in:
lib/jaysus/base.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(set_attrs = {}) ⇒ Base

Returns a new instance of Base.



125
126
127
# File 'lib/jaysus/base.rb', line 125

def initialize(set_attrs = {})
  set_attributes(set_attrs)
end

Class Method Details

.attribute(name) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/jaysus/base.rb', line 8

def self.attribute(name)
  self.attributes << name
  class_eval do
    define_method(name) do               # def title
      instance_variable_get("@#{name}")  #   @title 
    end                                  # end
    
    define_method("#{name}=") do |value|         # def title=(value)
      instance_variable_set("@#{name}", value)   #   @title = value
    end                                          # end
  end
end

.attributesObject



21
22
23
# File 'lib/jaysus/base.rb', line 21

def self.attributes
  @attributes ||= []
end

.create(attrs = {}) ⇒ Object



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

def self.create(attrs = {})
  new(attrs).save
end

.decode(id, &block) ⇒ Object



55
56
57
58
59
# File 'lib/jaysus/base.rb', line 55

def self.decode(id, &block)
  ActiveSupport::JSON.decode(
    yield
  )[self.store_file_dir_name.singularize]
end

.find(id, &block) ⇒ Object



29
30
31
# File 'lib/jaysus/base.rb', line 29

def self.find(id, &block)
  new(decode(id, &block))
end

.find_all_by_attributes(attributes, *values) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/jaysus/base.rb', line 47

def self.find_all_by_attributes(attributes, *values)
  all.each.select do |record|
    attributes.zip(values).all? do |matcher|
      record.send(matcher.first) == matcher.last
    end
  end
end

.find_by_attributes(attributes, *values) ⇒ Object



43
44
45
# File 'lib/jaysus/base.rb', line 43

def self.find_by_attributes(attributes, *values)
  find_all_by_attributes(attributes, *values).first
end

.find_or_create_by_attributes(attributes, *values) ⇒ Object



33
34
35
36
37
38
39
40
41
# File 'lib/jaysus/base.rb', line 33

def self.find_or_create_by_attributes(attributes, *values)
  search = find_by_attributes(attributes, *values)
  return search unless search.blank?
  attrs = {}
  attributes.zip(values).each do |pair|
    attrs[pair.first] = pair.last
  end
  create(attrs)
end

.local_baseObject



65
66
67
# File 'lib/jaysus/base.rb', line 65

def self.local_base
  "#{model_name}::Local".constantize
end

.method_missing(method, *args) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
# File 'lib/jaysus/base.rb', line 113

def self.method_missing(method, *args)
  if method.to_s.match(/^find_by/)
    attrs = method.to_s.gsub(/^find_by_/, '').split('_and_')
    find_by_attributes(attrs, *args)
  elsif method.to_s.match(/^find_or_create_by/)
    attrs = method.to_s.gsub(/^find_or_create_by_/, '').split('_and_')
    find_or_create_by_attributes(attrs, *args)
  else
    super
  end
end

.model_baseObject



61
62
63
# File 'lib/jaysus/base.rb', line 61

def self.model_base
  "#{model_name}::Base".constantize
end

.model_nameObject



73
74
75
76
# File 'lib/jaysus/base.rb', line 73

def self.model_name
  parts = super.reverse.split('::',2).reverse.map { |p| p.reverse }
  ActiveModel::Name.new(parts.first.constantize)
end

.object_nameObject



97
98
99
# File 'lib/jaysus/base.rb', line 97

def self.object_name
  self.model_name.split('::').last
end

.plural_nameObject



78
79
80
# File 'lib/jaysus/base.rb', line 78

def self.plural_name
  model_name.plural
end

.plural_object_nameObject



101
102
103
# File 'lib/jaysus/base.rb', line 101

def self.plural_object_name
  self.object_name.underscore.pluralize
end

.primary_key(name = nil) ⇒ Object



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

def self.primary_key(name = nil)
  @primary_key = name if name.present?
  @primary_key
end

.remote_baseObject



69
70
71
# File 'lib/jaysus/base.rb', line 69

def self.remote_base
  "#{model_name}::Remote".constantize
end

.singular_nameObject



82
83
84
# File 'lib/jaysus/base.rb', line 82

def self.singular_name
  model_name.singular
end

.singular_object_nameObject



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

def self.singular_object_name
  self.object_name.underscore.singularize
end

.store_file_dirObject



91
92
93
94
95
# File 'lib/jaysus/base.rb', line 91

def self.store_file_dir
  dir = Local.store_dir.join(store_file_dir_name)
  dir.mkpath unless dir.exist?
  dir
end

.store_file_dir_nameObject



109
110
111
# File 'lib/jaysus/base.rb', line 109

def self.store_file_dir_name
  self.plural_object_name
end

Instance Method Details

#attributesObject



129
130
131
132
133
134
135
# File 'lib/jaysus/base.rb', line 129

def attributes
  out = {}
  (self.class.model_base.attributes + self.class.attributes).each do |attribute|
    out[attribute.to_s] = send(attribute)
  end
  out
end

#attributes=(attrs = {}) ⇒ Object



137
138
139
# File 'lib/jaysus/base.rb', line 137

def attributes=(attrs = {})
  set_attributes(attrs)
end

#destroy(&block) ⇒ Object



141
142
143
# File 'lib/jaysus/base.rb', line 141

def destroy(&block)
  yield
end

#persisted?Boolean

Returns:

  • (Boolean)


185
186
187
# File 'lib/jaysus/base.rb', line 185

def persisted?
  store_file.exist?
end

#save(&block) ⇒ Object



156
157
158
# File 'lib/jaysus/base.rb', line 156

def save(&block)
  yield if block_given?
end

#set_attributes(attrs) ⇒ Object



150
151
152
153
154
# File 'lib/jaysus/base.rb', line 150

def set_attributes(attrs)
  attrs.each_pair do |attr, value|
    self.send("#{attr}=", value)
  end
end

#store_fileObject



160
161
162
# File 'lib/jaysus/base.rb', line 160

def store_file
  self.class.store_file_dir.join(store_file_name)
end

#store_file_nameObject



164
165
166
167
168
169
170
171
# File 'lib/jaysus/base.rb', line 164

def store_file_name
  pk = send(self.class.model_base.primary_key)
  if pk.blank?
    pk = ActiveSupport::SecureRandom.hex(32) 
    send("#{self.class.model_base.primary_key}=", pk)
  end
  "#{send(self.class.model_base.primary_key)}"
end

#to_jsonObject



173
174
175
176
177
178
179
180
181
182
183
# File 'lib/jaysus/base.rb', line 173

def to_json
  {}.tap do |outer_hash|
    outer_hash[self.class.store_file_dir_name.singularize] = {}.tap do |inner_hash|
      (self.class.model_base.attributes + self.class.attributes).each do |attribute|
        if self.send(attribute).present?
          inner_hash[attribute] = self.send(attribute)
        end
      end
    end
  end.to_json
end

#update_attributes(attrs) ⇒ Object



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

def update_attributes(attrs)
  set_attributes(attrs)
  save
end