Class: ArDocStore::Attributes::Base
- Inherits:
-
Object
- Object
- ArDocStore::Attributes::Base
- Includes:
- CallbackSupport
- Defined in:
- lib/ar_doc_store/attributes/base.rb
Direct Known Subclasses
Array, Boolean, Date, Datetime, Decimal, EmbedsBase, Enumeration, Float, Integer, String
Instance Attribute Summary collapse
-
#attribute ⇒ Object
Returns the value of attribute attribute.
-
#conversion ⇒ Object
Returns the value of attribute conversion.
-
#default ⇒ Object
Returns the value of attribute default.
-
#model ⇒ Object
Returns the value of attribute model.
-
#options ⇒ Object
Returns the value of attribute options.
-
#predicate ⇒ Object
Returns the value of attribute predicate.
Class Method Summary collapse
Instance Method Summary collapse
- #add_ransacker ⇒ Object
- #attribute_options ⇒ Object
- #attribute_type ⇒ Object
- #build ⇒ Object
- #define_query_method ⇒ Object
- #embedded? ⇒ Boolean
-
#initialize(model, attribute, options) ⇒ Base
constructor
A new instance of Base.
-
#store_attribute ⇒ Object
:nodoc:.
Methods included from CallbackSupport
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, ) @model, @attribute, = model, attribute, @default = .delete(:default) end |
Instance Attribute Details
#attribute ⇒ Object
Returns the value of attribute attribute.
6 7 8 |
# File 'lib/ar_doc_store/attributes/base.rb', line 6 def attribute @attribute end |
#conversion ⇒ Object
Returns the value of attribute conversion.
6 7 8 |
# File 'lib/ar_doc_store/attributes/base.rb', line 6 def conversion @conversion end |
#default ⇒ Object
Returns the value of attribute default.
6 7 8 |
# File 'lib/ar_doc_store/attributes/base.rb', line 6 def default @default end |
#model ⇒ Object
Returns the value of attribute model.
6 7 8 |
# File 'lib/ar_doc_store/attributes/base.rb', line 6 def model @model end |
#options ⇒ Object
Returns the value of attribute options.
6 7 8 |
# File 'lib/ar_doc_store/attributes/base.rb', line 6 def end |
#predicate ⇒ Object
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, ={}) new(model, attribute, ).build end |
Instance Method Details
#add_ransacker ⇒ Object
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_options ⇒ Object
49 50 51 |
# File 'lib/ar_doc_store/attributes/base.rb', line 49 def {} end |
#attribute_type ⇒ Object
45 46 47 |
# File 'lib/ar_doc_store/attributes/base.rb', line 45 def attribute_type ActiveModel::Type::Value end |
#build ⇒ Object
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_method ⇒ Object
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 |
#store_attribute ⇒ Object
: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 = .merge!(default: default) if default.present? model.class_eval do attribute attribute_name, attribute_type.new, ** 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 |