Class: FixedModel::Base

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::AttributeMethods, ActiveModel::Model
Defined in:
lib/fixed_model/base.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(row = {}) ⇒ Base

Returns a new instance of Base.



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

def initialize(row={})
  @row = row
  define_attribute_readers
end

Class Method Details

.allObject



49
50
51
# File 'lib/fixed_model/base.rb', line 49

def self.all
  data.map { |row| new(row) }
end

.attribute_namesObject



41
42
43
44
45
46
47
# File 'lib/fixed_model/base.rb', line 41

def self.attribute_names
  names = []
  data.each do |row|
    names.concat(row.keys)
  end
  names.uniq.map(&:to_s)
end

.each(&block) ⇒ Object



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

def self.each(&block)
  all.each(&block)
end

.inherited(subclass) ⇒ Object



31
32
33
34
35
# File 'lib/fixed_model/base.rb', line 31

def self.inherited(subclass)
  subclass.define_find_by_attributes
  subclass.define_find_by_attributes!
  define_attribute_methods(subclass.attribute_names)
end

Instance Method Details

#==(other) ⇒ Object



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

def ==(other)
  other.instance_of?(self.class) && other.attributes == attributes
end

#attribute?(attr) ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/fixed_model/base.rb', line 37

def attribute?(attr)
  send(attr.to_sym).present?
end

#attribute_namesObject



57
58
59
# File 'lib/fixed_model/base.rb', line 57

def attribute_names
  @row.keys.map(&:to_s)
end

#attributesObject



27
28
29
# File 'lib/fixed_model/base.rb', line 27

def attributes
  @row.dup.freeze
end

#new_record?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/fixed_model/base.rb', line 23

def new_record?
  false
end

#persisted?Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/fixed_model/base.rb', line 19

def persisted?
  true
end

#read_attribute(attribute) ⇒ Object



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

def read_attribute(attribute)
  @row[attribute]
end