Module: Hari::Entity::Property::Builder

Defined in:
lib/hari/entity/property/builder.rb

Instance Method Summary collapse

Instance Method Details

#properties(*args) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/hari/entity/property/builder.rb', line 27

def properties(*args)
  options = args.extract_options!
  args.each { |name| property name, options }

  @properties ||= begin
    if self == Hari::Entity
      []
    else
      entities_ancestors = ancestors.select do |a|
        a.ancestors.include? Hari::Entity
      end

      entity = entities_ancestors[1]
      entity ? entity.properties.dup : []
    end
  end
end

#property(name, options = {}) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/hari/entity/property/builder.rb', line 6

def property(name, options = {})
  attr_reader name
  define_attribute_method name

  validates_presence_of name if options[:required]

  define_method "#{name}=" do |value|
    unless send(name) == value
      send "#{name}_will_change!"
    end

    instance_variable_set "@#{name}", value
  end

  if options[:type] == Serialization::Boolean
    define_method("#{name}?") { send name }
  end

  properties << Property.new(self, name, options)
end