Module: Horcrux::Entity::ClassMethods

Defined in:
lib/horcrux/entity.rb

Instance Method Summary collapse

Instance Method Details

#attr(type, *keys) ⇒ Object

Public



19
20
21
22
23
24
25
26
# File 'lib/horcrux/entity.rb', line 19

def attr(type, *keys)
  method = "build_#{type}_attr"
  if respond_to?(method)
    send method, keys, false
  else
    raise TypeError, "Type #{type.inspect} not recognized."
  end
end

#build_array_attr(keys, is_readonly) ⇒ Object



74
75
76
# File 'lib/horcrux/entity.rb', line 74

def build_array_attr(keys, is_readonly)
  build_class_attr(Array, keys, is_readonly)
end

#build_attrs(keys, is_readonly) ⇒ Object



113
114
115
116
117
118
119
120
121
# File 'lib/horcrux/entity.rb', line 113

def build_attrs(keys, is_readonly)
  keys.each do |key|
    key_s = key.to_s
    @default_attribute ||= key_s
    @attributes << key_s
    @writable_attributes << key_s unless is_readonly
    yield key_s
  end
end

#build_bool_attr(keys, is_readonly) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/horcrux/entity.rb', line 54

def build_bool_attr(keys, is_readonly)
  attr_reader *keys

  build_attrs(keys, is_readonly) do |key_s|
    ivar_key = "@#{key_s}"

    define_method "#{key_s}?" do
      !!instance_variable_get(ivar_key)
    end

    define_method "#{key_s}=" do |value|
      instance_variable_set ivar_key, case value
        when Fixnum  then value > 0
        when /t|f/i  then value =~ /t/i ? true : false
        else !!value
      end
    end
  end
end

#build_class_attr(klass, keys, is_readonly) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/horcrux/entity.rb', line 96

def build_class_attr(klass, keys, is_readonly)
  build_attrs(keys, is_readonly) do |key_s|
    ivar_key = "@#{key_s}"
    define_method key_s do
      instance_variable_get(ivar_key) ||
        instance_variable_set(ivar_key, klass.new)
    end

    define_method "#{key_s}=" do |value|
      unless value.is_a?(klass)
        raise TypeError, "#{key_s} should be a #{klass}: #{value.inspect}"
      end
      instance_variable_set(ivar_key, value)
    end
  end
end

#build_hash_attr(keys, is_readonly) ⇒ Object



78
79
80
# File 'lib/horcrux/entity.rb', line 78

def build_hash_attr(keys, is_readonly)
  build_class_attr(Hash, keys, is_readonly)
end

#build_string_attr(keys, is_readonly) ⇒ Object



43
44
45
46
47
48
49
50
51
52
# File 'lib/horcrux/entity.rb', line 43

def build_string_attr(keys, is_readonly)
  attr_reader *keys

  build_attrs(keys, is_readonly) do |key_s|
    ivar_key = "@#{key_s}"
    define_method "#{key_s}=" do |value|
      instance_variable_set ivar_key, value
    end
  end
end

#build_time_attr(keys, is_readonly) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/horcrux/entity.rb', line 82

def build_time_attr(keys, is_readonly)
  attr_reader *keys

  build_attrs(keys, is_readonly) do |key_s|
    ivar_key = "@#{key_s}"
    define_method "#{key_s}=" do |value|
      instance_variable_set ivar_key,
        value.respond_to?(:utc) ? 
          value :
          Time.at(value.to_i).utc
    end
  end
end

#from(hash = {}) ⇒ Object

Public



39
40
41
# File 'lib/horcrux/entity.rb', line 39

def from(hash = {})
  hash.respond_to?(@default_attribute) ? hash : new(hash)
end

#readonly(type, *keys) ⇒ Object

Public



29
30
31
32
33
34
35
36
# File 'lib/horcrux/entity.rb', line 29

def readonly(type, *keys)
  method = "build_#{type}_attr"
  if respond_to?(method)
    send method, keys, true
  else
    raise TypeError, "Type #{type.inspect} not recognized."
  end
end