Class: ActiveRecord::Bixformer::Model::Base
- Inherits:
-
Object
- Object
- ActiveRecord::Bixformer::Model::Base
- Defined in:
- lib/activerecord-bixformer/model/base.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#associations ⇒ Hash<String, ActiveRecord::Bixformer::Model::Base>
readonly
the import/export target association names and its instance.
-
#attributes ⇒ Hash<String, ActiveRecord::Bixformer::Attribute::Base>
readonly
the import/export target attribute names and its instance.
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
-
#name ⇒ String
readonly
the name or association name of handled ActiveRecord.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#parent ⇒ ActiveRecord::Bixformer::Model::Base
readonly
the instance has parent association.
-
#preferred_skip_attributes ⇒ Array<String>
readonly
the list of attribute name to not make key if its value is blank.
-
#translator ⇒ ActiveRecord::Bixformer::Translator::I18n
readonly
The current value of translator.
Instance Method Summary collapse
-
#activerecord_constant ⇒ Constant
The constant value of handling ActiveRecord.
- #add_association(model) ⇒ Object
- #find_record_by!(condition) ⇒ Object
-
#initialize(model_or_association_name, options) ⇒ Base
constructor
A new instance of Base.
-
#parent_foreign_key ⇒ String
The foreign key name to associate to parent ActiveRecord.
- #parents ⇒ Object
- #plan ⇒ Object
- #set_parent(model) ⇒ Object
- #setup(plan) ⇒ Object
- #should_be_included ⇒ Object
- #translate(attribute_name) ⇒ Object
Methods included from ImportValueValidatable
Constructor Details
#initialize(model_or_association_name, options) ⇒ Base
Returns a new instance of Base.
22 23 24 25 26 27 |
# File 'lib/activerecord-bixformer/model/base.rb', line 22 def initialize(model_or_association_name, ) @name = model_or_association_name.to_s = (.is_a?(::Hash) ? : {}).with_indifferent_access @associations = [] @errors = ::ActiveRecord::Bixformer::Errors.new end |
Instance Attribute Details
#associations ⇒ Hash<String, ActiveRecord::Bixformer::Model::Base> (readonly)
the import/export target association names and its instance.
15 16 17 |
# File 'lib/activerecord-bixformer/model/base.rb', line 15 def associations @associations end |
#attributes ⇒ Hash<String, ActiveRecord::Bixformer::Attribute::Base> (readonly)
the import/export target attribute names and its instance.
15 16 17 |
# File 'lib/activerecord-bixformer/model/base.rb', line 15 def attributes @attributes end |
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
19 20 21 |
# File 'lib/activerecord-bixformer/model/base.rb', line 19 def errors @errors end |
#name ⇒ String (readonly)
the name or association name of handled ActiveRecord
15 16 17 |
# File 'lib/activerecord-bixformer/model/base.rb', line 15 def name @name end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
19 20 21 |
# File 'lib/activerecord-bixformer/model/base.rb', line 19 def end |
#parent ⇒ ActiveRecord::Bixformer::Model::Base (readonly)
the instance has parent association.
15 16 17 |
# File 'lib/activerecord-bixformer/model/base.rb', line 15 def parent @parent end |
#preferred_skip_attributes ⇒ Array<String> (readonly)
the list of attribute name to not make key if its value is blank.
15 16 17 |
# File 'lib/activerecord-bixformer/model/base.rb', line 15 def preferred_skip_attributes @preferred_skip_attributes end |
#translator ⇒ ActiveRecord::Bixformer::Translator::I18n (readonly)
Returns the current value of translator.
15 16 17 |
# File 'lib/activerecord-bixformer/model/base.rb', line 15 def translator @translator end |
Instance Method Details
#activerecord_constant ⇒ Constant
Returns the constant value of handling ActiveRecord.
77 78 79 80 81 82 83 84 |
# File 'lib/activerecord-bixformer/model/base.rb', line 77 def activerecord_constant @activerecord_constant ||= if @parent @parent.activerecord_constant.reflections[@name].class_name.constantize else @name.camelize.constantize end end |
#add_association(model) ⇒ Object
70 71 72 73 74 |
# File 'lib/activerecord-bixformer/model/base.rb', line 70 def add_association(model) @associations.push(model) model.set_parent(self) end |
#find_record_by!(condition) ⇒ Object
138 139 140 |
# File 'lib/activerecord-bixformer/model/base.rb', line 138 def find_record_by!(condition) activerecord_constant.find_by!(condition) end |
#parent_foreign_key ⇒ String
Returns the foreign key name to associate to parent ActiveRecord.
64 65 66 67 68 |
# File 'lib/activerecord-bixformer/model/base.rb', line 64 def parent_foreign_key return nil unless @parent @parent_foreign_key ||= @parent.activerecord_constant.reflections[@name].foreign_key end |
#parents ⇒ Object
59 60 61 |
# File 'lib/activerecord-bixformer/model/base.rb', line 59 def parents @parent ? [*parent.parents, @parent] : [] end |
#plan ⇒ Object
51 52 53 |
# File 'lib/activerecord-bixformer/model/base.rb', line 51 def plan @plan.raw_value end |
#set_parent(model) ⇒ Object
55 56 57 |
# File 'lib/activerecord-bixformer/model/base.rb', line 55 def set_parent(model) @parent = model end |
#setup(plan) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/activerecord-bixformer/model/base.rb', line 29 def setup(plan) @plan = ActiveRecord::Bixformer::PlanAccessor.new(plan) entry = @plan.pickup_value_for(self, :entry, {}) @attributes = (entry[:attributes] || {}).map do |attribute_name, attribute_value| attribute_type, = @plan.(attribute_value) @plan.new_module_instance(:attribute, attribute_type, self, attribute_name, ) end @preferred_skip_attributes = @plan.pickup_value_for(self, :preferred_skip_attributes, []) @default_values = @plan.pickup_value_for(self, :default_values, {}) @sort_indexes = @plan.pickup_value_for(self, :sort_indexes, {}) # At present, translation function is only i18n @translator = ::ActiveRecord::Bixformer::Translator::I18n.new @translator.config = @plan.value_of(:translation_config).dup @translator.model = self end |
#should_be_included ⇒ Object
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 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/activerecord-bixformer/model/base.rb', line 86 def should_be_included arr = [] hash = {} symbolizer = -> (value) do case value when ::Hash value.map { |k, v| [k.to_sym, symbolizer.call(v)]}.to_h when ::Array value.map { |v| symbolizer.call(v) } when ::String value.to_sym else value end end @attributes.each do |attr| attr_should_be_included = attr.should_be_included next unless attr_should_be_included if attr_should_be_included.is_a?(::Hash) hash.merge!(symbolizer.call(attr_should_be_included)) elsif attr_should_be_included.is_a?(::Array) arr.push *symbolizer.call(attr_should_be_included) else arr.push symbolizer.call(attr_should_be_included) end end @associations.each do |assoc| assoc_should_be_included = assoc.should_be_included if assoc_should_be_included.empty? arr.push assoc.name.to_sym else hash[assoc.name.to_sym] = assoc_should_be_included end end if hash.empty? && arr.empty? [] elsif hash.empty? arr elsif arr.empty? hash else [*arr, hash] end end |
#translate(attribute_name) ⇒ Object
142 143 144 |
# File 'lib/activerecord-bixformer/model/base.rb', line 142 def translate(attribute_name) @translator.translate_attribute(attribute_name) end |