Class: ArDocStore::Attributes::Base

Inherits:
Object
  • Object
show all
Includes:
CallbackSupport
Defined in:
lib/ar_doc_store/attributes/base.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from CallbackSupport

included

Constructor Details

#initialize(model, attribute, options) ⇒ Base

Returns a new instance of Base.



15
16
17
18
# File 'lib/ar_doc_store/attributes/base.rb', line 15

def initialize(model, attribute, options)
  @model, @attribute, @options = model, attribute, options
  @default = options.delete(:default)
end

Instance Attribute Details

#attributeObject

Returns the value of attribute attribute.



6
7
8
# File 'lib/ar_doc_store/attributes/base.rb', line 6

def attribute
  @attribute
end

#conversionObject

Returns the value of attribute conversion.



6
7
8
# File 'lib/ar_doc_store/attributes/base.rb', line 6

def conversion
  @conversion
end

#defaultObject

Returns the value of attribute default.



6
7
8
# File 'lib/ar_doc_store/attributes/base.rb', line 6

def default
  @default
end

#modelObject

Returns the value of attribute model.



6
7
8
# File 'lib/ar_doc_store/attributes/base.rb', line 6

def model
  @model
end

#optionsObject

Returns the value of attribute options.



6
7
8
# File 'lib/ar_doc_store/attributes/base.rb', line 6

def options
  @options
end

#predicateObject

Returns the value of attribute predicate.



6
7
8
# File 'lib/ar_doc_store/attributes/base.rb', line 6

def predicate
  @predicate
end

Class Method Details

.build(model, attribute, options = {}) ⇒ Object



11
12
13
# File 'lib/ar_doc_store/attributes/base.rb', line 11

def self.build(model, attribute, options={})
  new(model, attribute, options).build
end

Instance Method Details

#add_ransackerObject



57
58
59
60
61
# File 'lib/ar_doc_store/attributes/base.rb', line 57

def add_ransacker
  model.class_eval "    add_ransacker :\#{attribute}, \"\#{predicate}\"\n  CODE\nend\n", __FILE__, __LINE__ + 1

#attribute_optionsObject



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

def attribute_options
  {}
end

#attribute_typeObject



45
46
47
# File 'lib/ar_doc_store/attributes/base.rb', line 45

def attribute_type
  ActiveModel::Type::Value
end

#buildObject



20
21
22
23
24
25
# File 'lib/ar_doc_store/attributes/base.rb', line 20

def build
  run_callbacks :build do
    store_attribute
  end
  self
end

#define_query_methodObject



63
64
65
66
67
68
69
# File 'lib/ar_doc_store/attributes/base.rb', line 63

def define_query_method
  model.class_eval "    def \#{attribute}?\n      !!\#{attribute}\n    end\n  CODE\nend\n", __FILE__, __LINE__ + 1

#embedded?Boolean

Returns:



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

def embedded?
  false
end

#store_attributeObject

:nodoc:



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/ar_doc_store/attributes/base.rb', line 28

def store_attribute
  attribute_name = @attribute
  attribute_type = self.attribute_type
  options = attribute_options
  options.merge!(default: default) if default.present?
  model.class_eval do
    attribute attribute_name, attribute_type.new, **options
    define_method "#{attribute_name}=".to_sym, -> (value) {
      value = nil if value == '' || value == ['']
      send :attribute=, attribute_name, value
      new_value = send :attribute, attribute_name
      write_store_attribute(json_column, attribute_name, new_value)
      new_value
    }
  end
end