Class: Moonrope::Structure

Inherits:
Object
  • Object
show all
Defined in:
lib/moonrope/structure.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base, name) { ... } ⇒ Structure

Initialize a new structure

Parameters:

Yields:

  • instance evals the contents within the structure DSL



31
32
33
34
35
36
37
38
# File 'lib/moonrope/structure.rb', line 31

def initialize(base, name, &block)
  @base = base
  @name = name
  @expansions = {}
  @attributes = {:basic => [], :full => [], :expansion => []}
  @dsl = Moonrope::DSL::StructureDSL.new(self)
  @dsl.instance_eval(&block) if block_given?
end

Instance Attribute Details

#attributesHash (readonly)

Returns attributes which should be included in this structure.

Returns:

  • (Hash)

    attributes which should be included in this structure



23
24
25
# File 'lib/moonrope/structure.rb', line 23

def attributes
  @attributes
end

#baseMoonrope::Base (readonly)

Returns the base API.

Returns:



20
21
22
# File 'lib/moonrope/structure.rb', line 20

def base
  @base
end

#basicProc

Returns the basic data block.

Returns:

  • (Proc)

    the basic data block



8
9
10
# File 'lib/moonrope/structure.rb', line 8

def basic
  @basic
end

#dslMoonrope::DSL::StructureDSL (readonly)

Returns the DSL.

Returns:



14
15
16
# File 'lib/moonrope/structure.rb', line 14

def dsl
  @dsl
end

#expansionsHash (readonly)

Returns all expansions for the structure.

Returns:

  • (Hash)

    all expansions for the structure



17
18
19
# File 'lib/moonrope/structure.rb', line 17

def expansions
  @expansions
end

#fullProc

Returns the full data block.

Returns:

  • (Proc)

    the full data block



11
12
13
# File 'lib/moonrope/structure.rb', line 11

def full
  @full
end

#nameSymbol

Returns the name of the structure.

Returns:

  • (Symbol)

    the name of the structure



5
6
7
# File 'lib/moonrope/structure.rb', line 5

def name
  @name
end

Instance Method Details

#hash(object, options = {}) ⇒ Hash

Return a hash for this struture

Parameters:

  • object (Object)

    the object

  • options (Hash) (defaults to: {})

    additional options

Returns:

  • (Hash)


47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/moonrope/structure.rb', line 47

def hash(object, options = {})
  # Set up an environment
  environment = EvalEnvironment.new(base, options[:request], :o => object)
  
  # Set a new hash
  hash = Hash.new
  
  # Add the 'basic' structured fields
  DeepMerge.deep_merge! hash_for_attributes(@attributes[:basic], object, environment), hash
  
  # Always get a basic hash to work from
  if self.basic.is_a?(Proc)
    DeepMerge.deep_merge! environment.instance_eval(&self.basic), hash
  end
  
  # Enhance with the full hash if requested
  if options[:full]
    
    # Add the 'full' structured fields
    DeepMerge.deep_merge! hash_for_attributes(@attributes[:full], object, environment), hash
    
    if self.full.is_a?(Proc)
      full_hash = environment.instance_eval(&self.full)
      DeepMerge.deep_merge! full_hash,hash
    end
  end
  
  # Add expansions
  if options[:expansions]        
    
    # Add structured expansions
    @attributes[:expansion].each do |attribute|
      next if options[:expansions].is_a?(Array) && !options[:expansions].include?(attribute.name.to_sym)
      DeepMerge.deep_merge! hash_for_attributes([attribute], object, environment), hash
    end
    
    # Add the expansions
    expansions.each do |name, expansion|
      next if options[:expansions].is_a?(Array) && !options[:expansions].include?(name.to_sym)
      DeepMerge.deep_merge!({name.to_sym => environment.instance_eval(&expansion)}, hash)
    end
  end
  
  # Return the hash
  hash
end