Module: PushType::Customizable::ClassMethods

Defined in:
app/models/concerns/push_type/customizable.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#fieldsObject (readonly)

Returns the value of attribute fields.



43
44
45
# File 'app/models/concerns/push_type/customizable.rb', line 43

def fields
  @fields
end

Instance Method Details

#field(name, *args, &blk) ⇒ Object

Raises:

  • (ArgumentError)


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
# File 'app/models/concerns/push_type/customizable.rb', line 49

def field(name, *args, &blk)
  raise ArgumentError if args.size > 2
  kind, opts = case args.size
    when 2 then args
    when 1 then args[0].is_a?(Hash) ? args.insert(0, :string) : args.insert(-1, {})
    else        [ :string, {} ]
  end

  # Store field type and set accessor
  fields[name] = [ field_type(kind), opts, blk ].compact
  store_accessor :field_store, name

  # Set inline validates
  validates name, opts[:validates] if opts[:validates]

  # Override setter accessor
  define_method "#{ name }=".to_sym do |val|
    f = initialized_field(name)
    super f.primitive.to_json(val)
  end

  # Override getter accessor
  define_method name do
    self.fields[name].value
  end

  if block = fields[name][0].def_block
    block.call(self, name, fields[name][0])
  end
end