Module: MassiveRecord::ORM::Schema::TableInterface::ClassMethods
- Defined in:
- lib/massive_record/orm/schema/table_interface.rb
Instance Method Summary collapse
-
#add_field_to_column_family(family_name, *field_args) ⇒ Object
If you need to add fields to a column family dynamically, use this method.
-
#autoload_column_families_and_fields_with(column_names) ⇒ Object
Create column families and fields with incomming array of column names.
-
#column_family(name, &block) ⇒ Object
DSL method exposed into class.
Instance Method Details
#add_field_to_column_family(family_name, *field_args) ⇒ Object
If you need to add fields to a column family dynamically, use this method. It wraps functionality needed to keep the class in a consistent state. There is also an instance method defined which will inject default value to the object itself after defining the field.
41 42 43 44 45 46 47 48 49 50 |
# File 'lib/massive_record/orm/schema/table_interface.rb', line 41 def add_field_to_column_family(family_name, *field_args) ensure_column_families_exists field = Field.new_with_arguments_from_dsl(*field_args) column_families.family_by_name_or_new(family_name) << field undefine_attribute_methods if respond_to? :undefine_attribute_methods field end |
#autoload_column_families_and_fields_with(column_names) ⇒ Object
Create column families and fields with incomming array of column names. It should be on a unique and complete form like [“info:name”, “info:phone”]
56 57 58 59 60 61 62 63 64 |
# File 'lib/massive_record/orm/schema/table_interface.rb', line 56 def autoload_column_families_and_fields_with(column_names) column_names.each do |column_family_and_column_name| family_name, column_name = column_family_and_column_name.split(":") if family = column_families.family_by_name(family_name) and family.autoload_fields? family.add? Field.new(:name => column_name) end end end |
#column_family(name, &block) ⇒ Object
DSL method exposed into class. Makes it possible to do:
class Person < MassiveRecord::ORM::Table
column_family :info do
field :name
field :age, :integer, :default => 0
field :points, :integer, :column => :number_of_points
end
end
30 31 32 33 |
# File 'lib/massive_record/orm/schema/table_interface.rb', line 30 def column_family(name, &block) ensure_column_families_exists column_families.family_by_name_or_new(name).instance_eval(&block) end |