Module: Mamechiwa::ClassMethods
- Defined in:
- lib/mamechiwa.rb
Instance Attribute Summary collapse
-
#mame_config ⇒ Object
Returns the value of attribute mame_config.
-
#mame_group_field ⇒ Object
Returns the value of attribute mame_group_field.
-
#mame_scope ⇒ Object
Returns the value of attribute mame_scope.
Instance Method Summary collapse
- #create_embedded_class(attrs) ⇒ Object
- #define_validator(&validator) ⇒ Object
- #mame_group(name) {|_self| ... } ⇒ Object
- #mamechiwa(field, group_field = "") {|_self| ... } ⇒ Object
- #mattr(*name) ⇒ Object
Instance Attribute Details
#mame_config ⇒ Object
Returns the value of attribute mame_config.
5 6 7 |
# File 'lib/mamechiwa.rb', line 5 def mame_config @mame_config end |
#mame_group_field ⇒ Object
Returns the value of attribute mame_group_field.
5 6 7 |
# File 'lib/mamechiwa.rb', line 5 def mame_group_field @mame_group_field end |
#mame_scope ⇒ Object
Returns the value of attribute mame_scope.
5 6 7 |
# File 'lib/mamechiwa.rb', line 5 def mame_scope @mame_scope end |
Instance Method Details
#create_embedded_class(attrs) ⇒ Object
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 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/mamechiwa.rb', line 106 def (attrs) clazz = Class.new(Hash) do include ActiveModel::Validations define_method :mame_attrs do attrs end def self.name "MameciwaEmbedded" end def initialize(parent, field) @mame_parent = parent @mame_parent_field = field refresh end def refresh value = @mame_parent.send(:read_attribute, @mame_parent_field) mame_attrs.each do |attr| self.old_set(attr.to_s, nil) end unless value.blank? self.merge!(ActiveSupport::JSON.decode(value)) end end def serialize self.to_json end alias :old_get :[] def [](arg) self.old_get(arg) end alias :old_set :[]= def []=(*args) key, value = args self.old_set(key, value) @mame_parent.send(:write_attribute, @mame_parent_field.to_sym, self.serialize) end attrs.each do |attr| class_eval <<-EOF def #{attr} self["#{attr}"] end def #{attr}=(value) self["#{attr}"] = value end EOF end end end |
#define_validator(&validator) ⇒ Object
94 95 96 |
# File 'lib/mamechiwa.rb', line 94 def define_validator(&validator) mame_config[mame_scope][:validator] = validator end |
#mame_group(name) {|_self| ... } ⇒ Object
98 99 100 101 102 103 104 |
# File 'lib/mamechiwa.rb', line 98 def mame_group(name) self.mame_scope = name mame_config[mame_scope][:attrs] ||= [] mame_config[mame_scope][:validator] = Proc.new{ } yield self if block_given? self.mame_scope = "" end |
#mamechiwa(field, group_field = "") {|_self| ... } ⇒ Object
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 |
# File 'lib/mamechiwa.rb', line 15 def mamechiwa(field, group_field="") self.mame_group_field = group_field yield self self.mame_config.each do |k, scope| clazz = (scope[:attrs]) clazz.class_eval &scope[:validator] if scope[:validator] scope[:class] = clazz end class_eval do define_method :initialize_with_mame do unless @mame_embedded group = self.class.mame_group_field.length > 0 ? self.send(self.class.mame_group_field).to_s : "" if self.class.mame_config[group] && self.class.mame_config[group][:class] @mame_embedded = self.class.mame_config[group][:class].new(self, field) end end end define_method "#{field}=" do |value| initialize_with_mame if value.blank? write_attribute("#{field}", "{}") @mame_embedded.refresh if @mame_embedded return end hash = value hash = ActiveSupport::JSON.decode(value) if value.is_a?(String) if @mame_embedded write_attribute("#{field}", @mame_embedded.merge(hash).to_json) @mame_embedded.refresh else write_attribute("#{field}", hash.to_json) end end define_method "#{field}" do initialize_with_mame @mame_embedded end validate :mame_validator define_method :mame_validator do initialize_with_mame unless @mame_embedded errors.add("#{field}", "#{self.class.mame_group_field} is unregistered type.") else if (unregistered_attributes = (@mame_embedded.keys.map(&:to_sym) - @mame_embedded.mame_attrs)).size > 0 unregistered_attributes.each do |unregistered_attribute| errors.add("#{field}[#{unregistered_attribute}]", "is unregistered attribute.") end end if !@mame_embedded.valid? @mame_embedded.errors.each do |k, v| errors.add("#{field}[#{k}]", v) end end end end end end |
#mattr(*name) ⇒ Object
88 89 90 91 92 |
# File 'lib/mamechiwa.rb', line 88 def mattr(*name) mame_config[mame_scope][:attrs] ||= [] mame_config[mame_scope][:attrs] += [name].flatten mame_config[mame_scope][:attrs].uniq! end |