Module: Hashoid::ClassMethods
- Defined in:
- lib/hashoid.rb
Instance Method Summary collapse
- #_fields ⇒ Object
- #alias_boolean(field) ⇒ Object
- #collection(name, opts = {}) ⇒ Object
- #collections(names, opts = {}) ⇒ Object
- #field(name, opts = {}) ⇒ Object
- #field_default(field) ⇒ Object
- #field_names ⇒ Object
- #field_transform(field) ⇒ Object
- #field_type(field, &block) ⇒ Object
- #fields(names, opts = {}) ⇒ Object
- #find_class(field) ⇒ Object
- #from_json(json) ⇒ Object
- #is_collection?(field) ⇒ Boolean
- #my_module ⇒ Object
Instance Method Details
#_fields ⇒ Object
33 34 35 |
# File 'lib/hashoid.rb', line 33 def _fields @fields ||= superclass.respond_to?(:_fields) ? {}.merge(superclass._fields) : {} end |
#alias_boolean(field) ⇒ Object
67 68 69 |
# File 'lib/hashoid.rb', line 67 def alias_boolean field alias_method("#{field}?", field) end |
#collection(name, opts = {}) ⇒ Object
21 22 23 |
# File 'lib/hashoid.rb', line 21 def collection name, opts={} field(name, opts.merge(collection: true)) end |
#collections(names, opts = {}) ⇒ Object
25 26 27 |
# File 'lib/hashoid.rb', line 25 def collections names, opts={} names.each{|name| collection(name, opts)} end |
#field(name, opts = {}) ⇒ Object
12 13 14 15 |
# File 'lib/hashoid.rb', line 12 def field name, opts={} _fields[name] = opts.dup attr_reader name end |
#field_default(field) ⇒ Object
51 52 53 |
# File 'lib/hashoid.rb', line 51 def field_default field _fields[field] && _fields[field][:default] end |
#field_names ⇒ Object
41 42 43 |
# File 'lib/hashoid.rb', line 41 def field_names _fields.keys end |
#field_transform(field) ⇒ Object
55 56 57 |
# File 'lib/hashoid.rb', line 55 def field_transform field _fields[field] && _fields[field][:transform] end |
#field_type(field, &block) ⇒ Object
45 46 47 48 49 |
# File 'lib/hashoid.rb', line 45 def field_type(field, &block) info = _fields[field] ||= {} info[:checked], info[:type] = true, yield unless info[:type] or info[:checked] info[:type] end |
#fields(names, opts = {}) ⇒ Object
17 18 19 |
# File 'lib/hashoid.rb', line 17 def fields names, opts={} names.each{|name| field(name, opts)} end |
#find_class(field) ⇒ Object
59 60 61 62 63 64 65 |
# File 'lib/hashoid.rb', line 59 def find_class field field_type(field) do class_name = field.to_s.split('_').map(&:capitalize).join class_name = class_name.singularize if is_collection?(field) my_module.const_get(class_name) if my_module.const_defined?(class_name) end end |
#from_json(json) ⇒ Object
29 30 31 |
# File 'lib/hashoid.rb', line 29 def from_json(json) self.new(JSON.parse(json)) end |
#is_collection?(field) ⇒ Boolean
37 38 39 |
# File 'lib/hashoid.rb', line 37 def is_collection? field _fields[field] && _fields[field][:collection] end |
#my_module ⇒ Object
71 72 73 |
# File 'lib/hashoid.rb', line 71 def my_module @my_module ||= (self.name =~ /^(.+)::[^:]+$/) ? $1.constantize : Module end |