Module: Kangaroo::Attributes::ClassMethods

Defined in:
lib/kangaroo/model/attributes.rb

Instance Method Summary collapse

Instance Method Details

#attribute_namesArray

Get a list of available attributes

Returns:

  • (Array)

    attribute names



93
94
95
# File 'lib/kangaroo/model/attributes.rb', line 93

def attribute_names
  @attribute_names ||= []
end

#define_accessors(attribute_name) ⇒ Object

Define getter and setter for an attribute

Parameters:

  • attribute_name (String, Symbol)


100
101
102
103
104
105
106
107
108
109
110
# File 'lib/kangaroo/model/attributes.rb', line 100

def define_accessors attribute_name
  define_method attribute_name do
    read_attribute attribute_name
  end

  define_method "#{attribute_name}=" do |value|
    write_attribute attribute_name, value
  end

  attribute_names << attribute_name.to_s
end

#define_getter(attribute_name) ⇒ Object

Define getter

Parameters:

  • attribute_name (String, Symbol)


84
85
86
87
88
# File 'lib/kangaroo/model/attributes.rb', line 84

def define_getter attribute_name
  define_method attribute_name do
    read_attribute attribute_name
  end
end

#define_getters(*attribute_names) ⇒ Object

Define getters for attributes

Parameters:

  • attribute_names (Array)


75
76
77
78
79
# File 'lib/kangaroo/model/attributes.rb', line 75

def define_getters *attribute_names
  attribute_names.flatten.each do |name|
    define_getter name
  end
end

#define_multiple_accessors(*attribute_names) ⇒ Object

Define getters and setters for attributes

Parameters:

  • attribute_names (Array)


115
116
117
118
119
120
121
# File 'lib/kangaroo/model/attributes.rb', line 115

def define_multiple_accessors *attribute_names
  define_attribute_methods attribute_names.map(&:to_s)

  attribute_names.each do |attribute_name|
    define_accessors attribute_name
  end
end

#extend_attribute_methods(*attributes) ⇒ Object

If you need to customize your models, e.g. add attributes not covered by fields_get, you can call #extend_attribute_methods

Parameters:

  • attributes (Array)

    list of attribute names to define accessors for



65
66
67
68
69
70
# File 'lib/kangaroo/model/attributes.rb', line 65

def extend_attribute_methods *attributes
  attributes.flatten.each do |attr|
    next if attribute_names.include?(attr.to_s)
    define_accessors attr
  end
end