Module: LightRecord

Extended by:
LightRecord
Included in:
LightRecord
Defined in:
lib/light_record.rb

Defined Under Namespace

Modules: RecordAttributes, RelationMethods

Instance Method Summary collapse

Instance Method Details

#base_extended(klass) ⇒ Object

Create LightRecord class based on klass argument



154
155
156
157
158
159
160
161
# File 'lib/light_record.rb', line 154

def base_extended(klass)
  @base_extended ||= {}
  if @base_extended[klass]
    return @base_extended[klass]
  end

  @base_extended[klass] = LightRecord.build_for_class(klass, klass.column_names)
end

#build_for_class(klass, fields) ⇒ Object

Create LightRecord class based on klass argument Used internally by scope#light_records and scope#light_records_each

Parameters:

  • klass (Class)

    ActiveRecord model

  • fields (Array)

    list of fields, will be used to define attribute methods

Returns:

  • new class base on klass argument but extended with LightRecord methods



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/light_record.rb', line 13

def build_for_class(klass, fields)
  new_klass = Class.new(klass) do
    self.table_name = klass.table_name
    self.inheritance_column = nil

    extend LightRecord::RecordAttributes

    define_fields(fields)

    def initialize(data)
      @attributes = data
      @readonly = true
      @association_cache = {}
      #init_internals
    end

    def self.model_name
      self.superclass.model_name
    end

    def read_attribute_before_type_cast(attr_name)
      @attributes[attr_name.to_sym]
    end

    def self.subclass_from_attributes?(attrs)
      false
    end

    def has_attribute?(attr_name)
      @attributes.has_key?(attr_name.to_sym)
    end

    def read_attribute(attr_name)
      @attributes[attr_name.to_sym]
    end

    def _read_attribute(attr_name)
      @attributes.fetch(attr_name.to_sym) { |n| yield n if block_given? }
    end

    def [](attr_name)
      @attributes[attr_name.to_sym]
    end

    def attributes
      @attributes
    end

    # to avoid errors when try saving data
    def remember_transaction_record_state
      @new_record ||= false
      @destroyed ||= false
      @_start_transaction_state ||= {}
      super
    end

    def sync_with_transaction_state
      nil
    end

    def new_record?
      false
    end

    # For Rails < 5.1
    # Assign without type casting, no support for alias, sorry
    def write_attribute_with_type_cast(attr_name, value, should_type_cast)
      @attributes[attr_name.to_sym] = value
    end

    # For Rails >= 5.1
    # Assign without type casting, no support for alias, sorry
    if ActiveRecord.version >= Gem::Version.new("5.1.0")
      def write_attribute(attr_name, value)
        attr_name = attr_name.to_sym
        attr_name = self.class.primary_key if attr_name == :id && self.class.primary_key
        @attributes[attr_name] = value
        value
      end

      def raw_write_attribute(attr_name, value) # :nodoc:
        @attributes[attr_name.to_sym] = value
        value
      end
    end
  end

  if klass.const_defined?(:LightRecord, false)
    new_klass.send(:prepend, klass::LightRecord)
    if klass::LightRecord.respond_to?(:included)
      klass::LightRecord.included(new_klass)
    end
  elsif klass.superclass.const_defined?(:LightRecord, false)
    new_klass.send(:prepend, klass.superclass::LightRecord)
    if klass.superclass::LightRecord.respond_to?(:included)
      klass.superclass::LightRecord.included(new_klass)
    end
  end

  new_klass
end