Module: PollEverywhere::Serializable::ClassMethods

Defined in:
lib/polleverywhere/serializable.rb

Instance Method Summary collapse

Instance Method Details

#from_hash(hash) ⇒ Object

Instanciate a class from a hash



77
78
79
# File 'lib/polleverywhere/serializable.rb', line 77

def from_hash(hash)
  new.from_hash(hash)
end

#prop(name, &block) ⇒ Object

Define a property at the class level



51
52
53
54
55
# File 'lib/polleverywhere/serializable.rb', line 51

def prop(name, &block)
  prop = props[name]
  prop.instance_eval(&block) if block
  prop
end

#propsObject

Setup attributes hash and delegate setters/getters to the base class.



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/polleverywhere/serializable.rb', line 58

def props
  @props ||= Hash.new do |props,name|
    # Define the methods at the instance level
    class_eval %{
      def #{name}=(val)
        props[:#{name}].current = val
      end

      def #{name}
        props[:#{name}].current
      end}
    
    # Setup the attribute class
    props[name] = Property.new(name)

  end.merge superclass_props
end

#root_key(name = nil) ⇒ Object

Set or get the root key of the model



46
47
48
# File 'lib/polleverywhere/serializable.rb', line 46

def root_key(name=nil)
  name ? @name = name.to_sym : @name
end