Class: MetadataType
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- MetadataType
- Defined in:
- lib/metadata/metadata_type.rb
Constant Summary collapse
- DATATYPES =
{ :string => "string", :text => "text", :date => "date", :datetime => "datetime", :number => "number", :boolean => "boolean", :array => 'array' }
Instance Attribute Summary collapse
-
#default_json ⇒ Object
Returns the value of attribute default_json.
-
#models_json ⇒ Object
Returns the value of attribute models_json.
-
#values_json ⇒ Object
Returns the value of attribute values_json.
Class Method Summary collapse
- .default ⇒ Object
- .drop_cache(scope = nil) ⇒ Object
- .model_types(model, scope = nil) ⇒ Object
- .scheme(scope = nil) ⇒ Object
- .scheme_data(scope = nil) ⇒ Object
- .type(name, scope = nil) ⇒ Object
Instance Method Summary collapse
Instance Attribute Details
#default_json ⇒ Object
Returns the value of attribute default_json.
18 19 20 |
# File 'lib/metadata/metadata_type.rb', line 18 def default_json @default_json end |
#models_json ⇒ Object
Returns the value of attribute models_json.
18 19 20 |
# File 'lib/metadata/metadata_type.rb', line 18 def models_json @models_json end |
#values_json ⇒ Object
Returns the value of attribute values_json.
18 19 20 |
# File 'lib/metadata/metadata_type.rb', line 18 def values_json @values_json end |
Class Method Details
.default ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/metadata/metadata_type.rb', line 28 def self.default self.new({ :tag => :sample, :name => "Sample", :models => [:any], :mandatory => false, :multiple => false, :default => 'default', :datatype => "string", :format => nil, :values => nil }) end |
.drop_cache(scope = nil) ⇒ Object
136 137 138 139 140 |
# File 'lib/metadata/metadata_type.rb', line 136 def self.drop_cache(scope=nil) Rails.cache.delete("metadata_scheme_#{@@metadata_scope}#{scope}_data") Rails.cache.delete("metadata_scheme_#{@@metadata_scope}#{scope}_types") Rails.cache.delete("metadata_scheme_#{@@metadata_scope}#{scope}_modeltypes") end |
.model_types(model, scope = nil) ⇒ Object
118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/metadata/metadata_type.rb', line 118 def self.model_types(model, scope=nil) types = Rails.cache.fetch("metadata_scheme_#{@@metadata_scope}#{scope}_modeltypes", :expires_in => 60.minutes) do res = { :any => [] } self.scheme(scope).each do |tag, type| type.models.each do |model| res[model] = [] if res[model].blank? res[model] << tag end if type.models end res end types[model] ? (types[model] | types[:any]).uniq : types[:any] end |
.scheme(scope = nil) ⇒ Object
108 109 110 111 112 113 114 115 116 |
# File 'lib/metadata/metadata_type.rb', line 108 def self.scheme(scope=nil) Rails.cache.fetch("metadata_scheme_#{@@metadata_scope}#{scope}_types", :expires_in => 60.minutes) do res = {} self.scheme_data(scope).each do |type| res[type.tag] = type end res end end |
.scheme_data(scope = nil) ⇒ Object
95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/metadata/metadata_type.rb', line 95 def self.scheme_data(scope=nil) Rails.cache.fetch("metadata_scheme_#{@@metadata_scope}#{scope}_data", :expires_in => 60.minutes) do uncached do scheme_data = scope.blank? ? self.all : self.where(@@metadata_scope => scope).all if scheme_data.count > 0 scheme_data else [] end end end end |
.type(name, scope = nil) ⇒ Object
132 133 134 |
# File 'lib/metadata/metadata_type.rb', line 132 def self.type(name, scope=nil) self.scheme(scope)[name] end |
Instance Method Details
#default ⇒ Object
83 84 85 |
# File 'lib/metadata/metadata_type.rb', line 83 def default type_cast(attributes['default']) end |
#type_cast(value) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/metadata/metadata_type.rb', line 42 def type_cast(value) return value.map {|x| type_cast x } if value.is_a? Array return nil if value.nil? && datatype != 'boolean' return value unless value.is_a?(String) || value.nil? case datatype when 'date' ActiveRecord::ConnectionAdapters::Column.string_to_date value when 'datetime' ActiveRecord::ConnectionAdapters::Column.string_to_time value when 'number' Integer value when 'boolean' ActiveRecord::ConnectionAdapters::Column.value_to_boolean value else value end rescue nil end |
#values=(value) ⇒ Object
69 70 71 72 |
# File 'lib/metadata/metadata_type.rb', line 69 def values= value value = value.invert.to_a if value.is_a?(Hash) super end |