Module: AssociateJsonb::WithStoreAttribute::ClassMethods

Defined in:
lib/associate_jsonb/with_store_attribute.rb

Instance Method Summary collapse

Instance Method Details

#add_store_column_attribute_name(name, store, key) ⇒ Object



59
60
61
62
63
# File 'lib/associate_jsonb/with_store_attribute.rb', line 59

def add_store_column_attribute_name(name, store, key)
  store_column_attribute_tracker.synchronize do
    store_column_attribute_tracker.add_name(name, store, key)
  end
end

#data_column_attribute(*args, **opts) ⇒ Object



82
83
84
# File 'lib/associate_jsonb/with_store_attribute.rb', line 82

def data_column_attribute(*args, **opts)
  store_column_attribute :data, *args, **opts
end

#inherited(child) ⇒ Object



35
36
37
38
# File 'lib/associate_jsonb/with_store_attribute.rb', line 35

def inherited(child)
  child.initialize_store_column_attribute_tracker
  super
end

#initialize_store_column_attribute_trackerObject



40
41
42
43
44
45
# File 'lib/associate_jsonb/with_store_attribute.rb', line 40

def initialize_store_column_attribute_tracker
  @store_column_attribute_tracker = const_set(:StoreColumnAttributeTracker, StoreColumnAttributeTracker.new)
  private_constant :StoreColumnAttributeTracker

  store_column_attribute_tracker
end

#is_store_column_attribute?(name) ⇒ Boolean

Returns:



65
66
67
68
69
# File 'lib/associate_jsonb/with_store_attribute.rb', line 65

def is_store_column_attribute?(name)
  store_column_attribute_tracker.synchronize do
    store_column_attribute_tracker.has_name?(name)
  end
end

#set_store_column_attribute_values_on_initObject



71
72
73
74
75
76
77
78
79
80
# File 'lib/associate_jsonb/with_store_attribute.rb', line 71

def set_store_column_attribute_values_on_init
  lambda do
    self.class.store_column_attribute_names.each do |attr, opts|
      _write_attribute(attr, _read_attribute(opts[:store])[opts[:key]])
      clear_attribute_change(attr) if persisted?
    end
  rescue
    nil
  end
end

#store_column_attribute(store, attr, *opts, key: nil, **attribute_opts) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/associate_jsonb/with_store_attribute.rb', line 86

def store_column_attribute(store, attr, *opts, key: nil, **attribute_opts)
  store = store.to_sym
  attr = attr.to_sym
  key ||= attr
  key = key.to_s
  array = attribute_opts[:array]
  attribute attr, *opts, **attribute_opts

  instance_eval "    add_store_column_attribute_name(\"\#{attr}\", :\#{store}, \"\#{key}\")\n  CODE\n\n  include WithStoreAttribute::InstanceMethodsOnActivation.new(self, store, attr, key, array)\nend\n", __FILE__, __LINE__ + 1

#store_column_attribute_namesObject



51
52
53
54
55
56
57
# File 'lib/associate_jsonb/with_store_attribute.rb', line 51

def store_column_attribute_names
  store_column_attribute_tracker.synchronize do
    current_store_col_names = {}
    current_store_col_names.merge!(super) if defined? super
    current_store_col_names.merge!(store_column_attribute_tracker.names_list)
  end
end

#store_column_attribute_trackerObject



47
48
49
# File 'lib/associate_jsonb/with_store_attribute.rb', line 47

def store_column_attribute_tracker
  @store_column_attribute_tracker ||= initialize_store_column_attribute_tracker
end