Class: FrozenRecord::Base

Inherits:
Object
  • Object
show all
Extended by:
ActiveModel::Naming
Includes:
ActiveModel::AttributeMethods, ActiveModel::Conversion, ActiveModel::Serializers::JSON, ActiveModel::Serializers::Xml
Defined in:
lib/frozen_record/base.rb

Defined Under Namespace

Classes: ThreadSafeStorage

Constant Summary collapse

FIND_BY_PATTERN =
/\Afind_by_(\w+)(!?)/
FALSY_VALUES =
[false, nil, 0, ''].to_set

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs = {}) ⇒ Base

Returns a new instance of Base.



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

def initialize(attrs = {})
  @attributes = attrs.stringify_keys
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



108
109
110
# File 'lib/frozen_record/base.rb', line 108

def attributes
  @attributes
end

Class Method Details

.current_scopeObject Also known as: all



41
42
43
# File 'lib/frozen_record/base.rb', line 41

def current_scope
  store[:scope] ||= Scope.new(self, load_records)
end

.current_scope=(scope) ⇒ Object



46
47
48
# File 'lib/frozen_record/base.rb', line 46

def current_scope=(scope)
  store[:scope] = scope
end

.file_pathObject



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

def file_path
  raise "You must define `#{name}.base_path`" unless base_path
  File.join(base_path, "#{name.underscore.pluralize}.yml")
end

.respond_to_missing?(name) ⇒ Boolean

Returns:

  • (Boolean)


58
59
60
61
62
# File 'lib/frozen_record/base.rb', line 58

def respond_to_missing?(name, *)
  if name.to_s =~ FIND_BY_PATTERN
    return true if $1.split('_and_').all? { |attr| public_method_defined?(attr) }
  end
end

Instance Method Details

#==(other) ⇒ Object



123
124
125
# File 'lib/frozen_record/base.rb', line 123

def ==(other)
  super || other.is_a?(self.class) && other.id == id
end

#[](attr) ⇒ Object Also known as: attribute



118
119
120
# File 'lib/frozen_record/base.rb', line 118

def [](attr)
  @attributes[attr.to_s]
end

#idObject



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

def id
  self[primary_key]
end

#persisted?Boolean

Returns:

  • (Boolean)


127
128
129
# File 'lib/frozen_record/base.rb', line 127

def persisted?
  true
end

#to_keyObject



131
132
133
# File 'lib/frozen_record/base.rb', line 131

def to_key
  [id]
end