|
| 1 | +module ActiveModel |
| 2 | + |
| 3 | + # == Active \Model Basic \Model |
| 4 | + # |
| 5 | + # Includes the required interface for an object to interact with |
| 6 | + # <tt>ActionPack</tt>, using different <tt>ActiveModel</tt> modules. |
| 7 | + # It includes model name introspections, conversions, translations and |
| 8 | + # validations. Besides that, it allows you to initialize the object with a |
| 9 | + # hash of attributes, pretty much like <tt>ActiveRecord</tt> does. |
| 10 | + # |
| 11 | + # A minimal implementation could be: |
| 12 | + # |
| 13 | + # class Person |
| 14 | + # include ActiveModel::Model |
| 15 | + # attr_accessor :name, :age |
| 16 | + # end |
| 17 | + # |
| 18 | + # person = Person.new(name: 'bob', age: '18') |
| 19 | + # person.name # => 'bob' |
| 20 | + # person.age # => 18 |
| 21 | + # |
| 22 | + # Note that, by default, <tt>ActiveModel::Model</tt> implements <tt>persisted?</tt> |
| 23 | + # to return +false+, which is the most common case. You may want to override |
| 24 | + # it in your class to simulate a different scenario: |
| 25 | + # |
| 26 | + # class Person |
| 27 | + # include ActiveModel::Model |
| 28 | + # attr_accessor :id, :name |
| 29 | + # |
| 30 | + # def persisted? |
| 31 | + # self.id == 1 |
| 32 | + # end |
| 33 | + # end |
| 34 | + # |
| 35 | + # person = Person.new(id: 1, name: 'bob') |
| 36 | + # person.persisted? # => true |
| 37 | + # |
| 38 | + # Also, if for some reason you need to run code on <tt>initialize</tt>, make |
| 39 | + # sure you call +super+ if you want the attributes hash initialization to |
| 40 | + # happen. |
| 41 | + # |
| 42 | + # class Person |
| 43 | + # include ActiveModel::Model |
| 44 | + # attr_accessor :id, :name, :omg |
| 45 | + # |
| 46 | + # def initialize(attributes={}) |
| 47 | + # super |
| 48 | + # @omg ||= true |
| 49 | + # end |
| 50 | + # end |
| 51 | + # |
| 52 | + # person = Person.new(id: 1, name: 'bob') |
| 53 | + # person.omg # => true |
| 54 | + # |
| 55 | + # For more detailed information on other functionalities available, please |
| 56 | + # refer to the specific modules included in <tt>ActiveModel::Model</tt> |
| 57 | + # (see below). |
| 58 | + module Model |
| 59 | + def self.included(base) #:nodoc: |
| 60 | + base.class_eval do |
| 61 | + extend ActiveModel::Naming |
| 62 | + extend ActiveModel::Translation |
| 63 | + include ActiveModel::Validations |
| 64 | + include ActiveModel::Conversion |
| 65 | + end |
| 66 | + end |
| 67 | + |
| 68 | + # Initializes a new model with the given +params+. |
| 69 | + # |
| 70 | + # class Person |
| 71 | + # include ActiveModel::Model |
| 72 | + # attr_accessor :name, :age |
| 73 | + # end |
| 74 | + # |
| 75 | + # person = Person.new(name: 'bob', age: '18') |
| 76 | + # person.name # => "bob" |
| 77 | + # person.age # => 18 |
| 78 | + def initialize(params={}) |
| 79 | + params.each do |attr, value| |
| 80 | + self.public_send("#{attr}=", value) |
| 81 | + end if params |
| 82 | + end |
| 83 | + |
| 84 | + # Indicates if the model is persisted. Default is +false+. |
| 85 | + # |
| 86 | + # class Person |
| 87 | + # include ActiveModel::Model |
| 88 | + # attr_accessor :id, :name |
| 89 | + # end |
| 90 | + # |
| 91 | + # person = Person.new(id: 1, name: 'bob') |
| 92 | + # person.persisted? # => false |
| 93 | + def persisted? |
| 94 | + false |
| 95 | + end |
| 96 | + end |
| 97 | +end |
0 commit comments