Class: Sylfy::DataModel
- Inherits:
-
Object
- Object
- Sylfy::DataModel
- Defined in:
- lib/sylfy/datamodel.rb
Overview
Model template
Defined Under Namespace
Classes: DuplicateError
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(data = nil) ⇒ DataModel
constructor
Initialize function.
- #to_hash ⇒ Hash
-
#to_json ⇒ JSON
convert data object to JSON.
- #to_str ⇒ String
-
#valid? ⇒ Boolean
Test if data object is valid.
Constructor Details
#initialize(data = nil) ⇒ DataModel
Initialize function
18 19 20 21 22 23 24 25 26 |
# File 'lib/sylfy/datamodel.rb', line 18 def initialize(data = nil) @data = {class: self.class.to_sym} if data data.each_pair do |field, value| self.send "#{field}=".to_sym, value if self.method_defined?(field) end end end |
Instance Attribute Details
#data ⇒ Object (readonly)
Returns the value of attribute data.
15 16 17 |
# File 'lib/sylfy/datamodel.rb', line 15 def data @data end |
Class Method Details
.has_many(*attrs) ⇒ Object
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/sylfy/datamodel.rb', line 107 def self.has_many(*attrs) @@has_many ||= [] attrs.each do |attr| if !self.method_defined?(attr) @@has_many.push(attr) self.class_eval do define_method(attr) do @data[attr] = [] end define_method("#{attr}=") do |*param| @data[attr] = param.flatten end define_method("#{attr}_add") do |*param| @data[attr] += param.flatten end end else raise DuplicateError, "#{attr.inspect} is already defined." end end end |
.has_one(*attrs) ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/sylfy/datamodel.rb', line 87 def self.has_one(*attrs) @@has_one ||= [] attrs.each do |attr| if !self.method_defined?(attr) @@has_one.push(attr) self.class_eval do define_method(attr) do @data[attr] end define_method("#{attr}=") do |param| @data[attr] = param.kind_of?(DataModel) ? param.to_hash : param end end else raise DuplicateError, "#{attr.inspect} is already defined." end end end |
.validates(*attrs, rules) ⇒ Object
131 132 133 134 135 136 137 |
# File 'lib/sylfy/datamodel.rb', line 131 def self.validates(*attrs, rules) @@validates ||= {} attrs.each do |attr| @@validates[attr] ||= {} @@validates[attr].merge!(rules) end end |
Instance Method Details
#to_hash ⇒ Hash
38 39 40 |
# File 'lib/sylfy/datamodel.rb', line 38 def to_hash() return @data end |
#to_json ⇒ JSON
convert data object to JSON
30 31 32 |
# File 'lib/sylfy/datamodel.rb', line 30 def to_json() return @data.to_json end |
#to_str ⇒ String
34 35 36 |
# File 'lib/sylfy/datamodel.rb', line 34 def to_str() return JSON::pretty_generate(@data) end |
#valid? ⇒ Boolean
Test if data object is valid
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 |
# File 'lib/sylfy/datamodel.rb', line 42 def valid?() if @@validates result = true @@validates.each_pair do |field, rules| rules.each_pair do |type, rule| case type.to_sym when :type if rule.kind_of?(Array) rule.each {|e| result &&= self.assert_type_of?(e, @data[field.to_sym])} else result &&= self.assert_type_of?(rule, @data[field.to_sym]) end when :presence result &&= !(rule ^ (@data[field.to_sym] != nil && !@data[field.to_sym].empty?)) when :format if rule.kind_of?(Array) rule.each {|e| result &&= self.assert_match?(e, @data[field.to_sym])} else result &&= self.assert_match?(rule, @data[field.to_sym]) end end end end return result else return true end end |